1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Copyright (c) 2017 Constantin Galbenu <[email protected]> * |
4
|
|
|
******************************************************************************/ |
5
|
|
|
|
6
|
|
|
namespace Gica\CodeAnalysis; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator; |
10
|
|
|
use Gica\CodeAnalysis\Traits\FilesInDirectoryExtracter; |
11
|
|
|
|
12
|
|
|
class AggregateEventHandlersValidator |
13
|
|
|
{ |
14
|
|
|
use FilesInDirectoryExtracter; |
15
|
|
|
|
16
|
|
|
/** @var ListenerClassValidator */ |
17
|
|
|
private $classValidator; |
18
|
|
|
/** |
19
|
|
|
* @var PhpClassInFileInspector |
20
|
|
|
*/ |
21
|
|
|
private $phpClassInFileInspector; |
22
|
|
|
|
23
|
4 |
|
public function __construct( |
24
|
|
|
ListenerClassValidator $classValidator, |
25
|
|
|
PhpClassInFileInspector $phpClassInFileInspector = null |
26
|
|
|
) |
27
|
|
|
{ |
28
|
4 |
|
$this->classValidator = $classValidator; |
29
|
4 |
|
$this->phpClassInFileInspector = $phpClassInFileInspector ?? new PhpClassInFileInspector; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
4 |
|
public function validateEventHandlers($directory) |
34
|
|
|
{ |
35
|
4 |
|
$files = $this->getFilesInDirectory($directory); |
36
|
|
|
|
37
|
4 |
|
$files = $this->filterFiles($files); |
38
|
|
|
|
39
|
4 |
|
foreach ($files as $file) { |
40
|
4 |
|
$fullFilePath = $file; |
41
|
|
|
|
42
|
4 |
|
$this->validateFile($fullFilePath); |
43
|
|
|
} |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $filePath |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
4 |
|
protected function isListenerFileName($filePath) |
51
|
|
|
{ |
52
|
4 |
|
return preg_match('#\.php$#ims', $filePath); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $fullFilePath |
57
|
|
|
*/ |
58
|
4 |
|
protected function validateFile($fullFilePath) |
59
|
|
|
{ |
60
|
4 |
|
$fqn = $this->phpClassInFileInspector->getFullyQualifiedClassName($fullFilePath); |
61
|
|
|
|
62
|
4 |
|
if ($fqn !== null) { |
63
|
4 |
|
$this->validateEventHandlersInClass($fqn); |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
4 |
|
protected function filterFiles(array $files) |
68
|
|
|
{ |
69
|
4 |
|
return array_filter($files, [$this, 'isListenerFileName']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $className |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
4 |
|
private function validateEventHandlersInClass($className) |
77
|
|
|
{ |
78
|
4 |
|
$reflectionClass = new \ReflectionClass($className); |
79
|
|
|
|
80
|
4 |
|
if (!$this->classValidator->isClassAccepted($reflectionClass)) { |
81
|
1 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
85
|
|
|
|
86
|
3 |
|
if (!$this->isValidListenerMethod($reflectionMethod)) { |
87
|
1 |
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
$eventClass = $this->getMessageClassFromMethod($reflectionMethod); |
91
|
|
|
|
92
|
2 |
|
if ($eventClass) { |
93
|
|
|
|
94
|
2 |
|
$validMethodName = $this->getMethodNameFromEventClass($eventClass); |
95
|
|
|
|
96
|
2 |
|
if ($reflectionMethod->name != $validMethodName) { |
97
|
1 |
|
throw new \Exception("Method's name is invalid: {$reflectionMethod->name} for event $eventClass in\n" . |
98
|
1 |
|
"{$reflectionClass->getFileName()}:{$reflectionMethod->getStartLine()}\n" . |
99
|
2 |
|
"should be $validMethodName"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
3 |
|
private function getMessageClassFromMethod(\ReflectionMethod $reflectionMethod) |
106
|
|
|
{ |
107
|
3 |
|
$reflectionParameter = $reflectionMethod->getParameters()[0]; |
108
|
|
|
|
109
|
3 |
|
$typeHintedClass = $reflectionParameter->getClass(); |
110
|
|
|
|
111
|
3 |
|
if ($typeHintedClass) { |
112
|
2 |
|
return $typeHintedClass->getName(); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
throw new \Exception("Method parameter is not type hinted"); |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
private function isValidListenerMethod(\ReflectionMethod $reflectionMethod) |
119
|
|
|
{ |
120
|
3 |
|
if ($reflectionMethod->getNumberOfParameters() == 0) { |
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
return 0 === stripos($reflectionMethod->name, 'apply'); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
private function getMethodNameFromEventClass($className) |
128
|
|
|
{ |
129
|
2 |
|
$parts = explode('\\', $className); |
130
|
|
|
|
131
|
2 |
|
return 'apply' . end($parts); |
132
|
|
|
} |
133
|
|
|
} |