1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Copyright (c) 2016 Constantin Galbenu <[email protected]> * |
4
|
|
|
******************************************************************************/ |
5
|
|
|
|
6
|
|
|
namespace Gica\CodeAnalysis; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter; |
10
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator; |
11
|
|
|
use Gica\CodeAnalysis\Traits\FilesInDirectoryExtracter; |
12
|
|
|
|
13
|
|
|
class ClassDiscovery |
14
|
|
|
{ |
15
|
|
|
use FilesInDirectoryExtracter; |
16
|
|
|
|
17
|
|
|
protected $discoveredClasses = []; |
18
|
|
|
|
19
|
|
|
/** @var ListenerClassValidator */ |
20
|
|
|
private $classValidator; |
21
|
|
|
/** |
22
|
|
|
* @var ClassSorter |
23
|
|
|
*/ |
24
|
|
|
private $classSorter; |
25
|
|
|
/** |
26
|
|
|
* @var PhpClassInFileInspector |
27
|
|
|
*/ |
28
|
|
|
private $phpClassInFileInspector; |
29
|
|
|
|
30
|
1 |
View Code Duplication |
public function __construct( |
|
|
|
|
31
|
|
|
ListenerClassValidator $classValidator, |
32
|
|
|
ClassSorter $classSorter, |
33
|
|
|
PhpClassInFileInspector $phpClassInFileInspector = null |
34
|
|
|
) |
35
|
|
|
{ |
36
|
1 |
|
$this->classValidator = $classValidator; |
37
|
1 |
|
$this->classSorter = $classSorter; |
38
|
1 |
|
$this->phpClassInFileInspector = $phpClassInFileInspector ?? new PhpClassInFileInspector; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
1 |
|
public function discoverListeners($directory) |
43
|
|
|
{ |
44
|
1 |
|
$files = $this->getFilesInDirectory($directory); |
45
|
|
|
|
46
|
1 |
|
$files = $this->filterFiles($files); |
47
|
|
|
|
48
|
1 |
|
foreach ($files as $file) { |
49
|
1 |
|
$fullFilePath = $file; |
50
|
|
|
|
51
|
1 |
|
$extractedClass = $this->extractClassFromFileIfAccepted($fullFilePath); |
52
|
|
|
|
53
|
1 |
|
if ($extractedClass) { |
54
|
1 |
|
$this->discoveredClasses[] = $extractedClass; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$this->discoveredClasses = $this->sort($this->discoveredClasses); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $filePath |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
1 |
|
protected function isListenerFileName($filePath) |
66
|
|
|
{ |
67
|
1 |
|
return preg_match('#\.php$#ims', $filePath); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $fullFilePath |
72
|
|
|
* @return bool|\ReflectionClass |
73
|
|
|
*/ |
74
|
1 |
|
protected function extractClassFromFileIfAccepted($fullFilePath) |
75
|
|
|
{ |
76
|
1 |
|
$fqn = $this->phpClassInFileInspector->getFullyQualifiedClassName($fullFilePath); |
77
|
|
|
|
78
|
1 |
|
if (!$fqn) { |
|
|
|
|
79
|
1 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $this->getClassIfAccepted($fqn); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function readFile($fullFilePath) |
86
|
|
|
{ |
87
|
|
|
return file_get_contents($fullFilePath); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \ReflectionClass[] |
92
|
|
|
*/ |
93
|
1 |
|
public function getDiscoveredClasses() |
94
|
|
|
{ |
95
|
1 |
|
return $this->discoveredClasses; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function filterFiles(array $files) |
99
|
|
|
{ |
100
|
1 |
|
return array_filter($files, function ($file) { |
101
|
1 |
|
return $this->isListenerFileName($file); |
102
|
1 |
|
}); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $className |
107
|
|
|
* @return \ReflectionClass|null |
108
|
|
|
*/ |
109
|
1 |
|
private function getClassIfAccepted($className) |
110
|
|
|
{ |
111
|
1 |
|
$reflectionClass = new \ReflectionClass($className); |
112
|
|
|
|
113
|
1 |
|
if ($this->classValidator->isClassAccepted($reflectionClass)) { |
114
|
1 |
|
return $reflectionClass; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \ReflectionClass[] $discoveredClasses |
122
|
|
|
* @return \ReflectionClass[] |
123
|
|
|
*/ |
124
|
1 |
|
private function sort($discoveredClasses) |
125
|
|
|
{ |
126
|
1 |
|
usort($discoveredClasses, $this->classSorter); |
127
|
|
|
|
128
|
1 |
|
return $discoveredClasses; |
129
|
|
|
} |
130
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.