Skip to content

Commit baf359e

Browse files
committed
stdClass does not have __get method
1 parent 0ef3c87 commit baf359e

File tree

5 files changed

+42
-163
lines changed

5 files changed

+42
-163
lines changed

conf/config.neon

-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,6 @@ services:
720720
arguments:
721721
parser: @defaultAnalysisParser
722722
inferPrivatePropertyTypeFromConstructor: %inferPrivatePropertyTypeFromConstructor%
723-
universalObjectCratesClasses: %universalObjectCratesClasses%
724723

725724
-
726725
implement: PHPStan\Reflection\Php\PhpMethodReflectionFactory

src/Reflection/Php/FakeBuiltinMethodReflection.php

-128
This file was deleted.

src/Reflection/Php/PhpClassReflectionExtension.php

+7-34
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public function __construct(
9898
private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
9999
private FileTypeMapper $fileTypeMapper,
100100
private bool $inferPrivatePropertyTypeFromConstructor,
101-
private array $universalObjectCratesClasses,
102101
)
103102
{
104103
}
@@ -398,20 +397,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
398397

399398
public function hasNativeMethod(ClassReflection $classReflection, string $methodName): bool
400399
{
401-
$hasMethod = $this->hasMethod($classReflection, $methodName);
402-
if ($hasMethod) {
403-
return true;
404-
}
405-
406-
if ($methodName === '__get' && UniversalObjectCratesClassReflectionExtension::isUniversalObjectCrate(
407-
$this->reflectionProviderProvider->getReflectionProvider(),
408-
$this->universalObjectCratesClasses,
409-
$classReflection,
410-
)) {
411-
return true;
412-
}
413-
414-
return false;
400+
return $this->hasMethod($classReflection, $methodName);
415401
}
416402

417403
public function getNativeMethod(ClassReflection $classReflection, string $methodName): ExtendedMethodReflection
@@ -420,27 +406,14 @@ public function getNativeMethod(ClassReflection $classReflection, string $method
420406
return $this->nativeMethods[$classReflection->getCacheKey()][$methodName];
421407
}
422408

423-
if ($classReflection->getNativeReflection()->hasMethod($methodName)) {
424-
$nativeMethodReflection = new NativeBuiltinMethodReflection(
425-
$classReflection->getNativeReflection()->getMethod($methodName),
426-
);
427-
} else {
428-
if (
429-
$methodName !== '__get'
430-
|| !UniversalObjectCratesClassReflectionExtension::isUniversalObjectCrate(
431-
$this->reflectionProviderProvider->getReflectionProvider(),
432-
$this->universalObjectCratesClasses,
433-
$classReflection,
434-
)) {
435-
throw new ShouldNotHappenException();
436-
}
437-
438-
$nativeMethodReflection = new FakeBuiltinMethodReflection(
439-
$methodName,
440-
$classReflection->getNativeReflection(),
441-
);
409+
if (!$classReflection->getNativeReflection()->hasMethod($methodName)) {
410+
throw new ShouldNotHappenException();
442411
}
443412

413+
$nativeMethodReflection = new NativeBuiltinMethodReflection(
414+
$classReflection->getNativeReflection()->getMethod($methodName),
415+
);
416+
444417
if (!isset($this->nativeMethods[$classReflection->getCacheKey()][$nativeMethodReflection->getName()])) {
445418
$method = $this->createMethod($classReflection, $nativeMethodReflection, false);
446419
$this->nativeMethods[$classReflection->getCacheKey()][$nativeMethodReflection->getName()] = $method;

tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,21 @@ public function testBug9615(): void
712712
]);
713713
}
714714

715+
public function testBug10149(): void
716+
{
717+
$this->phpVersionId = PHP_VERSION_ID;
718+
$errors = [];
719+
if (PHP_VERSION_ID >= 80300) {
720+
$errors = [
721+
[
722+
'Method Bug10149\StdSat::__get() has #[\Override] attribute but does not override any method.',
723+
10,
724+
],
725+
];
726+
}
727+
$this->analyse([__DIR__ . '/data/bug-10149.php'], $errors);
728+
}
729+
715730
public function testTraits(): void
716731
{
717732
$errors = [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug10149;
4+
5+
trait WarnDynamicPropertyTrait
6+
{
7+
/**
8+
* @return mixed
9+
*/
10+
#[\Override]
11+
public function &__get(string $name)
12+
{
13+
return $this->{$name};
14+
}
15+
}
16+
17+
class StdSat extends \stdClass
18+
{
19+
use WarnDynamicPropertyTrait;
20+
}

0 commit comments

Comments
 (0)