Skip to content

Commit

Permalink
cleanup property type inferer in PromotedPropertyCandidateResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 28, 2022
1 parent c9f098d commit 32e5bd9
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 352 deletions.
31 changes: 0 additions & 31 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\TypeDeclaration\TypeNormalizer;
Expand All @@ -40,7 +39,6 @@ public function __construct(
private readonly ArrayTypeComparator $arrayTypeComparator,
private readonly ScalarTypeComparator $scalarTypeComparator,
private readonly TypeFactory $typeFactory,
private readonly UnionTypeAnalyzer $unionTypeAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder
) {
}
Expand Down Expand Up @@ -133,35 +131,6 @@ public function isSubtype(Type $checkedType, Type $mainType): bool
return $this->arrayTypeComparator->isSubtype($checkedType, $mainType);
}

public function areTypesPossiblyIncluded(Type $assumptionType, ?Type $exactType): bool
{
if (! $exactType instanceof Type) {
return true;
}

if ($this->areTypesEqual($assumptionType, $exactType)) {
return true;
}

if (! $assumptionType instanceof UnionType) {
return true;
}

if (! $exactType instanceof UnionType) {
return true;
}

$countAssumpionTypeTypes = count($assumptionType->getTypes());
$countExactTypeTypes = count($exactType->getTypes());

if ($countAssumpionTypeTypes === $countExactTypeTypes) {
$unionType = $this->unionTypeAnalyzer->mapGenericToClassStringType($exactType);
return $this->areTypesEqual($assumptionType, $unionType);
}

return $countAssumpionTypeTypes > $countExactTypeTypes;
}

private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool
{
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -150,16 +148,4 @@ public function isNullable(UnionType $unionType, bool $checkTwoTypes = false): b

return false;
}

public function mapGenericToClassStringType(UnionType $unionType): UnionType
{
$types = $unionType->getTypes();
foreach ($types as $key => $type) {
if ($type instanceof GenericClassStringType) {
$types[$key] = new ClassStringType();
}
}

return new UnionType($types);
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,4 @@ parameters:

# on purpose, as rule it about to be removed
- '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddArrayParamDocTypeRector" is missing @see annotation with test case class reference#'
- '#Register "Rector\\Php74\\Rector\\Property\\TypedPropertyRector" service to "php74\.php" config set#'
3 changes: 2 additions & 1 deletion rules/Php74/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -35,7 +36,7 @@ public function getRuleDefinition(): RuleDefinition
return new RuleDefinition(
'Changes property type by `@var` annotations or default value.',
[
new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
Expand Down
75 changes: 2 additions & 73 deletions rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,19 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;

final class PromotedPropertyCandidateResolver
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeComparator $nodeComparator,
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly TypeComparator $typeComparator,
private readonly TypeFactory $typeFactory,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}
Expand Down Expand Up @@ -111,7 +99,7 @@ private function matchPropertyPromotionCandidate(
continue;
}

if ($this->shouldSkipParam($matchedParam, $property, $assignedExpr, $firstParamAsVariable)) {
if ($this->shouldSkipParam($matchedParam, $assignedExpr, $firstParamAsVariable)) {
continue;
}

Expand Down Expand Up @@ -180,64 +168,11 @@ private function isParamUsedBeforeAssign(Variable $variable, array $firstParamAs
return $firstVariablePosition < $variable->getStartTokenPos();
}

private function hasConflictingParamType(Param $param, Type $propertyType): bool
{
if ($param->type === null) {
return false;
}

$matchedParamType = $this->nodeTypeResolver->getType($param);
if ($param->default !== null) {
$defaultValueType = $this->nodeTypeResolver->getType($param->default);
$matchedParamType = $this->typeFactory->createMixedPassedOrUnionType(
[$matchedParamType, $defaultValueType]
);
}

if (! $propertyType instanceof UnionType) {
return false;
}

if ($this->typeComparator->areTypesEqual($propertyType, $matchedParamType)) {
return false;
}

// different types, check not has mixed and not has templated generic types
if (! $this->hasMixedType($propertyType)) {
return false;
}

return ! $this->hasTemplatedGenericType($propertyType);
}

private function hasTemplatedGenericType(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $type) {
if ($type instanceof TemplateType) {
return true;
}
}

return false;
}

private function hasMixedType(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $type) {
if ($type instanceof MixedType) {
return true;
}
}

return false;
}

/**
* @param int[] $firstParamAsVariable
*/
private function shouldSkipParam(
Param $matchedParam,
Property $property,
Variable $assignedVariable,
array $firstParamAsVariable
): bool {
Expand All @@ -246,12 +181,6 @@ private function shouldSkipParam(
return true;
}

if ($this->isParamUsedBeforeAssign($assignedVariable, $firstParamAsVariable)) {
return true;
}

// @todo unknown type, not suitable?
$propertyType = $this->varDocPropertyTypeInferer->inferProperty($property);
return $this->hasConflictingParamType($matchedParam, $propertyType);
return $this->isParamUsedBeforeAssign($assignedVariable, $firstParamAsVariable);
}
}

This file was deleted.

Loading

0 comments on commit 32e5bd9

Please sign in to comment.