Skip to content

Commit a82ced4

Browse files
committed
Fix false return-type violation for __destruct()
1 parent 5ae0efe commit a82ced4

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Analyser/MethodNode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function isConstructor(): bool
2828
{
2929
return $this->name === '__construct';
3030
}
31+
32+
public function isDestructor(): bool
33+
{
34+
return $this->name === '__destruct';
35+
}
3136
}

src/Rule/Rules/Method/MustHaveReturnTypeRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public function evaluateAll(ClassNode $classNode): array
4545
$violations = [];
4646

4747
foreach ($classNode->methods as $method) {
48-
if (! $method->isPublic() || $method->isConstructor()) {
48+
if (
49+
! $method->isPublic()
50+
|| $method->isConstructor()
51+
|| $method->isDestructor()
52+
) {
4953
continue;
5054
}
5155

tests/Analyser/MethodNodeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ final class MethodNodeTest extends TestCase
1414
public function testMethodHelpers(): void
1515
{
1616
$publicConstructor = new MethodNode('__construct', 'public', false, false, 1, 1, 3);
17+
$publicDestructor = new MethodNode('__destruct', 'public', false, false, 0, 1, 3);
1718
$protectedMethod = new MethodNode('handle', 'protected', true, false, 0, 1, 2);
1819

1920
$this->assertTrue($publicConstructor->isPublic());
2021
$this->assertTrue($publicConstructor->isConstructor());
22+
$this->assertFalse($publicConstructor->isDestructor());
23+
$this->assertTrue($publicDestructor->isDestructor());
2124
$this->assertFalse($protectedMethod->isPublic());
2225
$this->assertFalse($protectedMethod->isConstructor());
26+
$this->assertFalse($protectedMethod->isDestructor());
2327
}
2428
}

tests/Rule/Method/MustHaveReturnTypeRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function testIgnoresConstructor(): void
8282
$this->assertNotInstanceOf(RuleViolation::class, $mustHaveReturnTypeRule->evaluate($classNode));
8383
}
8484

85+
public function testIgnoresDestructor(): void
86+
{
87+
$mustHaveReturnTypeRule = new MustHaveReturnTypeRule(layer: 'Domain');
88+
$classNode = $this->makeNode([
89+
$this->method('__destruct', hasReturnType: false),
90+
]);
91+
92+
$this->assertNotInstanceOf(RuleViolation::class, $mustHaveReturnTypeRule->evaluate($classNode));
93+
}
94+
8595
public function testIgnoresPrivateMethods(): void
8696
{
8797
$mustHaveReturnTypeRule = new MustHaveReturnTypeRule(layer: 'Domain');

0 commit comments

Comments
 (0)