mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2025-01-22 05:11:39 +01:00
Add setReturnType() method to function/method builders
Also support scalar type hints in existing setTypeHint() method on params.
This commit is contained in:
parent
2d0c3b70f8
commit
39f93f09f9
@ -29,6 +29,7 @@ $node = $factory->namespace('Name\Space')
|
||||
->addStmt($factory->method('someMethod')
|
||||
->makePublic()
|
||||
->makeAbstract() // ->makeFinal()
|
||||
->setReturnType('bool')
|
||||
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
||||
->setDocComment('/**
|
||||
* This method does something.
|
||||
@ -74,7 +75,7 @@ abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/
|
||||
public abstract function someMethod(SomeClass $someParam);
|
||||
public abstract function someMethod(SomeClass $someParam) : bool;
|
||||
protected function anotherMethod($someParam = 'test')
|
||||
{
|
||||
print $someParam;
|
||||
|
@ -10,6 +10,7 @@ abstract class FunctionLike extends Declaration
|
||||
{
|
||||
protected $returnByRef = false;
|
||||
protected $params = array();
|
||||
protected $returnType = null;
|
||||
|
||||
/**
|
||||
* Make the function return by reference.
|
||||
@ -55,4 +56,23 @@ abstract class FunctionLike extends Declaration
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set the return type for php7
|
||||
*
|
||||
* @param string $type support string, int, float, bool, array,
|
||||
* class names, interface and callable.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setReturnType($type)
|
||||
{
|
||||
if (in_array($type, array('array', 'callable', 'string', 'int', 'float', 'bool'))) {
|
||||
$this->returnType = $type;
|
||||
} else {
|
||||
$this->returnType = $this->normalizeName($type);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +40,10 @@ class Function_ extends FunctionLike
|
||||
*/
|
||||
public function getNode() {
|
||||
return new Stmt\Function_($this->name, array(
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'stmts' => $this->stmts,
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'returnType' => $this->returnType,
|
||||
'stmts' => $this->stmts,
|
||||
), $this->attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -116,10 +116,11 @@ class Method extends FunctionLike
|
||||
*/
|
||||
public function getNode() {
|
||||
return new Stmt\ClassMethod($this->name, array(
|
||||
'type' => $this->type,
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'stmts' => $this->stmts,
|
||||
'type' => $this->type,
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'returnType' => $this->returnType,
|
||||
'stmts' => $this->stmts,
|
||||
), $this->attributes);
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class Param extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setTypeHint($type) {
|
||||
if ($type === 'array' || $type === 'callable') {
|
||||
if (in_array($type, array('array', 'callable', 'string', 'int', 'float', 'bool'))) {
|
||||
$this->type = $type;
|
||||
} else {
|
||||
$this->type = $this->normalizeName($type);
|
||||
@ -73,4 +73,4 @@ class Param extends PhpParser\BuilderAbstract
|
||||
$this->name, $this->default, $this->type, $this->byRef
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ class BuilderFactory
|
||||
{
|
||||
/**
|
||||
* Creates a namespace builder.
|
||||
*
|
||||
*
|
||||
* @param null|string|Node\Name $name Name of the namespace
|
||||
*
|
||||
* @return Builder\Namespace_ The created namespace builder
|
||||
|
@ -76,6 +76,16 @@ class FunctionTest extends \PHPUnit_Framework_TestCase
|
||||
)), $node);
|
||||
}
|
||||
|
||||
public function testReturnType() {
|
||||
$node = $this->createFunctionBuilder('test')
|
||||
->setReturnType('bool')
|
||||
->getNode();
|
||||
|
||||
$this->assertEquals(new Stmt\Function_('test', array(
|
||||
'returnType' => 'bool'
|
||||
), array()), $node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Expected parameter node, got "Name"
|
||||
|
@ -120,6 +120,15 @@ class MethodTest extends \PHPUnit_Framework_TestCase
|
||||
)), $node);
|
||||
}
|
||||
|
||||
public function testReturnType() {
|
||||
$node = $this->createMethodBuilder('test')
|
||||
->setReturnType('bool')
|
||||
->getNode();
|
||||
$this->assertEquals(new Stmt\ClassMethod('test', array(
|
||||
'returnType' => 'bool'
|
||||
), array()), $node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Cannot add statements to an abstract method
|
||||
|
Loading…
x
Reference in New Issue
Block a user