Skip to content

Commit b5dd34e

Browse files
committed
AbstractMethodInNonAbstractClassRule - better error message for enums
1 parent 530699d commit b5dd34e

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ lint:
3131
--exclude tests/PHPStan/Rules/Classes/data/implements-error.php \
3232
--exclude tests/PHPStan/Rules/Classes/data/interface-extends-error.php \
3333
--exclude tests/PHPStan/Rules/Classes/data/trait-use-error.php \
34+
--exclude tests/PHPStan/Rules/Methods/data/method-in-enum-without-body.php \
3435
--exclude tests/PHPStan/Rules/Properties/data/default-value-for-native-property-type.php \
3536
--exclude tests/PHPStan/Rules/Arrays/data/empty-array-item.php \
3637
--exclude tests/PHPStan/Rules/Classes/data/invalid-promoted-properties.php \

src/Rules/Methods/AbstractMethodInNonAbstractClassRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public function processNode(Node $node, Scope $scope): array
2929
$class = $scope->getClassReflection();
3030

3131
if (!$class->isAbstract() && $node->isAbstract()) {
32+
$description = $class->getClassTypeDescription();
3233
return [
3334
RuleErrorBuilder::message(sprintf(
3435
'%s %s contains abstract method %s().',
35-
$class->isInterface() ? 'Interface' : 'Non-abstract class',
36+
$description === 'Class' ? 'Non-abstract class' : $description,
3637
$class->getDisplayName(),
3738
$node->name->toString(),
3839
))
@@ -54,6 +55,8 @@ public function processNode(Node $node, Scope $scope): array
5455
->build(),
5556
];
5657
}
58+
59+
return [];
5760
}
5861

5962
}

tests/PHPStan/Rules/Methods/AbstractMethodInNonAbstractClassRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Bug3406\ClassFoo;
77
use PHPStan\Rules\Rule;
88
use PHPStan\Testing\RuleTestCase;
9+
use const PHP_VERSION_ID;
910

1011
/**
1112
* @extends RuleTestCase<AbstractMethodInNonAbstractClassRule>
@@ -70,4 +71,22 @@ public function testNonAbstractMethodWithNoBody(): void
7071
]);
7172
}
7273

74+
public function testEnum(): void
75+
{
76+
if (PHP_VERSION_ID < 80100) {
77+
$this->markTestSkipped('Test requires PHP 8.1.');
78+
}
79+
80+
$this->analyse([__DIR__ . '/data/method-in-enum-without-body.php'], [
81+
[
82+
'Non-abstract method MethodInEnumWithoutBody\Foo::doFoo() must contain a body.',
83+
8,
84+
],
85+
[
86+
'Enum MethodInEnumWithoutBody\Foo contains abstract method doBar().',
87+
10,
88+
],
89+
]);
90+
}
91+
7392
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lint >= 8.1
2+
3+
namespace MethodInEnumWithoutBody;
4+
5+
enum Foo
6+
{
7+
8+
public function doFoo(): void;
9+
10+
abstract public function doBar(): void;
11+
12+
}

0 commit comments

Comments
 (0)