Skip to content

Commit

Permalink
AbstractMethodInNonAbstractClassRule - better error message for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 13, 2023
1 parent 530699d commit b5dd34e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ lint:
--exclude tests/PHPStan/Rules/Classes/data/implements-error.php \
--exclude tests/PHPStan/Rules/Classes/data/interface-extends-error.php \
--exclude tests/PHPStan/Rules/Classes/data/trait-use-error.php \
--exclude tests/PHPStan/Rules/Methods/data/method-in-enum-without-body.php \
--exclude tests/PHPStan/Rules/Properties/data/default-value-for-native-property-type.php \
--exclude tests/PHPStan/Rules/Arrays/data/empty-array-item.php \
--exclude tests/PHPStan/Rules/Classes/data/invalid-promoted-properties.php \
Expand Down
5 changes: 4 additions & 1 deletion src/Rules/Methods/AbstractMethodInNonAbstractClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public function processNode(Node $node, Scope $scope): array
$class = $scope->getClassReflection();

if (!$class->isAbstract() && $node->isAbstract()) {
$description = $class->getClassTypeDescription();
return [
RuleErrorBuilder::message(sprintf(
'%s %s contains abstract method %s().',
$class->isInterface() ? 'Interface' : 'Non-abstract class',
$description === 'Class' ? 'Non-abstract class' : $description,
$class->getDisplayName(),
$node->name->toString(),
))
Expand All @@ -54,6 +55,8 @@ public function processNode(Node $node, Scope $scope): array
->build(),
];
}

return [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Bug3406\ClassFoo;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<AbstractMethodInNonAbstractClassRule>
Expand Down Expand Up @@ -70,4 +71,22 @@ public function testNonAbstractMethodWithNoBody(): void
]);
}

public function testEnum(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/method-in-enum-without-body.php'], [
[
'Non-abstract method MethodInEnumWithoutBody\Foo::doFoo() must contain a body.',
8,
],
[
'Enum MethodInEnumWithoutBody\Foo contains abstract method doBar().',
10,
],
]);
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/data/method-in-enum-without-body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php // lint >= 8.1

namespace MethodInEnumWithoutBody;

enum Foo
{

public function doFoo(): void;

abstract public function doBar(): void;

}

0 comments on commit b5dd34e

Please sign in to comment.