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) { |
|
|
|
|
63
|
4 |
|
$this->validateEventHandlersInClass($fqn); |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
protected function readFile($fullFilePath) |
68
|
|
|
{ |
69
|
|
|
return file_get_contents($fullFilePath); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
protected function filterFiles(array $files) |
73
|
|
|
{ |
74
|
4 |
|
return array_filter($files, [$this, 'isListenerFileName']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $className |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
4 |
|
private function validateEventHandlersInClass($className) |
82
|
|
|
{ |
83
|
4 |
|
$reflectionClass = new \ReflectionClass($className); |
84
|
|
|
|
85
|
4 |
|
if (!$this->classValidator->isClassAccepted($reflectionClass)) { |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
90
|
|
|
|
91
|
3 |
|
if (!$this->isValidListenerMethod($reflectionMethod)) { |
92
|
1 |
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
$eventClass = $this->getMessageClassFromMethod($reflectionMethod); |
96
|
|
|
|
97
|
2 |
|
if ($eventClass) { |
98
|
|
|
|
99
|
2 |
|
$validMethodName = $this->getMethodNameFromEventClass($eventClass); |
100
|
|
|
|
101
|
2 |
|
if ($reflectionMethod->name != $validMethodName) { |
102
|
1 |
|
throw new \Exception("Method's name is invalid: {$reflectionMethod->name} for event $eventClass in\n" . |
103
|
1 |
|
"{$reflectionClass->getFileName()}:{$reflectionMethod->getStartLine()}\n" . |
104
|
2 |
|
"should be $validMethodName"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
3 |
|
private function getMessageClassFromMethod(\ReflectionMethod $reflectionMethod) |
111
|
|
|
{ |
112
|
3 |
|
$reflectionParameter = $reflectionMethod->getParameters()[0]; |
113
|
|
|
|
114
|
3 |
|
$typeHintedClass = $reflectionParameter->getClass(); |
115
|
|
|
|
116
|
3 |
|
if ($typeHintedClass) { |
117
|
2 |
|
return $typeHintedClass->getName(); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
throw new \Exception("Method parameter is not type hinted"); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
private function isValidListenerMethod(\ReflectionMethod $reflectionMethod) |
124
|
|
|
{ |
125
|
3 |
|
if ($reflectionMethod->getNumberOfParameters() == 0) { |
126
|
1 |
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
return 0 === stripos($reflectionMethod->name, 'apply'); |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
private function getMethodNameFromEventClass($className) |
133
|
|
|
{ |
134
|
2 |
|
$parts = explode('\\', $className); |
135
|
|
|
|
136
|
2 |
|
return 'apply' . end($parts); |
137
|
|
|
} |
138
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: