|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Aop; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\BuilderFactory; |
|
8
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
9
|
|
|
use PhpParser\Parser; |
|
10
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
11
|
|
|
|
|
12
|
|
|
final class CodeGenMethod |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \PhpParser\Parser |
|
16
|
|
|
*/ |
|
17
|
|
|
private $parser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \PhpParser\BuilderFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
private $factory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \PhpParser\PrettyPrinter\Standard |
|
26
|
|
|
*/ |
|
27
|
|
|
private $printer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( |
|
33
|
|
|
Parser $parser, |
|
34
|
|
|
BuilderFactory $factory, |
|
35
|
|
|
Standard $printer |
|
36
|
|
|
) { |
|
37
|
|
|
$this->parser = $parser; |
|
38
|
|
|
$this->factory = $factory; |
|
39
|
|
|
$this->printer = $printer; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getMethods(\ReflectionClass $class, BindInterface $bind, CodeVisitor $code) : array |
|
43
|
|
|
{ |
|
44
|
|
|
$bindingMethods = array_keys($bind->getBindings()); |
|
45
|
|
|
$classMethods = $code->classMethod; |
|
46
|
|
|
$methods = []; |
|
47
|
|
|
foreach ($classMethods as $classMethod) { |
|
48
|
|
|
$methodName = $classMethod->name->name; |
|
49
|
|
|
$method = new \ReflectionMethod($class->name, $methodName); |
|
|
|
|
|
|
50
|
|
|
$isBindingMethod = in_array($methodName, $bindingMethods, true); |
|
51
|
|
|
/* @var $method \ReflectionMethod */ |
|
52
|
|
|
$isPublic = $classMethod->flags === Class_::MODIFIER_PUBLIC; |
|
53
|
|
|
if ($isBindingMethod && $isPublic) { |
|
54
|
|
|
$methodInsideStatements = $this->getTemplateMethodNodeStmts(); |
|
55
|
|
|
// replace statements in the method |
|
56
|
33 |
|
$classMethod->stmts = $methodInsideStatements; |
|
57
|
|
|
$methods[] = $classMethod; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
33 |
|
return $methods; |
|
62
|
33 |
|
} |
|
63
|
33 |
|
|
|
64
|
33 |
|
private function getTemplateMethodNodeStmts() : array |
|
65
|
33 |
|
{ |
|
66
|
|
|
$code = $this->getTemplateCode(); |
|
67
|
|
|
$node = $this->parser->parse($code)[0]; |
|
68
|
|
|
if (! $node instanceof Class_) { |
|
69
|
|
|
throw new \LogicException; // @codeCoverageIgnore |
|
70
|
|
|
} |
|
71
|
|
|
$methodNode = $node->getMethods()[0]; |
|
72
|
|
|
if ($methodNode->stmts === null) { |
|
73
|
17 |
|
throw new \LogicException; // @codeCoverageIgnore |
|
74
|
|
|
} |
|
75
|
17 |
|
|
|
76
|
17 |
|
return $methodNode->stmts; |
|
77
|
17 |
|
} |
|
78
|
17 |
|
|
|
79
|
16 |
|
/** |
|
80
|
16 |
|
* Return CodeGenTemplate string |
|
81
|
|
|
* |
|
82
|
16 |
|
* Compiler takes only the statements in the method. Then create new inherit code with interceptors. |
|
83
|
16 |
|
* |
|
84
|
|
|
* @see http://paul-m-jones.com/archives/182 |
|
85
|
|
|
* @see http://stackoverflow.com/questions/8343399/calling-a-function-with-explicit-parameters-vs-call-user-func-array |
|
86
|
|
|
* @see http://stackoverflow.com/questions/1796100/what-is-faster-many-ifs-or-else-if |
|
87
|
17 |
|
* @see http://stackoverflow.com/questions/2401478/why-is-faster-than-in-php |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getTemplateCode() : string |
|
90
|
|
|
{ |
|
91
|
|
|
return /* @lang PHP */ |
|
92
|
|
|
<<<'EOT' |
|
93
|
|
|
<?php |
|
94
|
|
|
class AopTemplate extends \Ray\Aop\FakeMock implements Ray\Aop\WeavedInterface |
|
95
|
|
|
{ |
|
96
|
|
|
/** |
|
97
|
15 |
|
* @var array |
|
98
|
|
|
* |
|
99
|
15 |
|
* [$methodName => [$interceptorA[]][] |
|
100
|
15 |
|
*/ |
|
101
|
15 |
|
public $bindings; |
|
102
|
13 |
|
|
|
103
|
|
|
/** |
|
104
|
15 |
|
* @var bool |
|
105
|
15 |
|
*/ |
|
106
|
5 |
|
private $isAspect = true; |
|
107
|
|
|
|
|
108
|
15 |
|
/** |
|
109
|
15 |
|
* Method Template |
|
110
|
|
|
* |
|
111
|
15 |
|
* @param mixed $a |
|
112
|
|
|
*/ |
|
113
|
|
|
public function templateMethod($a, $b) |
|
114
|
|
|
{ |
|
115
|
|
|
if (! $this->isAspect) { |
|
116
|
|
|
$this->isAspect = true; |
|
117
|
13 |
|
|
|
118
|
|
|
return call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args()); |
|
119
|
|
|
} |
|
120
|
13 |
|
|
|
121
|
|
|
$this->isAspect = false; |
|
122
|
13 |
|
$result = (new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed(); |
|
123
|
13 |
|
$this->isAspect = true; |
|
124
|
13 |
|
|
|
125
|
|
|
return $result; |
|
126
|
13 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
EOT; |
|
129
|
15 |
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.