forked from ariddlestone/phpstan-cakephp2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassReflectionFinder.php
More file actions
80 lines (72 loc) · 2.08 KB
/
ClassReflectionFinder.php
File metadata and controls
80 lines (72 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace PHPStanCakePHP2;
use Exception;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
/**
* Finds class reflections for all classes at specified glob paths, optionally
* restricted to children of certain classes.
*/
final class ClassReflectionFinder
{
private ReflectionProvider $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
/**
* @param array<string> $paths
*
* @return array<ClassReflection>
*
* @throws Exception
*/
public function getClassReflections(
array $paths,
string $isA = 'stdClass',
?callable $pathToClassName = null
): array {
$classReflections = array_map(
[$this->reflectionProvider, 'getClass'],
$this->getClassNamesFromPaths($paths, $pathToClassName)
);
return array_filter(
$classReflections,
static function (
ClassReflection $classReflection
) use ($isA) {
return $classReflection->is($isA);
}
);
}
/**
* @param array<string> $paths
*
* @return array<string>
*
* @throws Exception
*/
private function getClassNamesFromPaths(
array $paths,
?callable $pathToClassName
): array {
$classPaths = [];
foreach ($paths as $path) {
$filePaths = glob($path);
if (! is_array($filePaths)) {
throw new Exception(sprintf('glob(%s) caused an error', $path));
}
$classPaths = array_merge($classPaths, $filePaths);
}
$classNames = array_map($pathToClassName ?? [$this, 'getClassNameFromFileName'], $classPaths);
return array_filter(
$classNames,
[$this->reflectionProvider, 'hasClass']
);
}
private function getClassNameFromFileName(string $fileName): string
{
return basename($fileName, '.php');
}
}