Skip to content

Commit 6f59d4e

Browse files
committed
add set
1 parent a1414e2 commit 6f59d4e

12 files changed

Lines changed: 117 additions & 111 deletions

File tree

config/set/type-declaration-docblocks.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
7-
use Rector\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
6+
use Rector\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
7+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
88

99
return static function (RectorConfig $rectorConfig): void {
1010
// 2025-09, experimental hidden set for type declaration in docblocks
1111

1212
$rectorConfig->rules([
13-
// properties
13+
// property
1414
DocblockVarFromParamDocblockInConstructorRector::class,
1515

1616
DocblockGetterReturnArrayFromPropertyDocblockVarRector::class,

rules-tests/TypeDeclaration/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/DocblockGetterReturnArrayFromPropertyDocblockVarRectorTest.php renamed to rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/DocblockGetterReturnArrayFromPropertyDocblockVarRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/TypeDeclaration/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/skip_missing_return_type.php.inc renamed to rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/skip_missing_return_type.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
44

55
final class SkipMissingPropertyType
66
{

rules-tests/TypeDeclaration/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/some_property_with_array_docblock.php.inc renamed to rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/some_property_with_array_docblock.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
44

55
final class SomePropertyWithArrayDocblock
66
{

rules-tests/TypeDeclaration/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/config/configured_rule.php renamed to rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
6+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
77

88
return RectorConfig::configure()
99
->withRules([DocblockGetterReturnArrayFromPropertyDocblockVarRector::class]);

rules-tests/TypeDeclaration/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/DocblockVarFromParamDocblockInConstructorRectorTest.php renamed to rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/DocblockVarFromParamDocblockInConstructorRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/TypeDeclaration/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/Fixture/skip_if_array_type_missing.php.inc renamed to rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/Fixture/skip_if_array_type_missing.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
44

55
final class SkipIfArrayTypeMissing
66
{

rules-tests/TypeDeclaration/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/Fixture/some_class.php.inc renamed to rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/Fixture/some_class.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
44

55
final class SomeClass
66
{

rules-tests/TypeDeclaration/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/config/configured_rule.php renamed to rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarFromParamDocblockInConstructorRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
6+
use Rector\TypeDeclarationDocblocks\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
77

88
return static function (RectorConfig $rectorConfig): void {
99
$rectorConfig->rule(DocblockVarFromParamDocblockInConstructorRector::class);

rules/TypeDeclaration/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php renamed to rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\TypeDeclaration\Rector\ClassMethod;
5+
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\PropertyFetch;
@@ -33,6 +33,46 @@ public function getNodeTypes(): array
3333
return [ClassMethod::class];
3434
}
3535

36+
public function getRuleDefinition(): RuleDefinition
37+
{
38+
return new RuleDefinition('Add @return array docblock to a getter method based on @var of the property', [
39+
new CodeSample(
40+
<<<'CODE_SAMPLE'
41+
class SomeClass
42+
{
43+
/**
44+
* @var int[]
45+
*/
46+
private array $items;
47+
48+
public function getItems(): array
49+
{
50+
return $this->items;
51+
}
52+
}
53+
CODE_SAMPLE
54+
,
55+
<<<'CODE_SAMPLE'
56+
class SomeClass
57+
{
58+
/**
59+
* @var int[]
60+
*/
61+
private array $items;
62+
63+
/**
64+
* @return int[]
65+
*/
66+
public function getItems(): array
67+
{
68+
return $this->items;
69+
}
70+
}
71+
CODE_SAMPLE
72+
),
73+
]);
74+
}
75+
3676
/**
3777
* @param ClassMethod $node
3878
*/
@@ -53,16 +93,30 @@ public function refactor(Node $node): ?Node
5393
return null;
5494
}
5595

56-
if ($node->stmts === null) {
96+
$propertyFetch = $this->matchReturnLocalPropertyFetch($node);
97+
if (! $propertyFetch instanceof PropertyFetch) {
5798
return null;
5899
}
59100

101+
$propertyFetchType = $this->getType($propertyFetch);
102+
$propertyFetchDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($propertyFetchType);
103+
104+
$returnTagValueNode = new ReturnTagValueNode($propertyFetchDocTypeNode, '');
105+
$phpDocInfo->addTagValueNode($returnTagValueNode);
106+
107+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
108+
109+
return $node;
110+
}
111+
112+
private function matchReturnLocalPropertyFetch(ClassMethod $classMethod): ?PropertyFetch
113+
{
60114
// we need exactly one statement of return
61-
if (count($node->stmts) !== 1) {
115+
if ($classMethod->stmts === null || count($classMethod->stmts) !== 1) {
62116
return null;
63117
}
64118

65-
$onlyStmt = $node->stmts[0];
119+
$onlyStmt = $classMethod->stmts[0];
66120
if (! $onlyStmt instanceof Return_) {
67121
return null;
68122
}
@@ -76,55 +130,6 @@ public function refactor(Node $node): ?Node
76130
return null;
77131
}
78132

79-
$propertyFetchType = $this->getType($propertyFetch);
80-
81-
$propertyFetchDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($propertyFetchType);
82-
83-
$returnTagValueNode = new ReturnTagValueNode($propertyFetchDocTypeNode, '');
84-
$phpDocInfo->addTagValueNode($returnTagValueNode);
85-
86-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
87-
88-
return $node;
89-
}
90-
91-
public function getRuleDefinition(): RuleDefinition
92-
{
93-
return new RuleDefinition('Add @return array docblock to a getter method based on @var of the property', [
94-
new CodeSample(
95-
<<<'CODE_SAMPLE'
96-
class SomeClass
97-
{
98-
/**
99-
* @var int[]
100-
*/
101-
private array $items;
102-
103-
public function getItems(): array
104-
{
105-
return $this->items;
106-
}
107-
}
108-
CODE_SAMPLE
109-
,
110-
<<<'CODE_SAMPLE'
111-
class SomeClass
112-
{
113-
/**
114-
* @var int[]
115-
*/
116-
private array $items;
117-
118-
/**
119-
* @return int[]
120-
*/
121-
public function getItems(): array
122-
{
123-
return $this->items;
124-
}
125-
}
126-
CODE_SAMPLE
127-
),
128-
]);
133+
return $propertyFetch;
129134
}
130135
}

0 commit comments

Comments
 (0)