Skip to content

Commit

Permalink
Introduce ClassReflection::getClassTypeDescription()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 20, 2023
1 parent 8864fd7 commit dec9e43
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 63 deletions.
16 changes: 16 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,22 @@ public function isEnum(): bool
return $this->reflection->isEnum();
}

/**
* @return 'Interface'|'Trait'|'Enum'|'Class'
*/
public function getClassTypeDescription(): string
{
if ($this->isInterface()) {
return 'Interface';
} elseif ($this->isTrait()) {
return 'Trait';
} elseif ($this->isEnum()) {
return 'Enum';
}

return 'Class';
}

public function isReadOnly(): bool
{
return $this->reflection->isReadOnly();
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Api/ApiInstanceofRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\UnionType;
use function count;
use function sprintf;
use function strtolower;

/**
* @implements Rule<Node\Expr\Instanceof_>
Expand Down Expand Up @@ -52,9 +53,9 @@ public function processNode(Node $node, Scope $scope): array
$ruleError = RuleErrorBuilder::message(sprintf(
'Asking about instanceof %s is not covered by backward compatibility promise. The %s might change in a minor PHPStan version.',
$classReflection->getDisplayName(),
$classReflection->isInterface() ? 'interface' : 'class',
strtolower($classReflection->getClassTypeDescription()),
))
->identifier(sprintf('phpstanApi.%s', $classReflection->isInterface() ? 'interface' : 'class'))
->identifier(sprintf('phpstanApi.%s', strtolower($classReflection->getClassTypeDescription())))
->tip(sprintf(
"If you think it should be covered by backward compatibility promise, open a discussion:\n %s\n\n See also:\n https://phpstan.org/developing-extensions/backward-compatibility-promise",
'https://github.com/phpstan/phpstan/discussions',
Expand Down
11 changes: 1 addition & 10 deletions src/Rules/AttributesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ public function check(

$attributeClass = $this->reflectionProvider->getClass($name);
if (!$attributeClass->isAttributeClass()) {
$classLikeDescription = 'Class';
if ($attributeClass->isInterface()) {
$classLikeDescription = 'Interface';
} elseif ($attributeClass->isTrait()) {
$classLikeDescription = 'Trait';
} elseif ($attributeClass->isEnum()) {
$classLikeDescription = 'Enum';
}

$errors[] = RuleErrorBuilder::message(sprintf('%s %s is not an Attribute class.', $classLikeDescription, $attributeClass->getDisplayName()))
$errors[] = RuleErrorBuilder::message(sprintf('%s %s is not an Attribute class.', $attributeClass->getClassTypeDescription(), $attributeClass->getDisplayName()))
->identifier('attribute.notAttribute')
->line($attribute->getLine())
->build();
Expand Down
19 changes: 1 addition & 18 deletions src/Rules/ClassCaseSensitivityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function checkClassNames(array $pairs): array
continue;
}

$typeName = $this->getTypeName($classReflection);
$typeName = $classReflection->getClassTypeDescription();
$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s referenced with incorrect case: %s.',
$typeName,
Expand All @@ -53,20 +52,4 @@ public function checkClassNames(array $pairs): array
return $errors;
}

/**
* @return 'Interface'|'Trait'|'Enum'|'Class'
*/
private function getTypeName(ClassReflection $classReflection): string
{
if ($classReflection->isInterface()) {
return 'Interface';
} elseif ($classReflection->isTrait()) {
return 'Trait';
} elseif ($classReflection->isEnum()) {
return 'Enum';
}

return 'Class';
}

}
9 changes: 2 additions & 7 deletions src/Rules/Classes/AllowedSubTypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use function array_values;
use function sprintf;
use function strtolower;

/**
* @implements Rule<InClassNode>
Expand Down Expand Up @@ -53,13 +54,7 @@ public function processNode(Node $node, Scope $scope): array
}
}

$identifierType = 'class';
if ($classReflection->isInterface()) {
$identifierType = 'interface';
} elseif ($classReflection->isEnum()) {
$identifierType = 'enum';
}

$identifierType = strtolower($classReflection->getClassTypeDescription());
$messages[] = RuleErrorBuilder::message(sprintf(
'Type %s is not allowed to be a subtype of %s.',
$className,
Expand Down
8 changes: 2 additions & 6 deletions src/Rules/Classes/DuplicateClassDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function count;
use function implode;
use function sprintf;
use function strtolower;

/**
* @implements Rule<InClassNode>
Expand Down Expand Up @@ -51,12 +52,7 @@ public function processNode(Node $node, Scope $scope): array

$filteredClasses = array_filter($filteredClasses, static fn (ReflectionClass $class) => $class->getStartLine() !== $thisClass->getNativeReflection()->getStartLine());

$identifierType = 'class';
if ($thisClass->isInterface()) {
$identifierType = 'interface';
} elseif ($thisClass->isEnum()) {
$identifierType = 'enum';
}
$identifierType = strtolower($thisClass->getClassTypeDescription());

return [
RuleErrorBuilder::message(sprintf(
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Classes/DuplicateDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}

$identifierType = 'class';
if ($classReflection->isInterface()) {
$identifierType = 'interface';
} elseif ($classReflection->isEnum()) {
$identifierType = 'enum';
}
$identifierType = strtolower($classReflection->getClassTypeDescription());

$errors = [];

Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Classes/NonClassAttributeClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function sprintf;
use function strtolower;

/**
* @implements Rule<InClassNode>
Expand Down Expand Up @@ -50,9 +51,9 @@ private function check(Scope $scope): array
return [
RuleErrorBuilder::message(sprintf(
'%s cannot be an Attribute class.',
$classReflection->isInterface() ? 'Interface' : 'Enum',
$classReflection->getClassTypeDescription(),
))
->identifier(sprintf('attribute.%s', $classReflection->isInterface() ? 'interface' : 'enum'))
->identifier(sprintf('attribute.%s', strtolower($classReflection->getClassTypeDescription())))
->build(),
];
}
Expand Down
10 changes: 2 additions & 8 deletions src/Rules/Generics/GenericObjectTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function count;
use function implode;
use function sprintf;
use function strtolower;

class GenericObjectTypeCheck
{
Expand All @@ -39,14 +40,7 @@ public function check(
continue;
}

$classLikeDescription = 'class';
if ($classReflection->isInterface()) {
$classLikeDescription = 'interface';
} elseif ($classReflection->isTrait()) {
$classLikeDescription = 'trait';
} elseif ($classReflection->isEnum()) {
$classLikeDescription = 'enum';
}
$classLikeDescription = strtolower($classReflection->getClassTypeDescription());
if (!$classReflection->isGeneric()) {
$messages[] = RuleErrorBuilder::message(sprintf($classNotGenericMessage, $genericType->describe(VerbosityLevel::typeOnly()), $classLikeDescription, $classReflection->getDisplayName()))
->identifier('generics.notGeneric')
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/Generics/UsedTraitsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Type;
use function array_map;
use function sprintf;
use function strtolower;
use function ucfirst;

/**
Expand Down Expand Up @@ -56,11 +57,11 @@ public function processNode(Node $node, Scope $scope): array
$useTags = $resolvedPhpDoc->getUsesTags();
}

$description = sprintf('class %s', SprintfHelper::escapeFormatString($className));
$typeDescription = 'class';
$typeDescription = strtolower($scope->getClassReflection()->getClassTypeDescription());
$description = sprintf('%s %s', $typeDescription, SprintfHelper::escapeFormatString($className));
if ($traitName !== null) {
$description = sprintf('trait %s', SprintfHelper::escapeFormatString($traitName));
$typeDescription = 'trait';
$description = sprintf('%s %s', $typeDescription, SprintfHelper::escapeFormatString($traitName));
}

return $this->genericAncestorsCheck->check(
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/MissingTypehintCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function array_merge;
use function in_array;
use function sprintf;
use function strtolower;

class MissingTypehintCheck
{
Expand Down Expand Up @@ -139,7 +140,7 @@ public function getNonGenericObjectTypesWithGenericClass(Type $type): array
throw new ShouldNotHappenException();
}
$objectTypes[] = [
sprintf('%s %s', $classReflection->isInterface() ? 'interface' : 'class', $classReflection->getDisplayName(false)),
sprintf('%s %s', strtolower($classReflection->getClassTypeDescription()), $classReflection->getDisplayName(false)),
array_keys($classReflection->getTemplateTypeMap()->getTypes()),
];
return $type;
Expand Down

0 comments on commit dec9e43

Please sign in to comment.