Skip to content

Commit

Permalink
Merge pull request #7542 from klimick/fix-object-const-inference
Browse files Browse the repository at this point in the history
Fix object constant inference
  • Loading branch information
orklah committed Jan 31, 2022
2 parents 69e8815 + e284b91 commit 041145d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Expand Up @@ -431,6 +431,18 @@ public static function infer(
}
}

if ($stmt instanceof PhpParser\Node\Expr\New_) {
$resolved_class_name = $stmt->class->getAttribute('resolvedName');

if (!is_string($resolved_class_name)) {
return null;
}

return new Union([
new Type\Atomic\TNamedObject($resolved_class_name)
]);
}

return null;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/ConstantTest.php
Expand Up @@ -2,14 +2,55 @@

namespace Psalm\Tests;

use Psalm\Context;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

use function getcwd;

use const DIRECTORY_SEPARATOR;

class ConstantTest extends TestCase
{
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;

public function testUseObjectConstant(): void
{
$file1 = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'file1.php';
$file2 = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'file2.php';

$this->addFile(
$file1,
'<?php
namespace Foo;
final class Bar {}
const bar = new Bar();
'
);

$this->addFile(
$file2,
'<?php
namespace Baz;
use Foo\Bar;
use const Foo\bar;
require("tests/file1.php");
function bar(): Bar
{
return bar;
}
'
);

$this->analyzeFile($file1, new Context());
$this->analyzeFile($file2, new Context());
}

/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[], php_version?: string}>
*/
Expand Down

0 comments on commit 041145d

Please sign in to comment.