Skip to content

Commit f9e7b05

Browse files
committed
Skip constructor defaults on extensible classes
1 parent 7ba3da3 commit f9e7b05

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class ValidatedBase
4+
{
5+
private bool $validated = false;
6+
7+
protected function __construct()
8+
{
9+
$this->validate();
10+
$this->validated = true;
11+
}
12+
13+
private function validate(): void
14+
{
15+
}
16+
}
17+
18+
final class SkipsParentConstructor extends ValidatedBase
19+
{
20+
public function __construct()
21+
{
22+
}
23+
}

‎rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public function getNodeTypes(): array
8282
*/
8383
public function refactor(Node $node): ?Node
8484
{
85+
// A child class may bypass the constructor and depend on the declared property default
86+
if (! $node->isFinal()) {
87+
return null;
88+
}
89+
8590
$hasChanged = false;
8691

8792
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);

0 commit comments

Comments
 (0)