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

Conversation

NaokiTsuchiya
Copy link
Member

@NaokiTsuchiya NaokiTsuchiya commented Dec 3, 2021

When the following class B is compiled, echo method could not be intercepted.

// A.php
class A
{
    public function echo(): void
    {
        echo 'echo';
    }
}

// B.php
class B extends A
{
}

// run.php
require __DIR__ . '/bootstrap.php';

$compiler = new Ray\Aop\Compiler(__DIR__ . '/tmp');
$bind = (new Ray\Aop\Bind())->bindInterceptors('echo', [new Ray\Aop\NullInterceptor()]);
$b = $compiler->newInstance(Ray\Aop\Demo\B::class, [], $bind);

// generated class
<?php

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

use Ray\Aop\WeavedInterface;
use Ray\Aop\ReflectiveMethodInvocation as Invocation;
class B_1599363031 extends \Ray\Aop\Demo\B implements WeavedInterface
{
    public $bind;
    public $bindings = [];
    public $methodAnnotations = 'a:0:{}';
    public $classAnnotations = 'a:0:{}';
    private $isAspect = true;
}

In this PR, the following class is generated

<?php

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

use Ray\Aop\WeavedInterface;
use Ray\Aop\ReflectiveMethodInvocation as Invocation;
class B_1599363031 extends \Ray\Aop\Demo\B implements WeavedInterface
{
    public $bind;
    public $bindings = [];
    public $methodAnnotations = 'a:0:{}';
    public $classAnnotations = 'a:0:{}';
    private $isAspect = true;
    public function echo() : void
    {
        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;
    }
}

@codecov
Copy link

codecov bot commented Dec 4, 2021

Codecov Report

Merging #181 (07824c7) into 2.x (1354004) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##                 2.x      #181   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       165       168    +3     
===========================================
  Files             29        29           
  Lines            486       496   +10     
===========================================
+ Hits             486       496   +10     
Impacted Files Coverage Δ
src/AopClass.php 100.00% <100.00%> (ø)
src/CodeGenMethod.php 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1354004...07824c7. Read the comment docs.

src/CodeGenMethod.php Outdated Show resolved Hide resolved
NaokiTsuchiya and others added 2 commits December 5, 2021 14:46
Co-authored-by: Akihito Koriyama <akihito.koriyama@gmail.com>
tests/Fake/FakeMockChild.php Outdated Show resolved Hide resolved
tests/Fake/FakeMockChildChild.php Outdated Show resolved Hide resolved
NaokiTsuchiya and others added 2 commits December 5, 2021 19:56
Co-authored-by: Akihito Koriyama <akihito.koriyama@gmail.com>
Co-authored-by: Akihito Koriyama <akihito.koriyama@gmail.com>
@koriym
Copy link
Member

koriym commented Dec 5, 2021

https://github.com/nikic/PHP-Parser/blob/master/doc/component/FAQ.markdown#how-can-the-parent-of-a-node-be-obtained

これを見落としてました、

この"parent"はノードの事でした!><

@sonarcloud
Copy link

sonarcloud bot commented Dec 6, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@koriym koriym added the Feature label Dec 6, 2021
@SerjoA
Copy link

SerjoA commented Feb 3, 2022

thank you for your hard and excellent work

@koriym
Copy link
Member

koriym commented Feb 3, 2022

@SerjoA Thank you for using Ray.Aop! cc/ @NaokiTsuchiya

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants