Allow null in ClassMethod::getStmts()

This commit is contained in:
Nikita Popov 2017-05-07 19:54:32 +02:00
parent b1af3d1f7d
commit 73dc35cbbd
5 changed files with 33 additions and 3 deletions

View File

@ -60,6 +60,7 @@ class Closure extends Expr implements FunctionLike
return $this->returnType;
}
/** @return Node\Stmt[] */
public function getStmts() : array {
return $this->stmts;
}

View File

@ -30,7 +30,7 @@ interface FunctionLike extends Node
/**
* The function body
*
* @return Node\Stmt[]
* @return Node\Stmt[]|null
*/
public function getStmts() : array;
public function getStmts();
}

View File

@ -77,7 +77,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike
return $this->returnType;
}
public function getStmts() : array {
public function getStmts() {
return $this->stmts;
}

View File

@ -58,6 +58,7 @@ class Function_ extends Node\Stmt implements FunctionLike
return $this->returnType;
}
/** @return Node\Stmt[] */
public function getStmts() : array {
return $this->stmts;
}

View File

@ -2,6 +2,9 @@
namespace PhpParser\Node\Stmt;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PHPUnit\Framework\TestCase;
class ClassMethodTest extends TestCase
@ -93,4 +96,29 @@ class ClassMethodTest extends TestCase
array('__debuginfo'),
);
}
public function testFunctionLike() {
$param = new Param(new Variable('a'));
$type = new Name('Foo');
$return = new Return_(new Variable('a'));
$method = new ClassMethod('test', [
'byRef' => false,
'params' => [$param],
'returnType' => $type,
'stmts' => [$return],
]);
$this->assertFalse($method->returnsByRef());
$this->assertSame([$param], $method->getParams());
$this->assertSame($type, $method->getReturnType());
$this->assertSame([$return], $method->getStmts());
$method = new ClassMethod('test', [
'byRef' => true,
'stmts' => null,
]);
$this->assertTrue($method->returnsByRef());
$this->assertNull($method->getStmts());
}
}