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
|
28 |
|
public function __construct(string $directory, string ...$directories) |
32
|
|
|
{ |
33
|
28 |
|
$this->directories = [$directory, ...array_values($directories)]; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @psalm-param class-string ...$interfaces |
38
|
|
|
*/ |
39
|
22 |
|
public function withInterface(string ...$interfaces): self |
40
|
|
|
{ |
41
|
22 |
|
$new = clone $this; |
42
|
22 |
|
array_push($new->interfaces, ...array_values($interfaces)); |
43
|
|
|
|
44
|
22 |
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @psalm-param class-string $parentClass |
49
|
|
|
*/ |
50
|
2 |
|
public function withParentClass(string $parentClass): self |
51
|
|
|
{ |
52
|
2 |
|
$new = clone $this; |
53
|
2 |
|
$new->parentClass = $parentClass; |
54
|
2 |
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @psalm-param class-string ...$attributes |
59
|
|
|
*/ |
60
|
8 |
|
public function withAttribute(string ...$attributes): self |
61
|
|
|
{ |
62
|
8 |
|
$new = clone $this; |
63
|
8 |
|
array_push($new->attributes, ...array_values($attributes)); |
64
|
|
|
|
65
|
8 |
|
return $new; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @psalm-return iterable<class-string> |
70
|
|
|
*/ |
71
|
28 |
|
public function find(): iterable |
72
|
|
|
{ |
73
|
28 |
|
if (empty($this->interfaces) && empty($this->attributes) && $this->parentClass === null) { |
74
|
6 |
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
22 |
|
yield from $this->getAvailableClasses(); |
78
|
|
|
} |
79
|
|
|
|
80
|
22 |
|
protected function getFiles(): Finder |
81
|
|
|
{ |
82
|
22 |
|
return (new Finder()) |
83
|
22 |
|
->in($this->directories) |
84
|
22 |
|
->name('*.php') |
85
|
22 |
|
->sortByName() |
86
|
22 |
|
->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.