Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit @immutable phpdoc #1489

Merged
merged 2 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,11 @@ public function isImmutable(): bool
if ($this->isImmutable === null) {
$resolvedPhpDoc = $this->getResolvedPhpDoc();
$this->isImmutable = $resolvedPhpDoc !== null && $resolvedPhpDoc->isImmutable();

$parentClass = $this->getParentClass();
if ($parentClass !== null && !$this->isImmutable) {
$this->isImmutable = $parentClass->isImmutable();
}
}

return $this->isImmutable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ public function testRule(): void
'@readonly property MissingReadOnlyPropertyAssignPhpDoc\FooTraitClass::$doubleAssigned is already assigned.',
192,
],
[
'Class MissingReadOnlyPropertyAssignPhpDoc\A has an uninitialized @readonly property $a. Assign it in the constructor.',
233,
],
[
'Class MissingReadOnlyPropertyAssignPhpDoc\B has an uninitialized @readonly property $b. Assign it in the constructor.',
240,
],
[
'Access to an uninitialized @readonly property MissingReadOnlyPropertyAssignPhpDoc\B::$b.',
244,
],
[
'@readonly property MissingReadOnlyPropertyAssignPhpDoc\C::$c is already assigned.',
257,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public function testRule(): void
'@readonly property ReadOnlyPropertyAssignRefPhpDoc\Immutable::$bar is assigned by reference.',
52,
],
[
'@readonly property ReadOnlyPropertyAssignRefPhpDoc\A::$a is assigned by reference.',
66,
],
[
'@readonly property ReadOnlyPropertyAssignRefPhpDoc\B::$b is assigned by reference.',
79,
],
[
'@readonly property ReadOnlyPropertyAssignRefPhpDoc\A::$a is assigned by reference.',
80,
],
[
'@readonly property ReadOnlyPropertyAssignRefPhpDoc\C::$c is assigned by reference.',
93,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public function testRule(): void
'@readonly property ReadonlyPropertyAssignPhpDoc\Immutable::$foo is assigned outside of the constructor.',
227,
],
[
'@readonly property ReadonlyPropertyAssignPhpDoc\B::$b is assigned outside of the constructor.',
259,
],
[
'@readonly property ReadonlyPropertyAssignPhpDoc\A::$a is assigned outside of its declaring class.',
260,
],
[
'@readonly property ReadonlyPropertyAssignPhpDoc\C::$c is assigned outside of the constructor.',
273,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public function testRule(): void
'@readonly property cannot have a default value.',
21,
],
[
'@readonly property cannot have a default value.',
39,
],
[
'@readonly property cannot have a default value.',
46,
],
[
'@readonly property cannot have a default value.',
53,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,36 @@ class BarClass
use BarTrait;

}

/** @immutable */
class A
{

public string $a;

}

class B extends A
{

public string $b;

public function __construct()
{
$b = $this->b;
}

}

class C extends B
{

public string $c;

public function __construct()
{
$this->c = '';
$this->c = '';
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Properties/data/read-only-property-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,25 @@ public function __construct(
{
}
}

/** @immutable */
class A
{

public string $a = '';

}

class B extends A
{

public string $b = '';

}

class C extends B
{

public string $c = '';

}
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,49 @@ public function setFoo(int $foo): void
}

}

/** @immutable */
class A
{

/** @var string */
public $a;

public function __construct() {
$this->a = ''; // constructor - fine
}

}

class B extends A
{

/** @var string */
public $b;

public function __construct()
{
parent::__construct();
$this->b = ''; // constructor - fine
}

public function mod()
{
$this->b = 'input'; // setter - report
$this->a = 'input2'; // setter - report
}

}

class C extends B
{

/** @var string */
public $c;

public function mod()
{
$this->c = 'input'; // setter - report
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign-ref-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,44 @@ public function doFoo()
}

}

/** @immutable */
class A
{

/** @var string */
public $a;

public function mod()
{
$a = &$this->a;
}

}

class B extends A
{

/** @var string */
public $b;

public function mod()
{
$b = &$this->b;
$a = &$this->a;
}

}

class C extends B
{

/** @var string */
public $c;

public function mod()
{
$c = &$this->c;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@ public function __construct()
}

}

/** @immutable */
class A
{

public string $a;

}

class B extends A
{

public string $b;

}

class C extends B
{

public string $c;

}