Skip to content

Commit

Permalink
stdClass does not have __get method
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 18, 2023
1 parent 0ef3c87 commit baf359e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 163 deletions.
1 change: 0 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,6 @@ services:
arguments:
parser: @defaultAnalysisParser
inferPrivatePropertyTypeFromConstructor: %inferPrivatePropertyTypeFromConstructor%
universalObjectCratesClasses: %universalObjectCratesClasses%

-
implement: PHPStan\Reflection\Php\PhpMethodReflectionFactory
Expand Down
128 changes: 0 additions & 128 deletions src/Reflection/Php/FakeBuiltinMethodReflection.php

This file was deleted.

41 changes: 7 additions & 34 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function __construct(
private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
private FileTypeMapper $fileTypeMapper,
private bool $inferPrivatePropertyTypeFromConstructor,

Check failure on line 100 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

PHPDoc tag @param references unknown parameter: $universalObjectCratesClasses

Check failure on line 100 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

PHPDoc tag @param references unknown parameter: $universalObjectCratesClasses
private array $universalObjectCratesClasses,
)
{
}
Expand Down Expand Up @@ -398,20 +397,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName):

public function hasNativeMethod(ClassReflection $classReflection, string $methodName): bool
{
$hasMethod = $this->hasMethod($classReflection, $methodName);
if ($hasMethod) {
return true;
}

if ($methodName === '__get' && UniversalObjectCratesClassReflectionExtension::isUniversalObjectCrate(
$this->reflectionProviderProvider->getReflectionProvider(),
$this->universalObjectCratesClasses,
$classReflection,
)) {
return true;
}

return false;
return $this->hasMethod($classReflection, $methodName);
}

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

if ($classReflection->getNativeReflection()->hasMethod($methodName)) {
$nativeMethodReflection = new NativeBuiltinMethodReflection(
$classReflection->getNativeReflection()->getMethod($methodName),
);
} else {
if (
$methodName !== '__get'
|| !UniversalObjectCratesClassReflectionExtension::isUniversalObjectCrate(
$this->reflectionProviderProvider->getReflectionProvider(),
$this->universalObjectCratesClasses,
$classReflection,
)) {
throw new ShouldNotHappenException();
}

$nativeMethodReflection = new FakeBuiltinMethodReflection(
$methodName,
$classReflection->getNativeReflection(),
);
if (!$classReflection->getNativeReflection()->hasMethod($methodName)) {
throw new ShouldNotHappenException();
}

$nativeMethodReflection = new NativeBuiltinMethodReflection(
$classReflection->getNativeReflection()->getMethod($methodName),
);

if (!isset($this->nativeMethods[$classReflection->getCacheKey()][$nativeMethodReflection->getName()])) {
$method = $this->createMethod($classReflection, $nativeMethodReflection, false);
$this->nativeMethods[$classReflection->getCacheKey()][$nativeMethodReflection->getName()] = $method;
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,21 @@ public function testBug9615(): void
]);
}

public function testBug10149(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$errors = [];
if (PHP_VERSION_ID >= 80300) {
$errors = [
[
'Method Bug10149\StdSat::__get() has #[\Override] attribute but does not override any method.',
10,
],
];
}
$this->analyse([__DIR__ . '/data/bug-10149.php'], $errors);
}

public function testTraits(): void
{
$errors = [];
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10149.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug10149;

trait WarnDynamicPropertyTrait
{
/**
* @return mixed
*/
#[\Override]
public function &__get(string $name)
{
return $this->{$name};
}
}

class StdSat extends \stdClass
{
use WarnDynamicPropertyTrait;
}

0 comments on commit baf359e

Please sign in to comment.