|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Classifier; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base implementation for {@see ClassifierInterface} with common filters. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractClassifier implements ClassifierInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected array $interfaces = []; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected array $attributes = []; |
|
22
|
|
|
/** |
|
23
|
|
|
* @psalm-var class-string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected ?string $parentClass = null; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected array $directories; |
|
30
|
|
|
|
|
31
|
14 |
|
public function __construct(string $directory, string ...$directories) |
|
32
|
|
|
{ |
|
33
|
14 |
|
$this->directories = [$directory, ...array_values($directories)]; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @psalm-param class-string ...$interfaces |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function withInterface(string ...$interfaces): self |
|
40
|
|
|
{ |
|
41
|
11 |
|
$new = clone $this; |
|
42
|
11 |
|
array_push($new->interfaces, ...array_values($interfaces)); |
|
43
|
|
|
|
|
44
|
11 |
|
return $new; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @psalm-param class-string $parentClass |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function withParentClass(string $parentClass): self |
|
51
|
|
|
{ |
|
52
|
1 |
|
$new = clone $this; |
|
53
|
1 |
|
$new->parentClass = $parentClass; |
|
54
|
1 |
|
return $new; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @psalm-param class-string ...$attributes |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function withAttribute(string ...$attributes): self |
|
61
|
|
|
{ |
|
62
|
4 |
|
$new = clone $this; |
|
63
|
4 |
|
array_push($new->attributes, ...array_values($attributes)); |
|
64
|
|
|
|
|
65
|
4 |
|
return $new; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @psalm-return iterable<class-string> |
|
70
|
|
|
*/ |
|
71
|
14 |
|
public function find(): iterable |
|
72
|
|
|
{ |
|
73
|
14 |
|
if (empty($this->interfaces) && empty($this->attributes) && $this->parentClass === null) { |
|
74
|
3 |
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
11 |
|
yield from $this->getAvailableClasses(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
11 |
|
protected function getFiles(): Finder |
|
81
|
|
|
{ |
|
82
|
11 |
|
return (new Finder()) |
|
83
|
11 |
|
->in($this->directories) |
|
84
|
11 |
|
->name('*.php') |
|
85
|
11 |
|
->sortByName() |
|
86
|
11 |
|
->files(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return iterable<class-string> |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract protected function getAvailableClasses(): iterable; |
|
93
|
|
|
} |
|
94
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.