Completed
Push — master ( c79ece...dd6614 )
by Alexander
10s
created

FunctionProxy::getJoinPoint()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
crap 12
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Proxy;
12
13
use Go\Aop\Advice;
14
use Go\Aop\Framework\ReflectionFunctionInvocation;
15
use Go\Aop\Intercept\FunctionInvocation;
16
use Go\Core\AspectContainer;
17
use Go\Core\AspectKernel;
18
use Go\Core\LazyAdvisorAccessor;
19
use Go\ParserReflection\ReflectionFileNamespace;
20
use ReflectionFunction;
21
22
/**
23
 * Function proxy builder that is used to generate a proxy-function from the list of joinpoints
24
 */
25
class FunctionProxy extends AbstractProxy
26
{
27
28
    /**
29
     * List of advices for functions
30
     *
31
     * @var array
32
     */
33
    protected static $functionAdvices = [];
34
35
    /**
36
     * Name for the current namespace
37
     *
38
     * @var string
39
     */
40
    protected $namespace = '';
41
42
    /**
43
     * Source code for functions
44
     *
45
     * @var array Name of the function => source code for it
46
     */
47
    protected $functionsCode = [];
48
49
    /**
50
     * Constructs functions stub class from namespace Reflection
51
     *
52
     * @param ReflectionFileNamespace $namespace Reflection of namespace
53
     * @param array $advices List of function advices
54
     *
55
     * @throws \InvalidArgumentException for invalid classes
56
     */
57
    public function __construct(ReflectionFileNamespace $namespace, array $advices = [])
58
    {
59
        parent::__construct($advices);
60
        $this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace of type object<Go\ParserReflecti...eflectionFileNamespace> is incompatible with the declared type string of property $namespace.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
62
        if (empty($advices[AspectContainer::FUNCTION_PREFIX])) {
63
            return;
64
        }
65
66
        foreach ($advices[AspectContainer::FUNCTION_PREFIX] as $pointName => $value) {
67
            $function = new ReflectionFunction($pointName);
68
            $this->override($function, $this->getJoinpointInvocationBody($function));
69
        }
70
    }
71
72
    /**
73
     * Returns a joinpoint for specific function in the namespace
74
     *
75
     * @param string $joinPointName Special joinpoint name
76
     * @param string $namespace Name of the namespace
77
     *
78
     * @return FunctionInvocation
79
     */
80
    public static function getJoinPoint($joinPointName, $namespace)
81
    {
82
        /** @var LazyAdvisorAccessor $accessor */
83
        static $accessor = null;
84
85
        if (!$accessor) {
86
            $accessor = AspectKernel::getInstance()->getContainer()->get('aspect.advisor.accessor');
87
        }
88
89
        $advices = self::$functionAdvices[$namespace][AspectContainer::FUNCTION_PREFIX][$joinPointName];
90
91
        $filledAdvices = [];
92
        foreach ($advices as $advisorName) {
93
            $filledAdvices[] = $accessor->$advisorName;
94
        }
95
96
        return new ReflectionFunctionInvocation($joinPointName, $filledAdvices);
97
    }
98
99
    /**
100
     * Inject advices for given trait
101
     *
102
     * NB This method will be used as a callback during source code evaluation to inject joinpoints
103
     *
104
     * @param string $namespace Aop child proxy class
105
     * @param array|Advice[] $advices List of advices to inject into class
106
     *
107
     * @return void
108
     */
109
    public static function injectJoinPoints($namespace, array $advices = [])
110
    {
111
        self::$functionAdvices[$namespace] = $advices;
112
    }
113
114
    /**
115
     * Override function with new body
116
     *
117
     * @param ReflectionFunction $function Function reflection
118
     * @param string $body New body for function
119
     *
120
     * @return $this
121
     */
122
    public function override(ReflectionFunction $function, $body)
123
    {
124
        $this->functionsCode[$function->name] = $this->getOverriddenFunction($function, $body);
125
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function __toString()
133
    {
134
        $functionsCode = (
135
            "<?php\n" . // Start of file header
136
            $this->namespace->getDocComment() . "\n" . // Doc-comment for file
0 ignored issues
show
Bug introduced by
The method getDocComment cannot be called on $this->namespace (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
137
            'namespace ' . // 'namespace' keyword
138
            $this->namespace->getName() . // Name
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $this->namespace (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
            ";\n" . // End of namespace name
140
            join("\n", $this->functionsCode) // Function definitions
141
        );
142
143
        return $functionsCode
144
            // Inject advices on call
145
            . PHP_EOL
146
            . '\\' . __CLASS__ . "::injectJoinPoints('"
147
                . $this->namespace->getName() . "',"
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $this->namespace (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148
                . var_export($this->advices, true) . ");";
149
    }
150
151
    /**
152
     * Creates definition for trait method body
153
     *
154
     * @param ReflectionFunction $function Method reflection
155
     *
156
     * @return string new method body
157
     */
158
    protected function getJoinpointInvocationBody(ReflectionFunction $function)
159
    {
160
        $class = '\\' . __CLASS__;
161
162
        $args = $this->prepareArgsLine($function);
163
164
        $return = 'return ';
165
        if (PHP_VERSION_ID >= 70100 && $function->hasReturnType()) {
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionFunction as the method hasReturnType() does only exist in the following sub-classes of ReflectionFunction: Go\ParserReflection\ReflectionFunction. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
166
            $returnType = (string) $function->getReturnType();
167
            if ($returnType === 'void') {
168
                // void return types should not return anything
169
                $return = '';
170
            }
171
        }
172
173
        return <<<BODY
174
static \$__joinPoint = null;
175
if (!\$__joinPoint) {
176
    \$__joinPoint = {$class}::getJoinPoint('{$function->name}', __NAMESPACE__);
177
}
178
{$return}\$__joinPoint->__invoke($args);
179
BODY;
180
    }
181
182
}
183