Skip to content
Permalink
Browse files

Fix #2494 - don’t use extended name for anonymous classes

  • Loading branch information
muglug committed Dec 31, 2019
1 parent 87fec3e commit 5bd9b988fb9f136763d4b294c290ed4b2aae1379
@@ -315,7 +315,14 @@ public static function analyze(
}
}

$statements_analyzer->node_data->setType($stmt, new Type\Union([new TNamedObject($fq_class_name)]));
$statements_analyzer->node_data->setType(
$stmt,
new Type\Union([
$stmt->class instanceof PhpParser\Node\Stmt\Class_
? new Type\Atomic\TAnonymousClassInstance($fq_class_name)
: new TNamedObject($fq_class_name)
])
);

if (strtolower($fq_class_name) !== 'stdclass' &&
$codebase->classlikes->classExists($fq_class_name)
@@ -0,0 +1,32 @@
<?php
namespace Psalm\Type\Atomic;

class TAnonymousClassInstance extends TNamedObject
{
public function toPhpString(
$namespace,
array $aliased_classes,
$this_class,
$php_major_version,
$php_minor_version
) {
return $php_major_version >= 7 && $php_minor_version >= 2 ? 'object' : null;
}

/**
* @param string|null $namespace
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'object';
}
}
@@ -766,6 +766,35 @@ function foo($t) {
['MissingReturnType'],
true
],
'missingReturnTypeAnonymousClass' => [
'<?php
function logger() {
return new class {};
}',
'<?php
function logger(): object {
return new class {};
}',
'7.4',
['MissingReturnType'],
true
],
'missingReturnTypeAnonymousClassPre72' => [
'<?php
function logger() {
return new class {};
}',
'<?php
/**
* @return object
*/
function logger() {
return new class {};
}',
'7.1',
['MissingReturnType'],
true
],
'fixInvalidIntReturnType56' => [
'<?php
/**

0 comments on commit 5bd9b98

Please sign in to comment.
You can’t perform that action at this time.