1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Allow new on objects

Fixes vimeo/psalm#8355
This commit is contained in:
Bruce Weirdan 2022-11-19 16:53:07 -04:00
parent 4e17585093
commit d7561919b2
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 61 additions and 0 deletions

View File

@ -865,6 +865,11 @@ class NewAnalyzer extends CallAnalyzer
&& $stmt_class_type->ignore_nullable_issues
) {
// do nothing
} elseif ($lhs_type_part instanceof TObject
|| $lhs_type_part instanceof TNamedObject
) {
$new_type = Type::combineUnionTypes($new_type, new Union([$lhs_type_part]));
continue;
} elseif (IssueBuffer::accepts(
new UndefinedClass(
'Type ' . $lhs_type_part . ' cannot be called as a class',

View File

@ -795,6 +795,62 @@ class ClassTest extends TestCase
abstract class C implements I {}
',
],
'newOnNamedObject' => [
'code' => '<?php
$o = new stdClass;
$o2 = new $o;
',
'assertions' => [
'$o2===' => 'stdClass',
],
],
'newOnObject' => [
'code' => '<?php
/** @var object $o */;
$o2 = new $o;
',
'assertions' => [
'$o2===' => 'object',
],
],
'newOnObjectOfAnonymousClass' => [
'code' => '<?php
function f(): object {
$o = new class {};
return new $o;
}
',
],
'newOnObjectOfAnonymousExtendingNamed' => [
'code' => '<?php
function f(): Exception {
$o = new class extends Exception {};
return new $o;
}
',
],
'newOnObjectOfAnonymousClassImplementingNamed' => [
'code' => '<?php
interface I {}
function f(): I {
$o = new class implements I {};
return new $o;
}
',
],
'throwAnonymousObjects' => [
'code' => '<?php
throw new class extends Exception {};
',
],
'throwTheResultOfNewOnAnAnonymousClass' => [
'code' => '<?php
declare(strict_types=1);
$test = new class extends \Exception { };
throw new $test();
'
]
];
}