Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/Rules/Properties/AccessPropertiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
if ($maybePropertyReflection !== null && $maybePropertyReflection->isDummy()->no()) {
return [];
}

if ($type->getObjectClassNames() === []) {
return [];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class AnalyserWithCheckDynamicPropertiesTest extends PHPStanTestCase
public function testBug13529(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13529.php');
$this->assertCount(1, $errors);
$this->assertSame('Access to an undefined property object::$bar.', $errors[0]->getMessage());
$this->assertSame(8, $errors[0]->getLine());
$this->assertCount(0, $errors);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,4 +1272,18 @@ public function testBug13537(): void
$this->analyse([__DIR__ . '/data/bug-13537.php'], $errors);
}

public function testBug13539(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/bug-13539.php'], [
[
'Access to an undefined property object::$baz.',
29,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
]);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13539.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

function broken(string $x): void {
$tmp = json_decode($x, false);
if (!isset($tmp->foo) || !isset($tmp->bar)) {
}
}

function works(string $x): void {
$tmp = json_decode($x, false);
if (!isset($tmp->foo, $tmp->bar)) {
}
}

function works_too(string $x): void {
/** @var stdClass $tmp */
$tmp = json_decode($x, false);
if (!isset($tmp->foo) || !isset($tmp->bar)) {
}
}

/**
* @param mixed $tmp
*/
function also_ok($tmp): void {
if (isset($tmp->foo) && isset($tmp->bar)) {
echo $tmp->foo;
echo $tmp->bar;
echo $tmp->baz; // intentional: baz not checked by isset
}
}
Loading