|
24 | 24 | use PHPStan\PhpDocParser\Parser\TypeParser; |
25 | 25 | use PHPStan\PhpDocParser\ParserConfig; |
26 | 26 | use PHPStan\Reflection\InitializerExprTypeResolver; |
| 27 | +use PHPStan\Reflection\MissingStaticAccessorInstanceException; |
27 | 28 | use PHPStan\Reflection\PhpVersionStaticAccessor; |
28 | 29 | use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider; |
29 | 30 | use PHPStan\Reflection\ReflectionProvider\DummyReflectionProvider; |
@@ -66,120 +67,143 @@ public function loadConfiguration(): void |
66 | 67 | $parser = Llk::load(new Read(__DIR__ . '/../../resources/RegexGrammar.pp')); |
67 | 68 | $reflectionProvider = new DummyReflectionProvider(); |
68 | 69 | $reflectionProviderProvider = new DirectReflectionProviderProvider($reflectionProvider); |
| 70 | + |
| 71 | + try { |
| 72 | + $originalReflectionProvider = ReflectionProviderStaticAccessor::getInstance(); |
| 73 | + } catch (MissingStaticAccessorInstanceException) { |
| 74 | + $originalReflectionProvider = null; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + $originalPhpVersion = PhpVersionStaticAccessor::getInstance(); |
| 79 | + } catch (MissingStaticAccessorInstanceException) { |
| 80 | + $originalPhpVersion = null; |
| 81 | + } |
| 82 | + |
69 | 83 | ReflectionProviderStaticAccessor::registerInstance($reflectionProvider); |
70 | 84 | PhpVersionStaticAccessor::registerInstance(new PhpVersion(PHP_VERSION_ID)); |
71 | | - $composerPhpVersionFactory = new ComposerPhpVersionFactory([]); |
72 | | - $constantResolver = new ConstantResolver($reflectionProviderProvider, [], null, $composerPhpVersionFactory, null); |
73 | | - |
74 | | - $phpDocParserConfig = new ParserConfig([]); |
75 | | - $ignoredRegexValidator = new IgnoredRegexValidator( |
76 | | - $parser, |
77 | | - new TypeStringResolver( |
78 | | - new Lexer($phpDocParserConfig), |
79 | | - new TypeParser($phpDocParserConfig, new ConstExprParser($phpDocParserConfig)), |
80 | | - new TypeNodeResolver( |
81 | | - new DirectTypeNodeResolverExtensionRegistryProvider( |
82 | | - new class implements TypeNodeResolverExtensionRegistry { |
83 | | - |
84 | | - public function getExtensions(): array |
| 85 | + |
| 86 | + try { |
| 87 | + $composerPhpVersionFactory = new ComposerPhpVersionFactory([]); |
| 88 | + $constantResolver = new ConstantResolver($reflectionProviderProvider, [], null, $composerPhpVersionFactory, null); |
| 89 | + |
| 90 | + $phpDocParserConfig = new ParserConfig([]); |
| 91 | + $ignoredRegexValidator = new IgnoredRegexValidator( |
| 92 | + $parser, |
| 93 | + new TypeStringResolver( |
| 94 | + new Lexer($phpDocParserConfig), |
| 95 | + new TypeParser($phpDocParserConfig, new ConstExprParser($phpDocParserConfig)), |
| 96 | + new TypeNodeResolver( |
| 97 | + new DirectTypeNodeResolverExtensionRegistryProvider( |
| 98 | + new class implements TypeNodeResolverExtensionRegistry { |
| 99 | + |
| 100 | + public function getExtensions(): array |
| 101 | + { |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + }, |
| 106 | + ), |
| 107 | + $reflectionProviderProvider, |
| 108 | + new DirectTypeAliasResolverProvider(new class implements TypeAliasResolver { |
| 109 | + |
| 110 | + public function hasTypeAlias(string $aliasName, ?string $classNameScope): bool |
85 | 111 | { |
86 | | - return []; |
| 112 | + return false; |
87 | 113 | } |
88 | 114 |
|
89 | | - }, |
90 | | - ), |
91 | | - $reflectionProviderProvider, |
92 | | - new DirectTypeAliasResolverProvider(new class implements TypeAliasResolver { |
93 | | - |
94 | | - public function hasTypeAlias(string $aliasName, ?string $classNameScope): bool |
95 | | - { |
96 | | - return false; |
97 | | - } |
98 | | - |
99 | | - public function resolveTypeAlias(string $aliasName, NameScope $nameScope): ?Type |
100 | | - { |
101 | | - return null; |
102 | | - } |
| 115 | + public function resolveTypeAlias(string $aliasName, NameScope $nameScope): ?Type |
| 116 | + { |
| 117 | + return null; |
| 118 | + } |
103 | 119 |
|
104 | | - }), |
105 | | - $constantResolver, |
106 | | - new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), new class implements OperatorTypeSpecifyingExtensionRegistryProvider { |
| 120 | + }), |
| 121 | + $constantResolver, |
| 122 | + new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), new class implements OperatorTypeSpecifyingExtensionRegistryProvider { |
107 | 123 |
|
108 | | - public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry |
109 | | - { |
110 | | - return new OperatorTypeSpecifyingExtensionRegistry([]); |
111 | | - } |
| 124 | + public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry |
| 125 | + { |
| 126 | + return new OperatorTypeSpecifyingExtensionRegistry([]); |
| 127 | + } |
112 | 128 |
|
113 | | - }, new OversizedArrayBuilder(), true), |
| 129 | + }, new OversizedArrayBuilder(), true), |
| 130 | + ), |
114 | 131 | ), |
115 | | - ), |
116 | | - ); |
| 132 | + ); |
117 | 133 |
|
118 | | - $errors = []; |
119 | | - foreach ($ignoreErrors as $ignoreError) { |
120 | | - if (is_array($ignoreError)) { |
121 | | - if (isset($ignoreError['count'])) { |
122 | | - continue; // ignoreError coming from baseline will be correct |
123 | | - } |
124 | | - if (isset($ignoreError['messages'])) { |
125 | | - $ignoreMessages = $ignoreError['messages']; |
126 | | - } elseif (isset($ignoreError['message'])) { |
127 | | - $ignoreMessages = [$ignoreError['message']]; |
| 134 | + $errors = []; |
| 135 | + foreach ($ignoreErrors as $ignoreError) { |
| 136 | + if (is_array($ignoreError)) { |
| 137 | + if (isset($ignoreError['count'])) { |
| 138 | + continue; // ignoreError coming from baseline will be correct |
| 139 | + } |
| 140 | + if (isset($ignoreError['messages'])) { |
| 141 | + $ignoreMessages = $ignoreError['messages']; |
| 142 | + } elseif (isset($ignoreError['message'])) { |
| 143 | + $ignoreMessages = [$ignoreError['message']]; |
| 144 | + } else { |
| 145 | + continue; |
| 146 | + } |
128 | 147 | } else { |
129 | | - continue; |
| 148 | + $ignoreMessages = [$ignoreError]; |
130 | 149 | } |
131 | | - } else { |
132 | | - $ignoreMessages = [$ignoreError]; |
133 | | - } |
134 | 150 |
|
135 | | - foreach ($ignoreMessages as $ignoreMessage) { |
136 | | - $error = $this->validateMessage($ignoredRegexValidator, $ignoreMessage); |
137 | | - if ($error === null) { |
138 | | - continue; |
| 151 | + foreach ($ignoreMessages as $ignoreMessage) { |
| 152 | + $error = $this->validateMessage($ignoredRegexValidator, $ignoreMessage); |
| 153 | + if ($error === null) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + $errors[] = $error; |
139 | 157 | } |
140 | | - $errors[] = $error; |
141 | 158 | } |
142 | | - } |
143 | 159 |
|
144 | | - $reportUnmatched = (bool) $builder->parameters['reportUnmatchedIgnoredErrors']; |
| 160 | + $reportUnmatched = (bool) $builder->parameters['reportUnmatchedIgnoredErrors']; |
145 | 161 |
|
146 | | - if ($reportUnmatched) { |
147 | | - foreach ($ignoreErrors as $ignoreError) { |
148 | | - if (!is_array($ignoreError)) { |
149 | | - continue; |
150 | | - } |
| 162 | + if ($reportUnmatched) { |
| 163 | + foreach ($ignoreErrors as $ignoreError) { |
| 164 | + if (!is_array($ignoreError)) { |
| 165 | + continue; |
| 166 | + } |
151 | 167 |
|
152 | | - if (isset($ignoreError['path'])) { |
153 | | - $ignorePaths = [$ignoreError['path']]; |
154 | | - } elseif (isset($ignoreError['paths'])) { |
155 | | - $ignorePaths = $ignoreError['paths']; |
156 | | - } else { |
157 | | - continue; |
158 | | - } |
| 168 | + if (isset($ignoreError['path'])) { |
| 169 | + $ignorePaths = [$ignoreError['path']]; |
| 170 | + } elseif (isset($ignoreError['paths'])) { |
| 171 | + $ignorePaths = $ignoreError['paths']; |
| 172 | + } else { |
| 173 | + continue; |
| 174 | + } |
159 | 175 |
|
160 | | - foreach ($ignorePaths as $ignorePath) { |
161 | | - if (FileExcluder::isAbsolutePath($ignorePath)) { |
162 | | - if (is_dir($ignorePath)) { |
163 | | - continue; |
| 176 | + foreach ($ignorePaths as $ignorePath) { |
| 177 | + if (FileExcluder::isAbsolutePath($ignorePath)) { |
| 178 | + if (is_dir($ignorePath)) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + if (is_file($ignorePath)) { |
| 182 | + continue; |
| 183 | + } |
164 | 184 | } |
165 | | - if (is_file($ignorePath)) { |
| 185 | + if (FileExcluder::isFnmatchPattern($ignorePath)) { |
166 | 186 | continue; |
167 | 187 | } |
168 | | - } |
169 | | - if (FileExcluder::isFnmatchPattern($ignorePath)) { |
170 | | - continue; |
171 | | - } |
172 | 188 |
|
173 | | - $errors[] = sprintf('Path "%s" is neither a directory, nor a file path, nor a fnmatch pattern.', $ignorePath); |
| 189 | + $errors[] = sprintf('Path "%s" is neither a directory, nor a file path, nor a fnmatch pattern.', $ignorePath); |
| 190 | + } |
174 | 191 | } |
175 | 192 | } |
176 | | - } |
177 | 193 |
|
178 | | - if (count($errors) === 0) { |
179 | | - return; |
180 | | - } |
| 194 | + if (count($errors) === 0) { |
| 195 | + return; |
| 196 | + } |
181 | 197 |
|
182 | | - throw new InvalidIgnoredErrorPatternsException($errors); |
| 198 | + throw new InvalidIgnoredErrorPatternsException($errors); |
| 199 | + } finally { |
| 200 | + if ($originalReflectionProvider !== null) { |
| 201 | + ReflectionProviderStaticAccessor::registerInstance($originalReflectionProvider); |
| 202 | + } |
| 203 | + if ($originalPhpVersion !== null) { |
| 204 | + PhpVersionStaticAccessor::registerInstance($originalPhpVersion); |
| 205 | + } |
| 206 | + } |
183 | 207 | } |
184 | 208 |
|
185 | 209 | private function validateMessage(IgnoredRegexValidator $ignoredRegexValidator, string $ignoreMessage): ?string |
|
0 commit comments