|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Aop; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\BuilderFactory; |
|
8
|
|
|
use PhpParser\Node\Identifier; |
|
9
|
|
|
use PhpParser\Node\Name; |
|
10
|
|
|
use PhpParser\Node\NullableType; |
|
11
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
12
|
|
|
use PhpParser\NodeAbstract; |
|
13
|
|
|
use PhpParser\Parser; |
|
14
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
15
|
|
|
|
|
16
|
|
|
final class CodeGenMethod |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \PhpParser\Parser |
|
20
|
|
|
*/ |
|
21
|
|
|
private $parser; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \PhpParser\BuilderFactory |
|
25
|
|
|
*/ |
|
26
|
|
|
private $factory; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \PhpParser\PrettyPrinter\Standard |
|
30
|
|
|
*/ |
|
31
|
|
|
private $printer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
Parser $parser, |
|
38
|
|
|
BuilderFactory $factory, |
|
39
|
|
|
Standard $printer |
|
40
|
|
|
) { |
|
41
|
|
|
$this->parser = $parser; |
|
42
|
|
|
$this->factory = $factory; |
|
43
|
|
|
$this->printer = $printer; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getMethods(\ReflectionClass $class, BindInterface $bind, CodeVisitor $code) : array |
|
47
|
|
|
{ |
|
48
|
|
|
$bindingMethods = array_keys($bind->getBindings()); |
|
49
|
|
|
$classMethods = $code->classMethod; |
|
50
|
|
|
$methods = []; |
|
51
|
|
|
foreach ($classMethods as $classMethod) { |
|
52
|
|
|
$methodName = $classMethod->name->name; |
|
53
|
|
|
$method = new \ReflectionMethod($class->name, $methodName); |
|
|
|
|
|
|
54
|
33 |
|
$isBindingMethod = in_array($methodName, $bindingMethods, true); |
|
55
|
|
|
/* @var $method \ReflectionMethod */ |
|
56
|
|
|
$isPublic = $classMethod->flags === Class_::MODIFIER_PUBLIC; |
|
57
|
|
|
if ($isBindingMethod && $isPublic) { |
|
58
|
|
|
$methodInsideStatements = $this->getTemplateMethodNodeStmts( |
|
59
|
33 |
|
$classMethod->getReturnType() |
|
60
|
33 |
|
); |
|
61
|
33 |
|
// replace statements in the method |
|
62
|
33 |
|
$classMethod->stmts = $methodInsideStatements; |
|
63
|
33 |
|
$methods[] = $classMethod; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $methods; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
17 |
|
* @param null|Identifier|Name|NullableType $returnType |
|
72
|
|
|
*/ |
|
73
|
17 |
|
private function getTemplateMethodNodeStmts(?NodeAbstract $returnType) : array |
|
74
|
17 |
|
{ |
|
75
|
17 |
|
$code = $this->isReturnVoid($returnType) ? AopTemplate::RETURN_VOID : AopTemplate::RETURN; |
|
76
|
17 |
|
$node = $this->parser->parse($code)[0]; |
|
77
|
16 |
|
if (! $node instanceof Class_) { |
|
78
|
16 |
|
throw new \LogicException; // @codeCoverageIgnore |
|
79
|
|
|
} |
|
80
|
16 |
|
$methodNode = $node->getMethods()[0]; |
|
81
|
16 |
|
if ($methodNode->stmts === null) { |
|
82
|
|
|
throw new \LogicException; // @codeCoverageIgnore |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
17 |
|
return $methodNode->stmts; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param null|Identifier|Name|NullableType $returnType |
|
90
|
|
|
*/ |
|
91
|
|
|
private function isReturnVoid(?NodeAbstract $returnType) : bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $returnType instanceof Identifier && $returnType->name === 'void'; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.