Skip to content

Commit 33bb8a6

Browse files
authored
[TypeDeclarationDocblocks] Fix missing backslash on class-string type on DocblockGetterReturnArrayFromPropertyDocblockVarRector (#7454)
* [TypeDeclarationDocblocks] Fix missing backslash on class-string type on DocblockGetterReturnArrayFromPropertyDocblockVarRector * fix
1 parent d416b99 commit 33bb8a6

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/class_string_case_sensitive.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClassStringCaseSensitive
1717
class ClassStringCaseSensitive
1818
{
1919
/**
20-
* @var class-string<ClassStringCaseSensitive>
20+
* @var class-string<\ClassStringCaseSensitive>
2121
*/
2222
public $value;
2323
public function set()

rules-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/phpdocs.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ final class PhpDocs
8383

8484
/**
8585
* @param class-string<SomeInterface> $class
86-
* @return class-string<Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface>
86+
* @return class-string<\Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface>
8787
*/
8888
public function bar(string $class): string
8989
{
9090
return $class;
9191
}
9292

93-
/** @return class-string<Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface> */
93+
/** @return class-string<\Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface> */
9494
public function baz(string $class): string
9595
{
9696
return SomeInterface::class;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
4+
5+
use App\Support\Benchmarking\Contracts\Metric;
6+
use LogicException;
7+
8+
class AddPrefixBackslash extends LogicException
9+
{
10+
public function __construct(
11+
/** @var array<string, array<int, class-string<Metric>>> */
12+
protected array $duplicateNames,
13+
) {
14+
parent::__construct(
15+
'Some solutions have duplicate names.',
16+
);
17+
}
18+
19+
public function getDuplicateNames(): array
20+
{
21+
return $this->duplicateNames;
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
30+
31+
use App\Support\Benchmarking\Contracts\Metric;
32+
use LogicException;
33+
34+
class AddPrefixBackslash extends LogicException
35+
{
36+
public function __construct(
37+
/** @var array<string, array<int, class-string<Metric>>> */
38+
protected array $duplicateNames,
39+
) {
40+
parent::__construct(
41+
'Some solutions have duplicate names.',
42+
);
43+
}
44+
45+
/**
46+
* @return array<string, class-string<\App\Support\Benchmarking\Contracts\Metric>[]>
47+
*/
48+
public function getDuplicateNames(): array
49+
{
50+
return $this->duplicateNames;
51+
}
52+
}
53+
54+
?>

src/PHPStanStaticTypeMapper/TypeMapper/ClassStringTypeMapper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PhpParser\Node\Identifier;
99
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1010
use PHPStan\Type\ClassStringType;
11+
use PHPStan\Type\ObjectType;
1112
use PHPStan\Type\Type;
13+
use PHPStan\Type\TypeTraverser;
1214
use Rector\Php\PhpVersionProvider;
1315
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
1416
use Rector\ValueObject\PhpVersionFeature;
@@ -33,6 +35,20 @@ public function getNodeClass(): string
3335
*/
3436
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
3537
{
38+
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
39+
if (! $type instanceof ObjectType) {
40+
return $traverse($type);
41+
}
42+
43+
$typeClass = $type::class;
44+
45+
if ($typeClass === 'PHPStan\Type\ObjectType') {
46+
return new ObjectType('\\' . $type->getClassName());
47+
}
48+
49+
return $traverse($type);
50+
});
51+
3652
return $type->toPhpDocNode();
3753
}
3854

0 commit comments

Comments
 (0)