Skip to content

Commit

Permalink
Modernize rules to use RuleErrorBuilder and report error identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 5, 2023
1 parent ca9e4fd commit 2313a63
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 145 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10.3"
"phpstan/phpstan": "^1.11"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
Expand Down
30 changes: 19 additions & 11 deletions src/Rules/Deprecations/AccessDeprecatedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPStan\Reflection\MissingPropertyFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use function strtolower;

/**
* @implements Rule<PropertyFetch>
Expand Down Expand Up @@ -57,19 +59,25 @@ public function processNode(Node $node, Scope $scope): array
if ($propertyReflection->isDeprecated()->yes()) {
$description = $propertyReflection->getDeprecatedDescription();
if ($description === null) {
return [sprintf(
'Access to deprecated property $%s of class %s.',
$propertyName,
$propertyReflection->getDeclaringClass()->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
'Access to deprecated property $%s of %s %s.',
$propertyName,
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
$propertyReflection->getDeclaringClass()->getName()
))->identifier('property.deprecated')->build(),
];
}

return [sprintf(
"Access to deprecated property $%s of class %s:\n%s",
$propertyName,
$propertyReflection->getDeclaringClass()->getName(),
$description
)];
return [
RuleErrorBuilder::message(sprintf(
"Access to deprecated property $%s of %s %s:\n%s",
$propertyName,
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
$propertyReflection->getDeclaringClass()->getName(),
$description
))->identifier('property.deprecated')->build(),
];
}
} catch (ClassNotFoundException $e) {
// Other rules will notify if the class is not found
Expand Down
30 changes: 19 additions & 11 deletions src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use PHPStan\Reflection\MissingPropertyFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function sprintf;
use function strtolower;

/**
* @implements Rule<StaticPropertyFetch>
Expand Down Expand Up @@ -88,19 +90,25 @@ static function (Type $type) use ($propertyName): bool {
if ($property->isDeprecated()->yes()) {
$description = $property->getDeprecatedDescription();
if ($description === null) {
return [sprintf(
'Access to deprecated static property $%s of class %s.',
$propertyName,
$property->getDeclaringClass()->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
'Access to deprecated static property $%s of %s %s.',
$propertyName,
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
$property->getDeclaringClass()->getName()
))->identifier('staticProperty.deprecated')->build(),
];
}

return [sprintf(
"Access to deprecated static property $%s of class %s:\n%s",
$propertyName,
$property->getDeclaringClass()->getName(),
$description
)];
return [
RuleErrorBuilder::message(sprintf(
"Access to deprecated static property $%s of %s %s:\n%s",
$propertyName,
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
$property->getDeclaringClass()->getName(),
$description
))->identifier('staticProperty.deprecated')->build(),
];
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/Rules/Deprecations/CallToDeprecatedFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Broker\FunctionNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
Expand Down Expand Up @@ -54,17 +55,21 @@ public function processNode(Node $node, Scope $scope): array
if ($function->isDeprecated()->yes()) {
$description = $function->getDeprecatedDescription();
if ($description === null) {
return [sprintf(
'Call to deprecated function %s().',
$function->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
'Call to deprecated function %s().',
$function->getName()
))->identifier('function.deprecated')->build(),
];
}

return [sprintf(
"Call to deprecated function %s():\n%s",
$function->getName(),
$description
)];
return [
RuleErrorBuilder::message(sprintf(
"Call to deprecated function %s():\n%s",
$function->getName(),
$description
))->identifier('function.deprecated')->build(),
];
}

return [];
Expand Down
30 changes: 19 additions & 11 deletions src/Rules/Deprecations/CallToDeprecatedMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use function strtolower;

/**
* @implements Rule<MethodCall>
Expand Down Expand Up @@ -60,19 +62,25 @@ public function processNode(Node $node, Scope $scope): array

$description = $methodReflection->getDeprecatedDescription();
if ($description === null) {
return [sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
'Call to deprecated method %s() of %s %s.',
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName()
))->identifier('method.deprecated')->build(),
];
}

return [sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$description
)];
return [
RuleErrorBuilder::message(sprintf(
"Call to deprecated method %s() of %s %s:\n%s",
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
$description
))->identifier('method.deprecated')->build(),
];
} catch (ClassNotFoundException $e) {
// Other rules will notify if the class is not found
} catch (MissingMethodFromReflectionException $e) {
Expand Down
30 changes: 18 additions & 12 deletions src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function sprintf;
use function strtolower;

/**
* @implements Rule<StaticCall>
Expand Down Expand Up @@ -90,18 +92,20 @@ static function (Type $type) use ($methodName): bool {
if ($class->isDeprecated()) {
$classDescription = $class->getDeprecatedDescription();
if ($classDescription === null) {
$errors[] = sprintf(
'Call to method %s() of deprecated class %s.',
$errors[] = RuleErrorBuilder::message(sprintf(
'Call to method %s() of deprecated %s %s.',
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName()
);
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
} else {
$errors[] = sprintf(
"Call to method %s() of deprecated class %s:\n%s",
$errors[] = RuleErrorBuilder::message(sprintf(
"Call to method %s() of deprecated %s %s:\n%s",
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
$classDescription
);
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
}
}

Expand All @@ -111,18 +115,20 @@ static function (Type $type) use ($methodName): bool {

$description = $methodReflection->getDeprecatedDescription();
if ($description === null) {
$errors[] = sprintf(
'Call to deprecated method %s() of class %s.',
$errors[] = RuleErrorBuilder::message(sprintf(
'Call to deprecated method %s() of %s %s.',
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName()
);
))->identifier('staticMethod.deprecated')->build();
} else {
$errors[] = sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$errors[] = RuleErrorBuilder::message(sprintf(
"Call to deprecated method %s() of %s %s:\n%s",
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
$description
);
))->identifier('staticMethod.deprecated')->build();
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/Rules/Deprecations/DeprecatedClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ public function __construct(ReflectionProvider $reflectionProvider)
$this->reflectionProvider = $reflectionProvider;
}

public function getClassType(ClassReflection $class): string
{
if ($class->isInterface()) {
return 'interface';
}

return 'class';
}

public function getClassDeprecationDescription(ClassReflection $class): string
{
$description = $class->getDeprecatedDescription();
Expand Down
29 changes: 17 additions & 12 deletions src/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -87,18 +88,20 @@ static function (Type $type) use ($constantName): bool {
if ($class->isDeprecated()) {
$classDescription = $class->getDeprecatedDescription();
if ($classDescription === null) {
$errors[] = sprintf(
'Fetching class constant %s of deprecated class %s.',
$errors[] = RuleErrorBuilder::message(sprintf(
'Fetching class constant %s of deprecated %s %s.',
$constantName,
strtolower($class->getClassTypeDescription()),
$referencedClass
);
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
} else {
$errors[] = sprintf(
"Fetching class constant %s of deprecated class %s:\n%s",
$errors[] = RuleErrorBuilder::message(sprintf(
"Fetching class constant %s of deprecated %s %s:\n%s",
$constantName,
strtolower($class->getClassTypeDescription()),
$referencedClass,
$classDescription
);
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
}
}

Expand All @@ -118,18 +121,20 @@ static function (Type $type) use ($constantName): bool {

$description = $constantReflection->getDeprecatedDescription();
if ($description === null) {
$errors[] = sprintf(
'Fetching deprecated class constant %s of class %s.',
$errors[] = RuleErrorBuilder::message(sprintf(
'Fetching deprecated class constant %s of %s %s.',
$constantName,
strtolower($class->getClassTypeDescription()),
$referencedClass
);
))->identifier('classConstant.deprecated')->build();
} else {
$errors[] = sprintf(
"Fetching deprecated class constant %s of class %s:\n%s",
$errors[] = RuleErrorBuilder::message(sprintf(
"Fetching deprecated class constant %s of %s %s:\n%s",
$constantName,
strtolower($class->getClassTypeDescription()),
$referencedClass,
$description
);
))->identifier('classConstant.deprecated')->build();
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/Rules/Deprecations/FetchingDeprecatedConstRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use const PHP_VERSION_ID;

Expand Down Expand Up @@ -55,17 +56,21 @@ public function processNode(Node $node, Scope $scope): array
$constantReflection = $this->reflectionProvider->getConstant($node->name, $scope);

if ($constantReflection->isDeprecated()->yes()) {
return [sprintf(
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
$constantReflection->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
$constantReflection->getName()
))->identifier('constant.deprecated')->build(),
];
}

if (isset($this->deprecatedConstants[$constantReflection->getName()])) {
return [sprintf(
$this->deprecatedConstants[$constantReflection->getName()],
$constantReflection->getName()
)];
return [
RuleErrorBuilder::message(sprintf(
$this->deprecatedConstants[$constantReflection->getName()],
$constantReflection->getName()
))->identifier('constant.deprecated')->build(),
];
}

return [];
Expand Down
Loading

0 comments on commit 2313a63

Please sign in to comment.