Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Node/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PHPStan\Node\Printer;

use Override;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\BooleanAndNode;
Expand Down Expand Up @@ -31,6 +34,7 @@
use PHPStan\Node\MethodCallableNode;
use PHPStan\Node\StaticMethodCallableNode;
use PHPStan\Type\VerbosityLevel;
use function preg_match;
use function sprintf;

/**
Expand All @@ -40,6 +44,25 @@
final class Printer extends Standard
{

/**
* Normalize curly-brace member access with a constant string name to the
* bareword form, so that e.g. `$obj->{'n'}` and `$obj->n` (or `$obj->{'n'}()`
* and `$obj->n()`) produce identical expression keys and are treated as the
* same member by the analyser.
*/
#[Override]
protected function pObjectProperty(Node $node): string
{
if (
$node instanceof String_
&& preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $node->value) === 1
) {
return $node->value;
}

return parent::pObjectProperty($node);
}

protected function pPHPStan_Node_TypeExpr(TypeExpr $expr): string // phpcs:ignore
{
return sprintf('__phpstanType(%s)', $expr->getExprType()->describe(VerbosityLevel::precise()));
Expand Down
51 changes: 51 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14847.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace Bug14847;

use function PHPStan\Testing\assertType;

class Foo
{

public ?string $n = null;

public static ?string $s = null;

}

function narrowFromBareword(Foo $obj): void
{
if ($obj->n !== null) {
assertType('string', $obj->n);
assertType('string', $obj->{'n'});
}
}

function narrowFromCurly(Foo $obj): void
{
if ($obj->{'n'} !== null) {
assertType('string', $obj->{'n'});
assertType('string', $obj->n);
}
}

function narrowStaticProperty(): void
{
if (Foo::$s !== null) {
assertType('string', Foo::$s);
assertType('string', Foo::${'s'});
}

if (Foo::${'s'} !== null) {
assertType('string', Foo::${'s'});
assertType('string', Foo::$s);
}
}

function narrowNullsafe(?Foo $obj): void
{
if ($obj?->n !== null) {
assertType('string', $obj->n);
assertType('string', $obj->{'n'});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,18 @@ public function testBug14791(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14791.php'], []);
}

public function testBug14847(): void
{
$this->analyse([__DIR__ . '/data/bug-14847.php'], [
[
'Strict comparison using === between string and null will always evaluate to false.',
18,
],
[
'Strict comparison using === between string and null will always evaluate to false.',
29,
],
]);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-14847.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug14847Comparison;

class Foo
{

public ?string $n = null;

}

function f(Foo $obj): void
{
if ($obj->n === null) {
return;
}

if ($obj->{'n'} === null) {
echo 'dead';
}
}

function g(Foo $obj): void
{
if ($obj->{'n'} === null) {
return;
}

if ($obj->n === null) {
echo 'dead';
}
}
Loading