Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@ public function enterPropertyHook(

$realParameterTypes = $this->getRealParameterTypes($hook);

return $this->enterFunctionLike(
$scope = $this->enterFunctionLike(
new PhpMethodFromParserNodeReflection(
$this->getClassReflection(),
$hook,
Expand Down Expand Up @@ -1606,6 +1606,17 @@ public function enterPropertyHook(
),
true,
);

if ($hookName === 'set') {
foreach ($this->getClassReflection()->getNativeReflection()->getProperties() as $nativeProperty) {
if ($nativeProperty->hasDefaultValue()) {
continue;
}
$scope = $scope->invalidateExpression(new PropertyInitializationExpr($nativeProperty->getName()));
}
}

return $scope;
}

private function transformStaticType(Type $type): Type
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,23 @@ public function testBug9503(): void
$this->analyse([__DIR__ . '/data/bug-9503.php'], []);
}

#[RequiresPhp('>= 8.4')]
public function testBug13473(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-13473.php'], [
[
'Property Bug13473\Bar::$bar in isset() is not nullable nor uninitialized.',
30,
],
[
'Property Bug13473\Baz::$foo (int) in isset() is not nullable.',
67,
],
]);
}

public function testBug14393(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
81 changes: 81 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13473.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php // lint >= 8.4

declare(strict_types = 1);

namespace Bug13473;

class Foo {
private(set) int $bar {
get => $this->bar;
set(int $bar) {
if (isset($this->bar)) {
throw new \Exception('bar is set');
}
$this->bar = $bar;
}
}

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

$foo = new Foo(10);

class Bar {
private(set) int $bar = 1 {
get => $this->bar;
set(int $bar) {
if (isset($this->bar)) {
throw new \Exception('bar is set');
}
$this->bar = $bar;
}
}

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

class Qux {
private(set) int $foo;
private(set) int $bar {
get => $this->bar;
set(int $bar) {
if (isset($this->foo)) { // $foo has no default, could be uninitialized - no error
throw new \Exception('foo is set');
}
$this->bar = $bar;
}
}

public function __construct(int $bar)
{
$this->bar = $bar;
$this->foo = 42;
}
}

class Baz {
private(set) int $foo = 5;
private(set) int $bar {
get => $this->bar;
set(int $bar) {
if (isset($this->foo)) { // $foo has default value, always initialized - should error
echo 'foo is set';
}
if (isset($this->bar)) { // $bar has no default, could be uninitialized - no error
throw new \Exception('bar is set');
}
$this->bar = $bar;
}
}

public function __construct(int $bar)
{
$this->bar = $bar;
}
}
Loading