Skip to content

Commit

Permalink
Merge pull request #63 from kuma-guy/support_for_type_declarations
Browse files Browse the repository at this point in the history
Fix set return type with no parameters in PHP7
  • Loading branch information
koriym authored Jul 1, 2016
2 parents cbcabcc + 93aaed3 commit 2afdaaf
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 29 deletions.
24 changes: 12 additions & 12 deletions src/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions src/CodeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(
/**
* @param string $class
* @param \ReflectionClass $sourceClass
* @param BindInterface $bind
*
* @return string
*/
Expand Down
22 changes: 12 additions & 10 deletions src/CodeGenMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function __construct(

/**
* @param \ReflectionClass $class
* @param BindInterface $bind
*
* @return array
*/
Expand Down Expand Up @@ -88,8 +89,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);
}
if ($isOverPhp7) {
$returnType = (string) $method->getReturnType();
$this->setReturnType($returnType, $methodStmt);
}
$methodInsideStatements = $this->getMethodInsideStatement();
$methodStmt->addStmts($methodInsideStatements);
Expand All @@ -103,21 +109,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;
Expand Down Expand Up @@ -209,14 +212,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
}
}
}
6 changes: 3 additions & 3 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 15 additions & 4 deletions tests/CodeGenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ 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);
}

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);
$isOverPhpParserVer2 = method_exists(Method::class, 'setReturnType');
$expected = $isOverPhpParserVer2 ? 'function returnTypeArray() : array' : 'function returnTypeArray()';
$this->assertContains($expected, $code);
}
}
2 changes: 2 additions & 0 deletions tests/Fake/FakeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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];
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Fake/FakePhp7ReturnTypeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Ray\Aop;

/**
* @FakeResource
* @FakeClassAnnotation
*/
class FakePhp7ReturnTypeClass
{
public function returnTypeArray(): array
{
return [1, 2, 3];
}

public function returnTypeBool(): bool
{
return true;
}

public function returnTypeFloat(): float
{
return 1.234;
}

public function returnTypeInteger(): int
{
return 1;
}

public function returnTypeString(): string
{
return 'this is string';
}
}
1 change: 1 addition & 0 deletions tests/Fake/FakeWeaved.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 2afdaaf

Please sign in to comment.