Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return type and method overwrite logic #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ myPhpFile.findFunction('foo').setCode('return $a + $b');
var myPhpFile = new writer(contents, options);
myPhpFile.findClass('Foo_Class').setProperty('bar', 'foo-bar');
```

##### Find a class and create a new method:

If the method is not present on the class, it will be created. Otherwise, it will be overwritten.

```js
var myPhpFile = new writer(contents, options);
myPhpFile.findClass('Foo_Class').setMethod('test', 'int $test = 0', 'return $test;', 'private', '?int');
```

13 changes: 10 additions & 3 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,18 @@ Class.prototype.getMethod = function(name) {
/**
* Appends or update an function
*/
Class.prototype.setMethod = function(name, args, body, flags) {
Class.prototype.setMethod = function(name, args, body, flags, returnType) {
if (typeof returnType !== 'undefined') {
if (returnType[0] !== ':') {
returnType = ':' + returnType;
}
}

var method = this.getMethod(name);
if (!method) {
// append the function
var ast = parser.parseEval('class a { \n' +
flags + ' function ' + name + '(' + args + ') {\n' +
flags + ' function ' + name + '(' + args + ') '+ returnType + '{\n' +
body + '\n' +
'}\n' +
' }');
Expand All @@ -170,9 +176,10 @@ Class.prototype.setMethod = function(name, args, body, flags) {
);
} else {
// update the function
if (typeof flags !== 'undefined') method.setFlags(flags);
if (typeof flags !== 'undefined') method.setVisibility(flags);
if (typeof args !== 'undefined') method.setArgs(args);
if (typeof body !== 'undefined') method.setCode(body);
if (typeof returnType !== 'undefined') method.setReturnType(returnType);
}
return this;
};
Expand Down
70 changes: 55 additions & 15 deletions src/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,98 @@ var editor = require('./helpers/editor');
* @constructor
*/
var Method = function Method(ast) {
this.ast = ast;
this.ast = ast;
};
editor(Method, 'body');

/**
* Change the method name
*/
Method.prototype.setName = function(name) {
Method.prototype.setName = function (name) {
this.ast.name = name;
};

/**
* Sets the abstract flag value
*/
Method.prototype.setAbstract = function(flag) {
Method.prototype.setAbstract = function (flag) {
this.ast.isAbstract = flag === true;
return this;
};

/**
* Sets the abstract flag value
* Sets the final flag value
*/
Method.prototype.setFinal = function(flag) {
Method.prototype.setFinal = function (flag) {
this.ast.isFinal = flag === true;
return this;
};

/**
* Sets the abstract flag value
* Sets the static flag value
*/
Method.prototype.setStatic = function(flag) {
Method.prototype.setStatic = function (flag) {
this.ast.isStatic = flag === true;
return this;
};

/**
* Sets the abstract flag value
* Sets the visibility level value
*/
Method.prototype.setVisibility = function(level) {
Method.prototype.setVisibility = function (level) {
this.ast.visibility = level;
return this;
};

/**
* Locate the node in the specified ast
* Sets the method arguments
*/
Method.prototype.setArgs = function (args) {
var ast = parser.parseEval('class a { \n' +
' function dummy(' + args + ') { return null;}\n' +
' }');

this.ast.arguments = ast.children[0].body[0].arguments;
return this;
};

/**
* Sets the method body
*/
Method.prototype.setCode = function (code) {
var ast = parser.parseEval('class a { \n' +
'public function dummy($dummy = true) { ' + code + '}\n' +
' }');

this.ast.body = ast.children[0].body[0].body
return this;
};

/**
* Sets the return type
*/
Method.locate = function(ast, name) {
return filter(ast, 'method', function(node) {
if (node.name === name) {
return new Method(node);
Method.prototype.setReturnType = function (returnType) {
var ast = parser.parseEval('class a { \n' +
'public function dummy($dummy = true) ' + returnType + ' { return null; }\n' +
' }');

this.ast.type = ast.children[0].body[0].type;

if (returnType[1] === '?') {
this.ast.nullable = true;
}
});
return this;
};

/**
* Locate the node in the specified ast
*/
Method.locate = function (ast, name) {
return filter(ast, 'method', function (node) {
if (node.name === name) {
return new Method(node);
}
});
};

module.exports = Method;