forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rectorphp#2780 from rectorphp/doc-api-cleanup
remove deprecated removeBy* in DocBlockManipulator
- Loading branch information
Showing
18 changed files
with
295 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/NodeTypeResolver/src/PHPStan/TypeComparator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PHPStan; | ||
|
||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\FloatType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use Rector\PHPStan\Type\AliasedObjectType; | ||
use Rector\PHPStan\Type\ShortenedObjectType; | ||
|
||
final class TypeComparator | ||
{ | ||
/** | ||
* @var TypeHasher | ||
*/ | ||
private $typeHasher; | ||
|
||
public function __construct(TypeHasher $typeHasher) | ||
{ | ||
$this->typeHasher = $typeHasher; | ||
} | ||
|
||
public function areTypesEquals(Type $firstType, Type $secondType): bool | ||
{ | ||
if ($this->areBothSameScalarType($firstType, $secondType)) { | ||
return true; | ||
} | ||
|
||
// aliases and types | ||
if ($this->areAliasedObjectMatchingFqnObject($firstType, $secondType)) { | ||
return true; | ||
} | ||
|
||
if ($this->typeHasher->areTypesEqual($firstType, $secondType)) { | ||
return true; | ||
} | ||
|
||
return $this->areArrayTypeWithSingleObjectChildToParent($firstType, $secondType); | ||
} | ||
|
||
private function areBothSameScalarType(Type $firstType, Type $secondType): bool | ||
{ | ||
if ($firstType instanceof StringType && $secondType instanceof StringType) { | ||
return true; | ||
} | ||
|
||
if ($firstType instanceof IntegerType && $secondType instanceof IntegerType) { | ||
return true; | ||
} | ||
|
||
if ($firstType instanceof FloatType && $secondType instanceof FloatType) { | ||
return true; | ||
} | ||
return $firstType instanceof BooleanType && $secondType instanceof BooleanType; | ||
} | ||
|
||
private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool | ||
{ | ||
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType && $firstType->getFullyQualifiedClass() === $secondType->getClassName()) { | ||
return true; | ||
} | ||
return $secondType instanceof AliasedObjectType && $firstType instanceof ObjectType && $secondType->getFullyQualifiedClass() === $firstType->getClassName(); | ||
} | ||
|
||
/** | ||
* E.g. class A extends B, class B → A[] is subtype of B[] → keep A[] | ||
*/ | ||
private function areArrayTypeWithSingleObjectChildToParent(Type $firstType, Type $secondType): bool | ||
{ | ||
if (! $firstType instanceof ArrayType || ! $secondType instanceof ArrayType) { | ||
return false; | ||
} | ||
|
||
$firstArrayItemType = $firstType->getItemType(); | ||
$secondArrayItemType = $secondType->getItemType(); | ||
|
||
if ($firstArrayItemType instanceof ObjectType && $secondArrayItemType instanceof ObjectType) { | ||
$firstFqnClassName = $this->getFqnClassName($firstArrayItemType); | ||
$secondFqnClassName = $this->getFqnClassName($secondArrayItemType); | ||
|
||
if (is_a($firstFqnClassName, $secondFqnClassName, true)) { | ||
return true; | ||
} | ||
|
||
if (is_a($secondFqnClassName, $firstFqnClassName, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getFqnClassName(ObjectType $objectType): string | ||
{ | ||
if ($objectType instanceof ShortenedObjectType) { | ||
return $objectType->getFullyQualifiedName(); | ||
} | ||
|
||
return $objectType->getClassName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.