Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check redeclared promoted readonly properties
- Loading branch information
Showing
9 changed files
with
568 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug8101; | ||
|
||
class A { | ||
public function __construct(public readonly int $myProp) {} | ||
} | ||
|
||
class B extends A { | ||
// This should be reported as an error, as a readonly prop cannot be redeclared. | ||
public function __construct(public readonly int $myProp) { | ||
parent::__construct($myProp); | ||
} | ||
} | ||
|
||
$foo = new B(7); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug9863; | ||
|
||
class ReadonlyParentWithoutIsset | ||
{ | ||
public function __construct( | ||
public readonly int $foo | ||
) {} | ||
} | ||
|
||
class ReadonlyChildWithoutIsset extends ReadonlyParentWithoutIsset | ||
{ | ||
public function __construct( | ||
public readonly int $foo = 42 | ||
) { | ||
parent::__construct($foo); | ||
} | ||
} | ||
|
||
class ReadonlyParentWithIsset | ||
{ | ||
public readonly int $foo; | ||
|
||
public function __construct( | ||
int $foo | ||
) { | ||
if (! isset($this->foo)) { | ||
$this->foo = $foo; | ||
} | ||
} | ||
} | ||
|
||
class ReadonlyChildWithIsset extends ReadonlyParentWithIsset | ||
{ | ||
public function __construct( | ||
public readonly int $foo = 42 | ||
) { | ||
parent::__construct($foo); | ||
} | ||
} | ||
|
||
$a = new ReadonlyParentWithoutIsset(0); | ||
$b = new ReadonlyChildWithoutIsset(); | ||
$c = new ReadonlyChildWithoutIsset(1); | ||
|
||
$x = new ReadonlyParentWithIsset(2); | ||
$y = new ReadonlyChildWithIsset(); | ||
$z = new ReadonlyChildWithIsset(3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.2 | ||
|
||
namespace Bug9864; | ||
|
||
readonly abstract class UuidValueObject | ||
{ | ||
public function __construct(public string $value) | ||
{ | ||
$this->ensureIsValidUuid($value); | ||
} | ||
|
||
private function ensureIsValidUuid(string $value): void | ||
{ | ||
} | ||
} | ||
|
||
|
||
final readonly class ProductId extends UuidValueObject | ||
{ | ||
public string $value; | ||
|
||
public function __construct( | ||
string $value | ||
) { | ||
parent::__construct($value); | ||
} | ||
} | ||
|
||
var_dump(new ProductId('test')); | ||
|
||
// property is assigned on parent class, no need to reassing, specially for readonly properties |
50 changes: 50 additions & 0 deletions
50
tests/PHPStan/Rules/Properties/data/redeclare-property-of-readonly-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.2 | ||
|
||
namespace RedeclarePropertyOfReadonlyClass; | ||
|
||
readonly class A { | ||
public function __construct(public int $promotedProp) | ||
{ | ||
} | ||
} | ||
|
||
readonly class B1 extends A { | ||
// $promotedProp is written twice | ||
public function __construct(public int $promotedProp) | ||
{ | ||
parent::__construct(5); | ||
} | ||
} | ||
|
||
readonly class B2 extends A { | ||
// Don't get confused by standard parameter with same name | ||
public function __construct(int $promotedProp) | ||
{ | ||
parent::__construct($promotedProp); | ||
} | ||
} | ||
|
||
readonly class B3 extends A { | ||
// This is allowed, because we don't write to the property. | ||
public int $promotedProp; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(7); | ||
} | ||
} | ||
|
||
readonly class B4 extends A { | ||
// The second write is not from the constructor. It is an error, but it is handled by different rule. | ||
public int $promotedProp; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(7); | ||
} | ||
|
||
public function set(): void | ||
{ | ||
$this->promotedProp = 7; | ||
} | ||
} |
Oops, something went wrong.