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

Parent method intercept #181

Merged
merged 9 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"psr-4": {
"Ray\\Aop\\": ["tests/", "tests/Fake/"]
},
"files": ["tests/Fake/FakeGlobalNamespaced.php"]
"files": ["tests/Fake/FakeGlobalNamespaced.php", "tests/Fake/FakeGlobalEmptyNamespaced.php"]
},
"suggest": {
"ray/di": "A dependency injection framework"
Expand Down
2 changes: 1 addition & 1 deletion src/AopClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
public function __invoke(CodeVisitor $visitor, ReflectionClass $sourceClass, BindInterface $bind): Class_
{
assert($visitor->class instanceof Class_);
$methods = $this->codeGenMethod->getMethods($bind, $visitor);
$methods = $this->codeGenMethod->getMethods($sourceClass, $bind, $visitor);
$propStms = ($this->aopProps)($sourceClass);
$classStm = $visitor->class;
$newClassName = ($this->aopClassName)((string) $visitor->class->name, $bind->toString(''));
Expand Down
49 changes: 39 additions & 10 deletions src/CodeGenMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,51 @@

namespace Ray\Aop;

use Doctrine\Common\Annotations\AnnotationException;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeAbstract;
use PhpParser\Parser;
use Ray\Aop\Exception\InvalidSourceClassException;
use ReflectionClass;
use ReflectionMethod;

use function array_keys;
use function assert;
use function in_array;

/** @SuppressWarnings(PHPMD.CouplingBetweenObjects) */
koriym marked this conversation as resolved.
Show resolved Hide resolved
final class CodeGenMethod
{
/** @var Parser */
private $parser;

/**
* @throws AnnotationException
*/
/** @var VisitorFactory */
private $visitorFactory;

public function __construct(
Parser $parser
) {
$this->parser = $parser;
$this->visitorFactory = new VisitorFactory($parser);
}

/**
* @param ReflectionClass<object> $reflectionClass
*
* @return ClassMethod[]
*/
public function getMethods(BindInterface $bind, CodeVisitor $code): array
public function getMethods(ReflectionClass $reflectionClass, BindInterface $bind, CodeVisitor $code): array
{
$bindingMethods = array_keys($bind->getBindings());
$classMethods = $code->classMethod;
$reflectionMethods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
$methods = [];
foreach ($classMethods as $classMethod) {
$methodName = $classMethod->name->name;
foreach ($reflectionMethods as $reflectionMethod) {
$methodName = $reflectionMethod->getName();
$isBindingMethod = in_array($methodName, $bindingMethods, true);
$isPublic = $classMethod->flags === Class_::MODIFIER_PUBLIC;
if ($isBindingMethod && $isPublic) {
if ($isBindingMethod) {
$classMethod = $this->getClassMethod($reflectionClass, $reflectionMethod, $code);
$methodInsideStatements = $this->getTemplateMethodNodeStmts(
$classMethod->getReturnType()
);
Expand Down Expand Up @@ -75,4 +81,27 @@ private function isReturnVoid(?NodeAbstract $returnType): bool
{
return $returnType instanceof Identifier && $returnType->name === 'void';
}

/** @param ReflectionClass<object> $sourceClass */
private function getClassMethod(
ReflectionClass $sourceClass,
ReflectionMethod $bindingMethod,
CodeVisitor $code
): ClassMethod {
$bindingMethodName = $bindingMethod->getName();
foreach ($code->classMethod as $classMethod) {
if ($classMethod->name->name === $bindingMethodName) {
return $classMethod;
}
}

$parentClass = $sourceClass->getParentClass();
if ($parentClass === false) {
throw new InvalidSourceClassException($sourceClass->getName()); // @codeCoverageIgnore
}

$code = ($this->visitorFactory)($parentClass);

return $this->getClassMethod($parentClass, $bindingMethod, $code);
}
}
30 changes: 30 additions & 0 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ray\Aop;

use Doctrine\Common\Annotations\AnnotationReader;
use FakeGlobalEmptyNamespaced;
use FakeGlobalNamespaced;
use LogicException;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,6 +21,7 @@
use function class_exists;
use function file_get_contents;
use function passthru;
use function property_exists;
use function serialize;
use function unserialize;

Expand Down Expand Up @@ -118,6 +120,26 @@ public function testMethodReturnValue(FakeMock $weaved): void
$this->assertSame(2, $result);
}

public function testParentMethodIntercept(): void
{
$mock = $this->compiler->newInstance(FakeMockChild::class, [], $this->bind);
assert($mock instanceof FakeMockChild);
assert(property_exists($mock, 'bindings'));
$mock->bindings = $this->bind->getBindings();
$result = $mock->returnSame(1);
$this->assertSame(2, $result);
}

public function testParentOfParentMethodIntercept(): void
{
$mock = $this->compiler->newInstance(FakeMockChildChild::class, [], $this->bind);
assert($mock instanceof FakeMockChild);
assert(property_exists($mock, 'bindings'));
$mock->bindings = $this->bind->getBindings();
$result = $mock->returnSame(1);
$this->assertSame(2, $result);
}

public function testGetPrivateVal(): void
{
$mock = $this->compiler->newInstance(FakeMock::class, [], $this->bind);
Expand Down Expand Up @@ -293,6 +315,14 @@ public function testUnnamespacedClass(): void
$this->assertSame(2, $mock->returnSame(1));
}

public function testEmptyNamespaceClass(): void
{
$mock = $this->compiler->newInstance(FakeGlobalEmptyNamespaced::class, [], $this->bind);
assert($mock instanceof FakeGlobalEmptyNamespaced);
$this->assertInstanceOf(FakeGlobalEmptyNamespaced::class, $mock);
$this->assertSame(2, $mock->returnSame(1));
}

public function testVoidFunction(): void
{
$bind = (new Bind())->bindInterceptors('returnTypeVoid', [new FakeChangeArgsInterceptor()]);
Expand Down
17 changes: 17 additions & 0 deletions tests/Fake/FakeGlobalEmptyNamespaced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace {
/** doc comment of FakeMock */
class FakeGlobalEmptyNamespaced
{
/**
* doc comment of returnSame
*/
public function returnSame($a)
{
return $a;
}
}
}
9 changes: 9 additions & 0 deletions tests/Fake/FakeMockChild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ray\Aop;

class FakeMockChild extends FakeMock
{
}
9 changes: 9 additions & 0 deletions tests/Fake/FakeMockChildChild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ray\Aop;

class FakeMockChildChild extends FakeMockChild
{
}
66 changes: 66 additions & 0 deletions tests/tmp_unerase/Ray_Aop_FakeWeaverMock_970308000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare (strict_types=1);
namespace Ray\Aop;

use Ray\Aop\WeavedInterface;
use Ray\Aop\ReflectiveMethodInvocation as Invocation;
/** doc comment of FakeMock */
class FakeWeaverMock_970308000 extends \Ray\Aop\FakeWeaverMock implements WeavedInterface
{
public $bind;
public $bindings = [];
public $methodAnnotations = 'a:0:{}';
public $classAnnotations = 'a:0:{}';
private $isAspect = true;
/**
* doc comment of returnSame
*/
public function returnSame($a)
{
if (!$this->isAspect) {
$this->isAspect = true;
call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args());
return;
}
$this->isAspect = false;
(new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed();
$this->isAspect = true;
}
/**
* doc comment of getSub
*/
public function getSub($a, $b)
{
if (!$this->isAspect) {
$this->isAspect = true;
call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args());
return;
}
$this->isAspect = false;
(new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed();
$this->isAspect = true;
}
public function returnValue(?FakeNum $num = null)
{
if (!$this->isAspect) {
$this->isAspect = true;
call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args());
return;
}
$this->isAspect = false;
(new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed();
$this->isAspect = true;
}
public function getPrivateVal()
{
if (!$this->isAspect) {
$this->isAspect = true;
call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args());
return;
}
$this->isAspect = false;
(new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed();
$this->isAspect = true;
}
}