Skip to content

Commit

Permalink
Remove myclabs/php-enum dependency (#2388)
Browse files Browse the repository at this point in the history
* next

* use TypeKind in PHPStan enum type check

* turn ObjectReference into constant enumlike

* prefer selt/this to constant enum
  • Loading branch information
TomasVotruba authored May 29, 2022
1 parent 953f9c7 commit db57fba
Show file tree
Hide file tree
Showing 110 changed files with 336 additions and 311 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"composer/semver": "^3.3",
"composer/xdebug-handler": "^3.0",
"doctrine/inflector": "^2.0",
"myclabs/php-enum": "^1.8",
"nette/utils": "^3.2.7",
"nikic/php-parser": "^4.13.2",
"ondram/ci-detector": "^4.1",
Expand Down
8 changes: 6 additions & 2 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@
// buggy - @todo fix on Symplify master
DocBlockLineLengthFixer::class,

// double to Double false positive
PhpdocTypesFixer::class => [__DIR__ . '/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php'],
PhpdocTypesFixer::class => [
// double to Double false positive
__DIR__ . '/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php',
// skip for enum types
__DIR__ . '/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php',
],

// breaking and handled better by Rector PHPUnit code quality set, removed in symplify dev-main
PhpUnitStrictFixer::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testMapPHPStanTypeToPHPStanPhpDocTypeNode(): void

$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$iterableType,
TypeKind::ANY()
TypeKind::ANY
);
$this->assertInstanceOf(ArrayTypeNode::class, $phpStanDocTypeNode);

Expand All @@ -77,7 +77,7 @@ public function testMixed(): void

$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$mixedType,
TypeKind::ANY()
TypeKind::ANY
);
$this->assertInstanceOf(IdentifierTypeNode::class, $phpStanDocTypeNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
*/
public function testWithoutKeys(ArrayType $arrayType, string $expectedResult): void
{
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY());
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY);
$this->assertSame($expectedResult, (string) $actualTypeNode);
}

Expand All @@ -40,7 +40,7 @@ public function testWithoutKeys(ArrayType $arrayType, string $expectedResult): v
*/
public function testWithKeys(ArrayType $arrayType, string $expectedResult): void
{
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY());
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY);
$this->assertSame($expectedResult, (string) $actualTypeNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function changeVarType(PhpDocInfo $phpDocInfo, Type $newType): void
// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::PROPERTY()
TypeKind::PROPERTY
);

$currentVarTagValueNode = $phpDocInfo->getVarTagValueNode();
Expand Down Expand Up @@ -108,7 +108,7 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): bool
// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::RETURN()
TypeKind::RETURN
);

$currentReturnTagValueNode = $phpDocInfo->getReturnTagValue();
Expand All @@ -132,7 +132,7 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
return;
}

$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM());
$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM);
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

// override existing type
Expand Down
2 changes: 1 addition & 1 deletion packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getPossibleUnionPropertyType(
$varType = new UnionType([$varType, new NullType()]);
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$varType,
TypeKind::PROPERTY()
TypeKind::PROPERTY
);

return new PropertyType($varType, $propertyTypeNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function resolveClassConstFetchType(ClassConstFetch $classConstFetch): M
{
$classConstantReference = $this->valueResolver->getValue($classConstFetch);

if (ObjectReference::STATIC()->getValue() === $classConstantReference) {
if ($classConstantReference === ObjectReference::STATIC) {
$classLike = $this->betterNodeFinder->findParentType($classConstFetch, Class_::class);
if (! $classLike instanceof ClassLike) {
return new MixedType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getNodeClasses(): array
*/
public function resolve(Node $node): Type
{
if ($node->toString() === ObjectReference::PARENT()->getValue()) {
if ($node->toString() === ObjectReference::PARENT) {
return $this->resolveParent($node);
}

Expand Down Expand Up @@ -99,11 +99,7 @@ private function resolveFullyQualifiedName(Name $name): string
{
$nameValue = $name->toString();

if (in_array(
$nameValue,
[ObjectReference::SELF()->getValue(), ObjectReference::STATIC()->getValue(), 'this'],
true
)) {
if (in_array($nameValue, [ObjectReference::SELF, ObjectReference::STATIC, 'this'], true)) {
$classLike = $this->betterNodeFinder->findParentType($name, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return $name->toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public function resolve(Node $node): Type
{
if ($node->class instanceof Name) {
$className = $this->nodeNameResolver->getName($node->class);
if (! in_array(
$className,
[ObjectReference::SELF()->getValue(), ObjectReference::PARENT()->getValue()],
true
)) {
if (! in_array($className, [ObjectReference::SELF, ObjectReference::PARENT], true)) {
return new ObjectType($className);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function enterNode(Node $node): ?Node

$newTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$oldToNewType->getNewType(),
TypeKind::ANY()
TypeKind::ANY
);

$parentType = $node->getAttribute(PhpDocAttributeKey::PARENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public function getNodeClass(): string;

/**
* @param TType $type
* @param TypeKind::* $typeKind
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode;
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode;

/**
* @param TType $type
* @param TypeKind::* $typeKind
* @return Name|ComplexType|null
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node;
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node;
}
18 changes: 5 additions & 13 deletions packages/PHPStanStaticTypeMapper/Enum/TypeKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@

namespace Rector\PHPStanStaticTypeMapper\Enum;

use MyCLabs\Enum\Enum;

/**
* @method static TypeKind PROPERTY()
* @method static TypeKind RETURN()
* @method static TypeKind PARAM()
* @method static TypeKind ANY()
*/
final class TypeKind extends Enum
final class TypeKind
{
/**
* @var string
*/
private const PROPERTY = 'property';
public const PROPERTY = 'property';

/**
* @var string
*/
private const RETURN = 'return';
public const RETURN = 'return';

/**
* @var string
*/
private const PARAM = 'param';
public const PARAM = 'param';

/**
* @var string
*/
private const ANY = 'any';
public const ANY = 'any';
}
10 changes: 8 additions & 2 deletions packages/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function __construct(
) {
}

public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
/**
* @param TypeKind::* $typeKind
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
Expand All @@ -51,7 +54,10 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class);
}

public function mapToPhpParserNode(Type $type, TypeKind $typeKind): Name | ComplexType | null
/**
* @param TypeKind::* $typeKind
*/
public function mapToPhpParserNode(Type $type, string $typeKind): Name | ComplexType | null
{
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;

/**
* @implements TypeMapperInterface<AccessoryNonEmptyStringType>
Expand All @@ -29,15 +28,15 @@ public function getNodeClass(): string
/**
* @param AccessoryNonEmptyStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
return new IdentifierTypeNode('string');
}

/**
* @param AccessoryNonEmptyStringType $type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('string');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;

/**
* @implements TypeMapperInterface<AccessoryNumericStringType>
Expand All @@ -29,15 +28,15 @@ public function getNodeClass(): string
/**
* @param AccessoryNumericStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
return new IdentifierTypeNode('string');
}

/**
* @param AccessoryNumericStringType $type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('string');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function mapConstantArrayType(ConstantArrayType $constantArrayType): Arra

$valueDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode(
$valueType,
TypeKind::RETURN()
TypeKind::RETURN
);

$arrayShapeItemNodes[] = new ArrayShapeItemNode(
Expand Down
27 changes: 20 additions & 7 deletions packages/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ public function getNodeClass(): string
}

/**
* @param TypeKind::* $typeKind
* @param ArrayType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
$itemType = $type->getItemType();

if ($itemType instanceof UnionType && ! $type instanceof ConstantArrayType) {
return $this->createArrayTypeNodeFromUnionType($itemType, $typeKind);
}

if ($type instanceof ConstantArrayType && $typeKind->equals(TypeKind::RETURN())) {
if ($type instanceof ConstantArrayType && $typeKind === TypeKind::RETURN) {
$arrayShapeNode = $this->arrayShapeTypeMapper->mapConstantArrayType($type);
if ($arrayShapeNode instanceof TypeNode) {
return $arrayShapeNode;
Expand Down Expand Up @@ -121,14 +122,17 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
/**
* @param ArrayType $type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name('array');
}

/**
* @param TypeKind::* $typeKind
*/
private function createArrayTypeNodeFromUnionType(
UnionType $unionType,
TypeKind $typeKind
string $typeKind
): SpacingAwareArrayTypeNode {
$unionedArrayType = [];
foreach ($unionType->getTypes() as $unionedType) {
Expand Down Expand Up @@ -183,9 +187,12 @@ private function isGenericArrayCandidate(ArrayType $arrayType): bool
return false;
}

/**
* @param TypeKind::* $typeKind
*/
private function createGenericArrayType(
ArrayType $arrayType,
TypeKind $typeKind,
string $typeKind,
bool $withKey = false
): GenericTypeNode {
$itemType = $arrayType->getItemType();
Expand Down Expand Up @@ -248,10 +255,13 @@ private function isIntegerKeyAndNonNestedArray(ArrayType $arrayType): bool
return ! $arrayType->getItemType() instanceof ArrayType;
}

/**
* @param TypeKind::* $typeKind
*/
private function narrowConstantArrayTypeOfUnionType(
ArrayType $arrayType,
Type $itemType,
TypeKind $typeKind
string $typeKind
): ?TypeNode {
if ($arrayType instanceof ConstantArrayType && $itemType instanceof UnionType) {
$narrowedItemType = $this->unionTypeCommonTypeNarrower->narrowToSharedObjectType($itemType);
Expand All @@ -272,9 +282,12 @@ private function narrowConstantArrayTypeOfUnionType(
return null;
}

/**
* @param TypeKind::* $typeKind
*/
private function createTypeNodeFromGenericClassStringType(
GenericClassStringType $genericClassStringType,
TypeKind $typeKind
string $typeKind
): IdentifierTypeNode|GenericTypeNode {
$genericType = $genericClassStringType->getGenericType();
if ($genericType instanceof ObjectType && ! $this->reflectionProvider->hasClass($genericType->getClassName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;

/**
* @implements TypeMapperInterface<BooleanType>
Expand All @@ -37,7 +36,7 @@ public function getNodeClass(): string
/**
* @param BooleanType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
if ($this->isFalseBooleanTypeWithUnion($type)) {
return new IdentifierTypeNode('false');
Expand All @@ -54,7 +53,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
/**
* @param BooleanType $type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
Expand Down
Loading

0 comments on commit db57fba

Please sign in to comment.