55use PHPStan \Reflection \ClassReflection ;
66use PHPStan \ShouldNotHappenException ;
77use PHPStan \Type \ObjectType ;
8+ use Symfony \Component \Console \Application ;
89use function file_exists ;
910use function get_class ;
1011use function is_readable ;
1112
1213final class ConsoleApplicationResolver
1314{
1415
15- /** @var \Symfony\Component\Console\Application|null */
16+ /** @var string|null */
17+ private $ consoleApplicationLoader ;
18+
19+ /** @var \Symfony\Component\Console\Application|false|null */
1620private $ consoleApplication ;
1721
1822public function __construct (?string $ consoleApplicationLoader )
1923{
20- if ($ consoleApplicationLoader === null ) {
21- return ;
22- }
23- $ this ->consoleApplication = $ this ->loadConsoleApplication ($ consoleApplicationLoader );
24+ $ this ->consoleApplicationLoader = $ consoleApplicationLoader ;
2425}
2526
2627/**
@@ -38,12 +39,34 @@ private function loadConsoleApplication(string $consoleApplicationLoader)
3839return require $ consoleApplicationLoader ;
3940}
4041
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+
4163/**
4264 * @return \Symfony\Component\Console\Command\Command[]
4365 */
4466public function findCommands (ClassReflection $ classReflection ): array
4567{
46- if ($ this ->consoleApplication === null ) {
68+ $ consoleApplication = $ this ->getConsoleApplication ();
69+ if ($ consoleApplication === null ) {
4770return [];
4871}
4972
@@ -53,7 +76,7 @@ public function findCommands(ClassReflection $classReflection): array
5376}
5477
5578$ commands = [];
56- foreach ($ this -> consoleApplication ->all () as $ name => $ command ) {
79+ foreach ($ consoleApplication ->all () as $ name => $ command ) {
5780if (!$ classType ->isSuperTypeOf (new ObjectType (get_class ($ command )))->yes ()) {
5881continue ;
5982}
0 commit comments