Skip to content

Commit

Permalink
All universalObjectCratesClasses allow dynamic properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 19, 2023
1 parent 808ab51 commit 2ec416e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ services:
implicitThrows: %exceptions.implicitThrows%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
detectDeadTypeInMultiCatch: %featureToggles.detectDeadTypeInMultiCatch%
universalObjectCratesClasses: %universalObjectCratesClasses%

-
class: PHPStan\Analyser\ConstantResolver
Expand Down Expand Up @@ -1916,6 +1917,7 @@ services:
class: PHPStan\Reflection\BetterReflection\BetterReflectionProvider
arguments:
reflector: @betterReflectionReflector
universalObjectCratesClasses: %universalObjectCratesClasses%
autowired: false

-
Expand All @@ -1931,6 +1933,8 @@ services:

-
implement: PHPStan\Reflection\BetterReflection\BetterReflectionProviderFactory
arguments:
universalObjectCratesClasses: %universalObjectCratesClasses%

-
class: PHPStan\Reflection\BetterReflection\SourceStubber\PhpStormStubsSourceStubberFactory
Expand Down
1 change: 1 addition & 0 deletions conf/config.stubValidator.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
class: PHPStan\Reflection\BetterReflection\BetterReflectionProvider
arguments:
reflector: @stubReflector
universalObjectCratesClasses: %universalObjectCratesClasses%
autowired: false

stubReflector:
Expand Down
3 changes: 3 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class NodeScopeResolver
/**
* @param string[][] $earlyTerminatingMethodCalls className(string) => methods(string[])
* @param array<int, string> $earlyTerminatingFunctionCalls
* @param string[] $universalObjectCratesClasses
*/
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
Expand All @@ -226,6 +227,7 @@ public function __construct(
private readonly bool $polluteScopeWithAlwaysIterableForeach,
private readonly array $earlyTerminatingMethodCalls,
private readonly array $earlyTerminatingFunctionCalls,
private readonly array $universalObjectCratesClasses,
private readonly bool $implicitThrows,
private readonly bool $treatPhpDocTypesAsCertain,
private readonly bool $detectDeadTypeInMultiCatch,
Expand Down Expand Up @@ -1707,6 +1709,7 @@ private function createAstClassReflection(Node\Stmt\ClassLike $stmt, string $cla
null,
null,
null,
$this->universalObjectCratesClasses,
sprintf('%s:%d', $scope->getFile(), $stmt->getStartLine()),
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class BetterReflectionProvider implements ReflectionProvider
/** @var array<string, GlobalConstantReflection> */
private array $cachedConstants = [];

/**
* @param string[] $universalObjectCratesClasses
*/
public function __construct(
private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
private InitializerExprTypeResolver $initializerExprTypeResolver,
Expand All @@ -84,6 +87,7 @@ public function __construct(
private FileHelper $fileHelper,
private PhpStormStubsSourceStubber $phpstormStubsSourceStubber,
private SignatureMapProvider $signatureMapProvider,
private array $universalObjectCratesClasses,
)
{
}
Expand Down Expand Up @@ -144,6 +148,7 @@ public function getClass(string $className): ClassReflection
null,
null,
$this->stubPhpDocProvider->findClassPhpDoc($reflectionClass->getName()),
$this->universalObjectCratesClasses,
);

$this->classReflections[$reflectionClassName] = $classReflection;
Expand Down Expand Up @@ -221,6 +226,7 @@ public function getAnonymousClassReflection(Node\Stmt\Class_ $classNode, Scope $
$scopeFile,
null,
$this->stubPhpDocProvider->findClassPhpDoc($className),
$this->universalObjectCratesClasses,
);
$this->classReflections[$className] = self::$anonymousClasses[$className];

Expand Down
12 changes: 10 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PHPStan\PhpDoc\Tag\TypeAliasTag;
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\Php\UniversalObjectCratesClassReflectionExtension;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\CircularTypeAliasDefinitionException;
Expand All @@ -45,7 +46,6 @@
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\VerbosityLevel;
use ReflectionException;
use stdClass;
use function array_diff;
use function array_filter;
use function array_key_exists;
Expand Down Expand Up @@ -131,6 +131,7 @@ class ClassReflection
* @param PropertiesClassReflectionExtension[] $propertiesClassReflectionExtensions
* @param MethodsClassReflectionExtension[] $methodsClassReflectionExtensions
* @param AllowedSubTypesClassReflectionExtension[] $allowedSubTypesClassReflectionExtensions
* @param string[] $universalObjectCratesClasses
*/
public function __construct(
private ReflectionProvider $reflectionProvider,
Expand All @@ -148,6 +149,7 @@ public function __construct(
private ?string $anonymousFilename,
private ?TemplateTypeMap $resolvedTemplateTypeMap,
private ?ResolvedPhpDocBlock $stubPhpDocBlock,
private array $universalObjectCratesClasses,
private ?string $extraCacheKey = null,
private ?TemplateTypeVarianceMap $resolvedCallSiteVarianceMap = null,
)
Expand Down Expand Up @@ -389,7 +391,11 @@ public function allowsDynamicProperties(): bool
return false;
}

if ($this->is(stdClass::class)) {
if (UniversalObjectCratesClassReflectionExtension::isUniversalObjectCrate(
$this->reflectionProvider,
$this->universalObjectCratesClasses,
$this,
)) {
return true;
}

Expand Down Expand Up @@ -1412,6 +1418,7 @@ public function withTypes(array $types): self
$this->anonymousFilename,
$this->typeMapFromList($types),
$this->stubPhpDocBlock,
$this->universalObjectCratesClasses,
null,
$this->resolvedCallSiteVarianceMap,
);
Expand All @@ -1438,6 +1445,7 @@ public function withVariances(array $variances): self
$this->anonymousFilename,
$this->resolvedTemplateTypeMap,
$this->stubPhpDocBlock,
$this->universalObjectCratesClasses,
null,
$this->varianceMapFromList($variances),
);
Expand Down
1 change: 1 addition & 0 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private function getAnalyser(): Analyser
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
[],
[],
self::getContainer()->getParameter('universalObjectCratesClasses'),
self::getContainer()->getParameter('exceptions')['implicitThrows'],
$this->shouldTreatPhpDocTypesAsCertain(),
self::getContainer()->getParameter('featureToggles')['detectDeadTypeInMultiCatch'],
Expand Down
1 change: 1 addition & 0 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function processFile(
true,
static::getEarlyTerminatingMethodCalls(),
static::getEarlyTerminatingFunctionCalls(),
self::getContainer()->getParameter('universalObjectCratesClasses'),
true,
self::getContainer()->getParameter('treatPhpDocTypesAsCertain'),
self::getContainer()->getParameter('featureToggles')['detectDeadTypeInMultiCatch'],
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\FileTypeMapper;
use stdClass;
use function array_map;
use function array_merge;
use function assert;
Expand Down Expand Up @@ -643,6 +644,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
true,
[],
[],
[stdClass::class],
true,
$this->shouldTreatPhpDocTypesAsCertain(),
self::getContainer()->getParameter('featureToggles')['detectDeadTypeInMultiCatch'],
Expand Down

0 comments on commit 2ec416e

Please sign in to comment.