Update to PHPStan 2.x

This commit is contained in:
Dylan K. Taylor
2025-01-07 22:34:43 +00:00
parent d25ec58a6f
commit 9633b7d8a7
18 changed files with 876 additions and 344 deletions

View File

@ -28,7 +28,6 @@ use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeWithClassName;
use pocketmine\utils\LegacyEnumShimTrait;
use function sprintf;
@ -51,18 +50,15 @@ final class DeprecatedLegacyEnumAccessRule implements Rule{
$scope->resolveTypeByName($node->class) :
$scope->getType($node->class);
if(!$classType instanceof TypeWithClassName){
return [];
}
$errors = [];
$reflections = $classType->getObjectClassReflections();
foreach($reflections as $reflection){
if(!$reflection->hasTraitUse(LegacyEnumShimTrait::class) || !$reflection->implementsInterface(\UnitEnum::class)){
continue;
}
$reflection = $classType->getClassReflection();
if($reflection === null || !$reflection->hasTraitUse(LegacyEnumShimTrait::class) || !$reflection->implementsInterface(\UnitEnum::class)){
return [];
}
if(!$reflection->hasNativeMethod($caseName)){
return [
RuleErrorBuilder::message(sprintf(
if(!$reflection->hasNativeMethod($caseName)){
$errors[] = RuleErrorBuilder::message(sprintf(
'Use of legacy enum case accessor %s::%s().',
$reflection->getName(),
$caseName
@ -70,10 +66,11 @@ final class DeprecatedLegacyEnumAccessRule implements Rule{
'Access the enum constant directly instead (remove the brackets), e.g. %s::%s',
$reflection->getName(),
$caseName
))->build()
];
))->identifier('pocketmine.enum.deprecatedAccessor')
->build();
}
}
return [];
return $errors;
}
}

View File

@ -30,7 +30,6 @@ use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
@ -61,7 +60,7 @@ class DisallowEnumComparisonRule implements Rule{
$node instanceof Identical ? '===' : '!==',
$leftType->describe(VerbosityLevel::value()),
$rightType->describe(VerbosityLevel::value())
))->build()];
))->identifier('pocketmine.enum.badComparison')->build()];
}
return [];
}
@ -69,7 +68,7 @@ class DisallowEnumComparisonRule implements Rule{
private function checkForEnumTypes(Type $comparedType) : bool{
//TODO: what we really want to do here is iterate over the contained types, but there's no universal way to
//do that. This might break with other circumstances.
if($comparedType instanceof ObjectType){
if($comparedType->isObject()->yes()){
$types = [$comparedType];
}elseif($comparedType instanceof UnionType){
$types = $comparedType->getTypes();
@ -77,12 +76,14 @@ class DisallowEnumComparisonRule implements Rule{
return false;
}
foreach($types as $containedType){
if(!($containedType instanceof ObjectType)){
if(!($containedType->isObject()->yes())){
continue;
}
$class = $containedType->getClassReflection();
if($class !== null && $class->hasTraitUse(EnumTrait::class)){
return true;
$classes = $containedType->getObjectClassReflections();
foreach($classes as $class){
if($class->hasTraitUse(EnumTrait::class)){
return true;
}
}
}
return false;

View File

@ -44,6 +44,7 @@ final class DisallowForeachByReferenceRule implements Rule{
return [
RuleErrorBuilder::message("Foreach by-reference is not allowed, because it has surprising behaviour.")
->tip("If the value variable is used outside of the foreach construct (e.g. in a second foreach), the iterable's contents will be unexpectedly altered.")
->identifier('pocketmine.foreach.byRef')
->build()
];
}

View File

@ -101,7 +101,7 @@ final class UnsafeForeachArrayOfStringRule implements Rule{
RuleErrorBuilder::message(sprintf(
"Unsafe foreach on array with key type %s (they might be casted to int).",
$iterableType->getIterableKeyType()->describe(VerbosityLevel::value())
))->tip($tip)->build()
))->tip($tip)->identifier('pocketmine.foreach.stringKeys')->build()
];
}
return [];