5
5
use PhpParser \Node ;
6
6
use PhpParser \Node \Expr \New_ ;
7
7
use PHPStan \Analyser \Scope ;
8
+ use PHPStan \DependencyInjection \Container ;
8
9
use PHPStan \Internal \SprintfHelper ;
9
10
use PHPStan \Reflection \ClassReflection ;
10
11
use PHPStan \Reflection \ParametersAcceptorSelector ;
15
16
use PHPStan \Rules \ClassNameUsageLocation ;
16
17
use PHPStan \Rules \FunctionCallParametersCheck ;
17
18
use PHPStan \Rules \IdentifierRuleError ;
19
+ use PHPStan \Rules \RestrictedUsage \RestrictedMethodUsageExtension ;
20
+ use PHPStan \Rules \RestrictedUsage \RewrittenDeclaringClassMethodReflection ;
18
21
use PHPStan \Rules \Rule ;
19
22
use PHPStan \Rules \RuleErrorBuilder ;
20
23
use PHPStan \ShouldNotHappenException ;
@@ -33,6 +36,7 @@ final class InstantiationRule implements Rule
33
36
{
34
37
35
38
public function __construct (
39
+ private Container $ container ,
36
40
private ReflectionProvider $ reflectionProvider ,
37
41
private FunctionCallParametersCheck $ check ,
38
42
private ClassNameCheck $ classCheck ,
@@ -197,6 +201,28 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
197
201
->build ();
198
202
}
199
203
204
+ /** @var RestrictedMethodUsageExtension[] $restrictedUsageExtensions */
205
+ $ restrictedUsageExtensions = $ this ->container ->getServicesByTag (RestrictedMethodUsageExtension::METHOD_EXTENSION_TAG );
206
+
207
+ foreach ($ restrictedUsageExtensions as $ extension ) {
208
+ $ restrictedUsage = $ extension ->isRestrictedMethodUsage ($ constructorReflection , $ scope );
209
+ if ($ restrictedUsage === null ) {
210
+ continue ;
211
+ }
212
+
213
+ if ($ classReflection ->getName () !== $ constructorReflection ->getDeclaringClass ()->getName ()) {
214
+ $ rewrittenConstructorReflection = new RewrittenDeclaringClassMethodReflection ($ classReflection , $ constructorReflection );
215
+ $ rewrittenRestrictedUsage = $ extension ->isRestrictedMethodUsage ($ rewrittenConstructorReflection , $ scope );
216
+ if ($ rewrittenRestrictedUsage === null ) {
217
+ continue ;
218
+ }
219
+ }
220
+
221
+ $ messages [] = RuleErrorBuilder::message ($ restrictedUsage ->errorMessage )
222
+ ->identifier ($ restrictedUsage ->identifier )
223
+ ->build ();
224
+ }
225
+
200
226
$ classDisplayName = SprintfHelper::escapeFormatString ($ classReflection ->getDisplayName ());
201
227
202
228
return array_merge ($ messages , $ this ->check ->check (
0 commit comments