Skip to content

Commit e55bc07

Browse files
committed
Skip constructor defaults on extensible classes
1 parent 40943e4 commit e55bc07

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
@@ -78,6 +78,11 @@ public function getNodeTypes(): array
7878
*/
7979
public function refactor(Node $node): ?Node
8080
{
81+
// A child class may bypass the constructor and depend on the declared property default
82+
if (! $node->isFinal()) {
83+
return null;
84+
}
85+
8186
$hasChanged = false;
8287

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

0 commit comments

Comments
 (0)