Skip to content

Commit 4e99a1b

Browse files
authored
Use PHPStan Type::toNativePhpTypeNode() and Type::toPhpDocNode() instead of own type mappers (#8885)
* Use PHPStan Type::toNativePhpTypeNode() instead of mapToPhpParserNode() Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce * Use PHPStan Type::toPhpDocNode() instead of PHPStanStaticTypeMapper PHPStan now returns phpdoc-parser 2.4's FullyQualifiedNameNode for class names from Type::toPhpDocNode(), so Rector no longer needs its own mapToPHPStanPhpDocTypeNode() mappers to tell class names from keywords. The PHPStanStaticTypeMapper package is removed. StaticTypeMapper keeps Rector's docblock style (A|B, A[], plain int) on top of toPhpDocNode(). Rector's shortened and aliased object types return a RelativeNameNode. The name importing and class renaming PHPDoc visitors also handle name nodes, and code reading the name of a generic or callable type node no longer assumes an IdentifierTypeNode. Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce
1 parent 9f5cad7 commit 4e99a1b

77 files changed

Lines changed: 377 additions & 2813 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎UPGRADING.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Upgrading to the next Rector release
2+
3+
* `TypeMapperInterface::mapToPhpParserNode()` is removed; custom type mappers only map to PHPDoc nodes now. A custom `Type` controls its native declaration by overriding `toNativePhpTypeNode()`.
4+
15
# Upgrading from Rector 2.2.14 to 2.3
26

37
* `FileWithoutNamespace` is deprecated, and replaced by `FileNode` that represents both namespaced and non-namespaced files and allow changes inside

‎phpstan.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,6 @@ parameters:
284284
paths:
285285
- src/NodeTypeResolver/PHPStan/Type/TypeFactory.php
286286

287-
# from mapper interface
288-
-
289-
identifier: return.unusedType
290-
message: '#mapToPhpParserNode\(\) never returns PhpParser\\.* so it can be removed from the return#'
291-
292287
# false positive
293288
-
294289
identifier: return.unusedType
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;
6+
7+
final class HasPropertyType
8+
{
9+
/**
10+
* @param mixed $data
11+
*/
12+
public function run($data)
13+
{
14+
if (! isset($data->type)) {
15+
return;
16+
}
17+
18+
if ($data->type === 'value') {
19+
$this->runData($data);
20+
}
21+
}
22+
23+
private function runData($data)
24+
{
25+
}
26+
}
27+
-----
28+
<?php
29+
30+
declare(strict_types=1);
31+
32+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;
33+
34+
final class HasPropertyType
35+
{
36+
/**
37+
* @param mixed $data
38+
*/
39+
public function run($data)
40+
{
41+
if (! isset($data->type)) {
42+
return;
43+
}
44+
45+
if ($data->type === 'value') {
46+
$this->runData($data);
47+
}
48+
}
49+
50+
private function runData(object $data)
51+
{
52+
}
53+
}

‎rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_has_property_type.php.inc‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use stdClass;
3333

3434
final class DifferentUsage {
3535
/**
36-
* @param \stdClass[]|\DateTime[] $items
36+
* @param \DateTime[]|\stdClass[] $items
3737
*/
3838
public function process(array $items): void
3939
{

‎rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use stdClass;
3333
final class WithArrayFilterAndArrayWalk
3434
{
3535
/**
36-
* @param string[]|\stdClass[] $items
36+
* @param \stdClass[]|string[] $items
3737
*/
3838
public function process(array $items): void
3939
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;
6+
7+
final class ReturnAnonymousClassAndOther
8+
{
9+
public function action()
10+
{
11+
if (rand(0, 1)) {
12+
return new class {};
13+
}
14+
15+
return new static();
16+
}
17+
}
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;
24+
25+
final class ReturnAnonymousClassAndOther
26+
{
27+
public function action(): object
28+
{
29+
if (rand(0, 1)) {
30+
return new class {};
31+
}
32+
33+
return new static();
34+
}
35+
}

‎rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_return_anonymous_class_and_other.php.inc‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;
4+
5+
final class AnonymousClassAndOtherObject
6+
{
7+
public function run()
8+
{
9+
if (rand(0,1)) {
10+
return new SkipAnonymousClassAndOtherObject();
11+
}
12+
13+
return new class {};
14+
}
15+
}
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;
20+
21+
final class AnonymousClassAndOtherObject
22+
{
23+
public function run(): object
24+
{
25+
if (rand(0,1)) {
26+
return new SkipAnonymousClassAndOtherObject();
27+
}
28+
29+
return new class {};
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;
4+
5+
final class AnonymousClassAndOtherObject2
6+
{
7+
public function run()
8+
{
9+
if (rand(0,1)) {
10+
return new SkipAnonymousClassAndOtherObject();
11+
}
12+
13+
$obj = new class {};
14+
return $obj;
15+
}
16+
}
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;
21+
22+
final class AnonymousClassAndOtherObject2
23+
{
24+
public function run(): object
25+
{
26+
if (rand(0,1)) {
27+
return new SkipAnonymousClassAndOtherObject();
28+
}
29+
30+
$obj = new class {};
31+
return $obj;
32+
}
33+
}

0 commit comments

Comments
 (0)