22
33namespace PHPStan \Reflection ;
44
5+ use PHPStan \Php \PhpVersion ;
56use PHPStan \PhpDoc \ResolvedPhpDocBlock ;
67use PHPStan \PhpDoc \Tag \ExtendsTag ;
78use PHPStan \PhpDoc \Tag \ImplementsTag ;
2021use PHPStan \Type \Generic \TemplateTypeScope ;
2122use PHPStan \Type \Type ;
2223use PHPStan \Type \VerbosityLevel ;
24+ use ReflectionMethod ;
2325
2426class ClassReflection implements ReflectionWithFilename
2527{
@@ -28,6 +30,8 @@ class ClassReflection implements ReflectionWithFilename
2830
2931private \PHPStan \Type \FileTypeMapper $ fileTypeMapper ;
3032
33+ private PhpVersion $ phpVersion ;
34+
3135/** @var \PHPStan\Reflection\PropertiesClassReflectionExtension[] */
3236private array $ propertiesClassReflectionExtensions ;
3337
@@ -97,6 +101,7 @@ class ClassReflection implements ReflectionWithFilename
97101public function __construct (
98102ReflectionProvider $ reflectionProvider ,
99103FileTypeMapper $ fileTypeMapper ,
104+ PhpVersion $ phpVersion ,
100105array $ propertiesClassReflectionExtensions ,
101106array $ methodsClassReflectionExtensions ,
102107string $ displayName ,
@@ -109,6 +114,7 @@ public function __construct(
109114{
110115$ this ->reflectionProvider = $ reflectionProvider ;
111116$ this ->fileTypeMapper = $ fileTypeMapper ;
117+ $ this ->phpVersion = $ phpVersion ;
112118$ this ->propertiesClassReflectionExtensions = $ propertiesClassReflectionExtensions ;
113119$ this ->methodsClassReflectionExtensions = $ methodsClassReflectionExtensions ;
114120$ this ->displayName = $ displayName ;
@@ -406,18 +412,36 @@ public function getNativeMethods(): array
406412
407413public function hasConstructor (): bool
408414{
409- return $ this ->reflection -> getConstructor () !== null ;
415+ return $ this ->findConstructor () !== null ;
410416}
411417
412418public function getConstructor (): MethodReflection
413419{
414- $ constructor = $ this ->reflection -> getConstructor ();
420+ $ constructor = $ this ->findConstructor ();
415421if ($ constructor === null ) {
416422throw new \PHPStan \ShouldNotHappenException ();
417423}
418424return $ this ->getNativeMethod ($ constructor ->getName ());
419425}
420426
427+ private function findConstructor (): ?ReflectionMethod
428+ {
429+ $ constructor = $ this ->reflection ->getConstructor ();
430+ if ($ constructor === null ) {
431+ return null ;
432+ }
433+
434+ if ($ this ->phpVersion ->supportsLegacyConstructor ()) {
435+ return $ constructor ;
436+ }
437+
438+ if (strtolower ($ constructor ->getName ()) !== '__construct ' ) {
439+ return null ;
440+ }
441+
442+ return $ constructor ;
443+ }
444+
421445private function getPhpExtension (): PhpClassReflectionExtension
422446{
423447$ extension = $ this ->methodsClassReflectionExtensions [0 ];
@@ -839,6 +863,7 @@ public function withTypes(array $types): self
839863return new self (
840864$ this ->reflectionProvider ,
841865$ this ->fileTypeMapper ,
866+ $ this ->phpVersion ,
842867$ this ->propertiesClassReflectionExtensions ,
843868$ this ->methodsClassReflectionExtensions ,
844869$ this ->displayName ,
0 commit comments