1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Allow use of namespaced Resource class

Ref #2052
This commit is contained in:
Matthew Brown 2019-08-26 00:08:18 -04:00
parent 03f59ae05e
commit 781e711243
3 changed files with 41 additions and 6 deletions

View File

@ -116,10 +116,12 @@ class ClassAnalyzer extends ClassLikeAnalyzer
return;
}
if ($class->name && preg_match(
'/(^|\\\)(int|float|bool|string|void|null|false|true|resource|object|numeric|mixed)$/i',
$fq_class_name
)) {
if ($class->name
&& (preg_match(
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|numeric|mixed)$/i',
$fq_class_name
) || strtolower($fq_class_name) === 'resource')
) {
$class_name_parts = explode('\\', $fq_class_name);
$class_name = array_pop($class_name_parts);

View File

@ -235,9 +235,9 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSou
}
if (preg_match(
'/(^|\\\)(int|float|bool|string|void|null|false|true|resource|object|numeric|mixed)$/i',
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|numeric|mixed)$/i',
$fq_class_name
)
) || strtolower($fq_class_name) === 'resource'
) {
$class_name_parts = explode('\\', $fq_class_name);
$class_name = array_pop($class_name_parts);

View File

@ -628,6 +628,39 @@ class ReturnTypeTest extends TestCase
}
}',
],
'allowResourceInNamespace' => [
'<?php
namespace Bar;
class Resource {
function get(string $key): ?string {
return "";
}
}
class Foo {
/** @var string[] */
private $references = [];
/** @var Resource */
private $resource;
public function __construct() {
$this->resource = new Resource();
}
public function foo(): array {
$types = [];
foreach ($this->references as $ref => $data) {
$types[$ref] = $this->resource->get($data);
}
return $types;
}
}',
],
];
}