5
5
use PHPStan \Reflection \ClassReflection ;
6
6
use PHPStan \ShouldNotHappenException ;
7
7
use PHPStan \Type \ObjectType ;
8
+ use Symfony \Component \Console \Application ;
8
9
use function file_exists ;
9
10
use function get_class ;
10
11
use function is_readable ;
11
12
12
13
final class ConsoleApplicationResolver
13
14
{
14
15
15
- /** @var \Symfony\Component\Console\Application|null */
16
+ /** @var string|null */
17
+ private $ consoleApplicationLoader ;
18
+
19
+ /** @var \Symfony\Component\Console\Application|false|null */
16
20
private $ consoleApplication ;
17
21
18
22
public function __construct (?string $ consoleApplicationLoader )
19
23
{
20
- if ($ consoleApplicationLoader === null ) {
21
- return ;
22
- }
23
- $ this ->consoleApplication = $ this ->loadConsoleApplication ($ consoleApplicationLoader );
24
+ $ this ->consoleApplicationLoader = $ consoleApplicationLoader ;
24
25
}
25
26
26
27
/**
@@ -38,12 +39,34 @@ private function loadConsoleApplication(string $consoleApplicationLoader)
38
39
return require $ consoleApplicationLoader ;
39
40
}
40
41
42
+ public function getConsoleApplication (): ?Application
43
+ {
44
+ if ($ this ->consoleApplication === false ) {
45
+ return null ;
46
+ }
47
+
48
+ if ($ this ->consoleApplication !== null ) {
49
+ return $ this ->consoleApplication ;
50
+ }
51
+
52
+ if ($ this ->consoleApplicationLoader === null ) {
53
+ $ this ->consoleApplication = false ;
54
+
55
+ return null ;
56
+ }
57
+
58
+ $ this ->consoleApplication = $ this ->loadConsoleApplication ($ this ->consoleApplicationLoader );
59
+
60
+ return $ this ->consoleApplication ;
61
+ }
62
+
41
63
/**
42
64
* @return \Symfony\Component\Console\Command\Command[]
43
65
*/
44
66
public function findCommands (ClassReflection $ classReflection ): array
45
67
{
46
- if ($ this ->consoleApplication === null ) {
68
+ $ consoleApplication = $ this ->getConsoleApplication ();
69
+ if ($ consoleApplication === null ) {
47
70
return [];
48
71
}
49
72
@@ -53,7 +76,7 @@ public function findCommands(ClassReflection $classReflection): array
53
76
}
54
77
55
78
$ commands = [];
56
- foreach ($ this -> consoleApplication ->all () as $ name => $ command ) {
79
+ foreach ($ consoleApplication ->all () as $ name => $ command ) {
57
80
if (!$ classType ->isSuperTypeOf (new ObjectType (get_class ($ command )))->yes ()) {
58
81
continue ;
59
82
}
0 commit comments