Skip to content

Commit

Permalink
[DX] Use type safe access with PrivatePropertyAccessor (#1660)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <m.staab@complex-it.de>
  • Loading branch information
TomasVotruba and clxmstaab committed Jan 11, 2022
1 parent fb76afb commit bbe0567
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
40 changes: 20 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@
"symfony/process": "^6.0",
"symfony/string": "^6.0",
"symfony/yaml": "^5.4|^6.0",
"symplify/astral": "^10.0.2",
"symplify/autowire-array-parameter": "^10.0.2",
"symplify/composer-json-manipulator": "^10.0.2",
"symplify/console-color-diff": "^10.0.2",
"symplify/easy-parallel": "^10.0.2",
"symplify/package-builder": "^10.0.2",
"symplify/rule-doc-generator-contracts": "^10.0.2",
"symplify/simple-php-doc-parser": "^10.0.2",
"symplify/skipper": "^10.0.2",
"symplify/smart-file-system": "^10.0.2",
"symplify/symfony-php-config": "^10.0.2",
"symplify/vendor-patches": "^10.0.2",
"symplify/astral": "^10.0.9",
"symplify/autowire-array-parameter": "^10.0.9",
"symplify/composer-json-manipulator": "^10.0.9",
"symplify/console-color-diff": "^10.0.9",
"symplify/easy-parallel": "^10.0.9",
"symplify/package-builder": "^10.0.9",
"symplify/rule-doc-generator-contracts": "^10.0.9",
"symplify/simple-php-doc-parser": "^10.0.9",
"symplify/skipper": "^10.0.9",
"symplify/smart-file-system": "^10.0.9",
"symplify/symfony-php-config": "^10.0.9",
"symplify/vendor-patches": "^10.0.9",
"tracy/tracy": "^2.8",
"webmozart/assert": "^1.10",
"helmich/typo3-typoscript-parser": "dev-master#0ccb3a6"
Expand All @@ -73,14 +73,14 @@
"phpunit/phpunit": "^9.5",
"rector/phpstan-rules": "^0.4.15",
"spatie/enum": "^3.10",
"symplify/coding-standard": "^10.0.2",
"symplify/easy-ci": "^10.0.2",
"symplify/easy-coding-standard": "^10.0.2",
"symplify/easy-testing": "^10.0.2",
"symplify/monorepo-builder": "^10.0.2",
"symplify/phpstan-extensions": "^10.0.2",
"symplify/phpstan-rules": "^10.0.2",
"symplify/rule-doc-generator": "^10.0.2",
"symplify/coding-standard": "^10.0.9",
"symplify/easy-ci": "^10.0.9",
"symplify/easy-coding-standard": "^10.0.9",
"symplify/easy-testing": "^10.0.9",
"symplify/monorepo-builder": "^10.0.9",
"symplify/phpstan-extensions": "^10.0.9",
"symplify/phpstan-rules": "^10.0.9",
"symplify/rule-doc-generator": "^10.0.9",
"timeweb/phpstan-enum": "dev-22-upgrade-phpstan-to-1.0"
},
"replace": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
use PHPStan\AnalysedCodeException;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeContext;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Node\UnreachableStatementNode;
use PHPStan\Reflection\BetterReflection\Reflector\MemoizingReflector;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Caching\FileSystem\DependencyResolver;
Expand Down Expand Up @@ -78,10 +81,26 @@ public function processNodes(array $stmts, SmartFileInfo $smartFileInfo): array

$traitScope = clone $scope;

$scopeContext = $this->privatesAccessor->getPrivateProperty($traitScope, self::CONTEXT);
$scopeContext = $this->privatesAccessor->getPrivatePropertyOfClass(
$traitScope,
self::CONTEXT,
ScopeContext::class
);

$traitContext = clone $scopeContext;
$this->privatesAccessor->setPrivateProperty($traitContext, 'classReflection', $traitReflectionClass);
$this->privatesAccessor->setPrivateProperty($traitScope, self::CONTEXT, $traitContext);

$this->privatesAccessor->setPrivatePropertyOfClass(
$traitContext,
'classReflection',
$traitReflectionClass,
ClassReflection::class
);
$this->privatesAccessor->setPrivatePropertyOfClass(
$traitScope,
self::CONTEXT,
$traitContext,
ScopeContext::class
);

$this->nodeScopeResolver->processNodes($node->stmts, $traitScope, $nodeCallback);
return;
Expand Down Expand Up @@ -199,19 +218,36 @@ private function decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator(
): void {
// 1. get PHPStan locator
/** @var MemoizingReflector $classReflector */
$classReflector = $this->privatesAccessor->getPrivateProperty($nodeScopeResolver, 'reflector');

$reflector = $this->privatesAccessor->getPrivateProperty($classReflector, 'reflector');
$classReflector = $this->privatesAccessor->getPrivatePropertyOfClass(
$nodeScopeResolver,
'reflector',
Reflector::class
);

$reflector = $this->privatesAccessor->getPrivatePropertyOfClass(
$classReflector,
'reflector',
Reflector::class
);

/** @var SourceLocator $sourceLocator */
$sourceLocator = $this->privatesAccessor->getPrivateProperty($reflector, 'sourceLocator');
$sourceLocator = $this->privatesAccessor->getPrivatePropertyOfClass(
$reflector,
'sourceLocator',
SourceLocator::class
);

// 2. get Rector locator
$aggregateSourceLocator = new AggregateSourceLocator([
$sourceLocator,
$this->renamedClassesSourceLocator,
$this->parentAttributeSourceLocator,
]);
$this->privatesAccessor->setPrivateProperty($reflector, 'sourceLocator', $aggregateSourceLocator);
$this->privatesAccessor->setPrivatePropertyOfClass(
$reflector,
'sourceLocator',
$aggregateSourceLocator,
AggregateSourceLocator::class
);
}
}
16 changes: 14 additions & 2 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,21 @@ public function normalizeArrayTypeAndArrayNever(Type $type): Type
{
return TypeTraverser::map($type, function (Type $traversedType, callable $traverserCallable): Type {
if ($this->isConstantArrayNever($traversedType)) {
assert($traversedType instanceof ConstantArrayType);

// not sure why, but with direct new node everything gets nulled to MixedType
$this->privatesAccessor->setPrivateProperty($traversedType, 'keyType', new MixedType());
$this->privatesAccessor->setPrivateProperty($traversedType, 'itemType', new MixedType());
$this->privatesAccessor->setPrivatePropertyOfClass(
$traversedType,
'keyType',
new MixedType(),
Type::class
);
$this->privatesAccessor->setPrivatePropertyOfClass(
$traversedType,
'itemType',
new MixedType(),
Type::class
);

return $traversedType;
}
Expand Down

0 comments on commit bbe0567

Please sign in to comment.