From 0e7dd7c222db966e3e14efb63ba770d302a484e1 Mon Sep 17 00:00:00 2001 From: Shingo Kumagai Date: Sat, 2 Jul 2016 02:22:36 +0900 Subject: [PATCH 1/5] Add test for return type with no arguments --- tests/CodeGenTest.php | 17 +++++++++++++ tests/Fake/FakePhp7ReturnTypeClass.php | 35 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/Fake/FakePhp7ReturnTypeClass.php diff --git a/tests/CodeGenTest.php b/tests/CodeGenTest.php index 9357140c..8d5e871c 100644 --- a/tests/CodeGenTest.php +++ b/tests/CodeGenTest.php @@ -34,4 +34,21 @@ public function testTypeDeclarations() } $this->assertContains($expected, $code); } + + public function testReturnType() + { + if (version_compare(PHP_VERSION, '7.0.0', '<')) { + return; + } + $codeGen = new CodeGen((new ParserFactory)->newInstance(), new BuilderFactory, new Standard); + $bind = new Bind; + $bind->bindInterceptors('returnTypeArray', []); + $code = $codeGen->generate('a', new \ReflectionClass(FakePhp7ReturnTypeClass::class), $bind); + if (method_exists(Method::class, 'setReturnType')) { + $expected = 'function returnTypeArray() : array'; + } else { + $expected = 'function returnTypeArray()'; + } + $this->assertContains($expected, $code); + } } diff --git a/tests/Fake/FakePhp7ReturnTypeClass.php b/tests/Fake/FakePhp7ReturnTypeClass.php new file mode 100644 index 00000000..0cd4dd5f --- /dev/null +++ b/tests/Fake/FakePhp7ReturnTypeClass.php @@ -0,0 +1,35 @@ + Date: Sat, 2 Jul 2016 02:33:27 +0900 Subject: [PATCH 2/5] clarify php parser version --- tests/CodeGenTest.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/CodeGenTest.php b/tests/CodeGenTest.php index 8d5e871c..3a27acef 100644 --- a/tests/CodeGenTest.php +++ b/tests/CodeGenTest.php @@ -27,11 +27,8 @@ public function testTypeDeclarations() $bind = new Bind; $bind->bindInterceptors('run', []); $code = $codeGen->generate('a', new \ReflectionClass(FakePhp7Class::class), $bind); - if (method_exists(Method::class, 'setReturnType')) { - $expected = 'function run(string $a, int $b, float $c, bool $d) : array'; - } else { - $expected = 'function run(string $a, int $b, float $c, bool $d)'; - } + $isOverPhpParserVer2 = method_exists(Method::class, 'setReturnType'); + $expected = $isOverPhpParserVer2 ? 'function run(string $a, int $b, float $c, bool $d) : array' : 'function run(string $a, int $b, float $c, bool $d)'; $this->assertContains($expected, $code); } @@ -44,11 +41,8 @@ public function testReturnType() $bind = new Bind; $bind->bindInterceptors('returnTypeArray', []); $code = $codeGen->generate('a', new \ReflectionClass(FakePhp7ReturnTypeClass::class), $bind); - if (method_exists(Method::class, 'setReturnType')) { - $expected = 'function returnTypeArray() : array'; - } else { - $expected = 'function returnTypeArray()'; - } + $isOverPhpParserVer2 = method_exists(Method::class, 'setReturnType'); + $expected = $isOverPhpParserVer2 ? 'function returnTypeArray() : array' : 'function returnTypeArray()'; $this->assertContains($expected, $code); } } From 1f2a314135d9f414d4ccc4e71cf544fc647fda33 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sat, 2 Jul 2016 02:42:01 +0900 Subject: [PATCH 3/5] add return type in php7 --- src/CodeGenMethod.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/CodeGenMethod.php b/src/CodeGenMethod.php index 79a72966..548434fb 100644 --- a/src/CodeGenMethod.php +++ b/src/CodeGenMethod.php @@ -88,8 +88,13 @@ private function getMethod(\ReflectionMethod $method) { $methodStmt = $this->factory->method($method->name); $params = $method->getParameters(); + $isOverPhp7 = version_compare(PHP_VERSION, '7.0.0') >= 0; foreach ($params as $param) { - $methodStmt = $this->getMethodStatement($param, $methodStmt); + $methodStmt = $this->getMethodStatement($param, $methodStmt, $isOverPhp7); + } + $returnType = (string) $method->getReturnType(); + if ($isOverPhp7) { + $this->setReturnType($returnType, $methodStmt); } $methodInsideStatements = $this->getMethodInsideStatement(); $methodStmt->addStmts($methodInsideStatements); @@ -103,21 +108,18 @@ private function getMethod(\ReflectionMethod $method) * * @param \ReflectionParameter $param * @param \PHPParser\Builder\Method $methodStmt + * @param bool $isOverPhp7 * * @return \PHPParser\Builder\Method */ - private function getMethodStatement(\ReflectionParameter $param, Method $methodStmt) + private function getMethodStatement(\ReflectionParameter $param, Method $methodStmt, $isOverPhp7) { - $isOverPhp7 = version_compare(PHP_VERSION, '7.0.0') >= 0; /** @var $paramStmt Param */ $paramStmt = $this->factory->param($param->name); /* @var $param \ReflectionParameter */ $typeHint = $param->getClass(); $this->setParameterType($param, $paramStmt, $isOverPhp7, $typeHint); $this->setDefault($param, $paramStmt); - if ($isOverPhp7) { - $this->setReturnType($param, $methodStmt, $isOverPhp7); - } $methodStmt->addParam($paramStmt); return $methodStmt; @@ -209,14 +211,13 @@ private function setParameterType(\ReflectionParameter $param, Param $paramStmt, } /** - * @param \ReflectionParameter $param + * @param string $returnType * @param Method $methodStmt */ - private function setReturnType(\ReflectionParameter $param, Method $methodStmt) + private function setReturnType($returnType, Method $methodStmt) { - $returnType = $param->getDeclaringFunction()->getReturnType(); if ($returnType && method_exists($methodStmt, 'setReturnType')) { - $methodStmt->setReturnType((string)$returnType); // @codeCoverageIgnore + $methodStmt->setReturnType($returnType); // @codeCoverageIgnore } } } From 4245fd7791c1da71435b04f9dc1e529694223cb6 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sat, 2 Jul 2016 02:58:11 +0900 Subject: [PATCH 4/5] cleanup --- src/Bind.php | 24 ++++++++++++------------ src/CodeGen.php | 1 + src/CodeGenMethod.php | 1 + src/Compiler.php | 6 +++--- tests/Fake/FakeMatcher.php | 2 ++ tests/Fake/FakeWeaved.php | 1 + 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Bind.php b/src/Bind.php index b2102078..2ec22522 100644 --- a/src/Bind.php +++ b/src/Bind.php @@ -40,8 +40,8 @@ public function bind($class, array $pointcuts) } /** - * @param ReflectionClass $class - * @param Pointcut[] $pointcuts + * @param \ReflectionClass $class + * @param array $pointcuts */ private function annotatedMethodsMatch(\ReflectionClass $class, array &$pointcuts) { @@ -52,9 +52,9 @@ private function annotatedMethodsMatch(\ReflectionClass $class, array &$pointcut } /** - * @param ReflectionClass $class - * @param ReflectionMethod $method - * @param Pointcut[] $pointcuts + * @param \ReflectionClass $class + * @param \ReflectionMethod $method + * @param Pointcut[] $pointcuts */ private function annotatedMethodMatch(\ReflectionClass $class, \ReflectionMethod $method, array $pointcuts) { @@ -75,9 +75,9 @@ private function annotatedMethodMatch(\ReflectionClass $class, \ReflectionMethod } /** - * @param ReflectionClass $class - * @param ReflectionMethod $method - * @param PointCut $pointCut + * @param \ReflectionClass $class + * @param \ReflectionMethod $method + * @param PointCut $pointCut */ private function annotatedMethodMatchBind(\ReflectionClass $class, \ReflectionMethod $method, PointCut $pointCut) { @@ -144,10 +144,10 @@ public function getAnnotationPointcuts(array &$pointcuts) } /** - * @param ReflectionClass $class - * @param ReflectionMethod $method - * @param Pointcut[] $pointcuts - * @param array $annotations + * @param \ReflectionClass $class + * @param \ReflectionMethod $method + * @param Pointcut[] $pointcuts + * @param array $annotations * * @return array */ diff --git a/src/CodeGen.php b/src/CodeGen.php index 7ce81660..3d87c341 100644 --- a/src/CodeGen.php +++ b/src/CodeGen.php @@ -64,6 +64,7 @@ public function __construct( /** * @param string $class * @param \ReflectionClass $sourceClass + * @param BindInterface $bind * * @return string */ diff --git a/src/CodeGenMethod.php b/src/CodeGenMethod.php index 548434fb..2afbd15d 100644 --- a/src/CodeGenMethod.php +++ b/src/CodeGenMethod.php @@ -57,6 +57,7 @@ public function __construct( /** * @param \ReflectionClass $class + * @param BindInterface $bind * * @return array */ diff --git a/src/Compiler.php b/src/Compiler.php index 14c78fb2..88abea50 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -122,9 +122,9 @@ private function hasBoundMethod($class, BindInterface $bind) } /** - * @param string $newClass - * @param ReflectionClass $sourceClass - * @param string $file + * @param string $newClass + * @param \ReflectionClass $sourceClass + * @param string $file */ private function includeGeneratedCode($newClass, \ReflectionClass $sourceClass, $file, BindInterface $bind) { diff --git a/tests/Fake/FakeMatcher.php b/tests/Fake/FakeMatcher.php index 4a7917f5..a9950c3d 100644 --- a/tests/Fake/FakeMatcher.php +++ b/tests/Fake/FakeMatcher.php @@ -11,6 +11,7 @@ public function __construct($arg1 = true, $arg2 = true) public function matchesClass(\ReflectionClass $class, array $arguments) { + unset($class); if (isset($arguments[1])) { return $arguments[0] && $arguments[1]; } @@ -20,6 +21,7 @@ public function matchesClass(\ReflectionClass $class, array $arguments) public function matchesMethod(\ReflectionMethod $method, array $arguments) { + unset($method); if (isset($arguments[1])) { return $arguments[0] && $arguments[1]; } diff --git a/tests/Fake/FakeWeaved.php b/tests/Fake/FakeWeaved.php index de7155b8..dac30860 100644 --- a/tests/Fake/FakeWeaved.php +++ b/tests/Fake/FakeWeaved.php @@ -15,6 +15,7 @@ public function ___postConstruct(Bind $bind) public function returnSame($a) { + unset($a); // direct call if (!$this->rayAopIntercept || !isset($this->bind[__FUNCTION__])) { return call_user_func_array('parent::' . __FUNCTION__, func_get_args()); From 93aaed330b261f9c7cf5a755dc5c4cf4f47d3796 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sat, 2 Jul 2016 03:00:35 +0900 Subject: [PATCH 5/5] getReturnType() only in php7 --- src/CodeGenMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CodeGenMethod.php b/src/CodeGenMethod.php index 2afbd15d..6fec688f 100644 --- a/src/CodeGenMethod.php +++ b/src/CodeGenMethod.php @@ -93,8 +93,8 @@ private function getMethod(\ReflectionMethod $method) foreach ($params as $param) { $methodStmt = $this->getMethodStatement($param, $methodStmt, $isOverPhp7); } - $returnType = (string) $method->getReturnType(); if ($isOverPhp7) { + $returnType = (string) $method->getReturnType(); $this->setReturnType($returnType, $methodStmt); } $methodInsideStatements = $this->getMethodInsideStatement();