Skip to content

Commit daf4cf0

Browse files
committed
[type-declaration-docblock] kick of docblock rules to help with iterables
1 parent 4099408 commit daf4cf0

11 files changed

Lines changed: 508 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
7+
use Rector\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
// 2025-09, experimental hidden set for type declaration in docblocks
11+
12+
$rectorConfig->rules([
13+
// properties
14+
DocblockVarFromParamDocblockInConstructorRector::class,
15+
16+
DocblockGetterReturnArrayFromPropertyDocblockVarRector::class,
17+
]);
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DocblockGetterReturnArrayFromPropertyDocblockVarRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
4+
5+
final class SkipMissingPropertyType
6+
{
7+
/**
8+
* @var string[]
9+
*/
10+
private array $names = [];
11+
12+
public function getNames()
13+
{
14+
return $this->names;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
4+
5+
final class SomePropertyWithArrayDocblock
6+
{
7+
/**
8+
* @var string[]
9+
*/
10+
private array $names = [];
11+
12+
public function getNames(): array
13+
{
14+
return $this->names;
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
23+
24+
final class SomePropertyWithArrayDocblock
25+
{
26+
/**
27+
* @var string[]
28+
*/
29+
private array $names = [];
30+
31+
/**
32+
* @return string[]
33+
*/
34+
public function getNames(): array
35+
{
36+
return $this->names;
37+
}
38+
}
39+
40+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([DocblockGetterReturnArrayFromPropertyDocblockVarRector::class]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DocblockVarFromParamDocblockInConstructorRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
4+
5+
final class SkipIfArrayTypeMissing
6+
{
7+
private $names = [];
8+
9+
/**
10+
* @param string[] $names
11+
*/
12+
public function __construct(array $names)
13+
{
14+
$this->names = $names;
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
4+
5+
final class SomeClass
6+
{
7+
private array $names = [];
8+
9+
/**
10+
* @param string[] $names
11+
*/
12+
public function __construct(array $names)
13+
{
14+
$this->names = $names;
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector\Fixture;
23+
24+
final class SomeClass
25+
{
26+
/**
27+
* @var string[]
28+
*/
29+
private array $names = [];
30+
31+
/**
32+
* @param string[] $names
33+
*/
34+
public function __construct(array $names)
35+
{
36+
$this->names = $names;
37+
}
38+
}
39+
40+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\Class_\DocblockVarFromParamDocblockInConstructorRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DocblockVarFromParamDocblockInConstructorRector::class);
10+
};
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclaration\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\PropertyFetch;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Return_;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
12+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
13+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
14+
use Rector\Rector\AbstractRector;
15+
use Rector\StaticTypeMapper\StaticTypeMapper;
16+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
17+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
19+
/**
20+
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\DocblockGetterReturnArrayFromPropertyDocblockVarRectorTest
21+
*/
22+
final class DocblockGetterReturnArrayFromPropertyDocblockVarRector extends AbstractRector
23+
{
24+
public function __construct(
25+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
26+
private readonly DocBlockUpdater $docBlockUpdater,
27+
private readonly StaticTypeMapper $staticTypeMapper
28+
) {
29+
}
30+
31+
public function getNodeTypes(): array
32+
{
33+
return [ClassMethod::class];
34+
}
35+
36+
/**
37+
* @param ClassMethod $node
38+
*/
39+
public function refactor(Node $node): ?Node
40+
{
41+
if (! $node->returnType instanceof Node) {
42+
return null;
43+
}
44+
45+
if (! $this->isName($node->returnType, 'array')) {
46+
return null;
47+
}
48+
49+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
50+
51+
// return tag is already given
52+
if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
53+
return null;
54+
}
55+
56+
if ($node->stmts === null) {
57+
return null;
58+
}
59+
60+
// we need exactly one statement of return
61+
if (count($node->stmts) !== 1) {
62+
return null;
63+
}
64+
65+
$onlyStmt = $node->stmts[0];
66+
if (! $onlyStmt instanceof Return_) {
67+
return null;
68+
}
69+
70+
if (! $onlyStmt->expr instanceof PropertyFetch) {
71+
return null;
72+
}
73+
74+
$propertyFetch = $onlyStmt->expr;
75+
if (! $this->isName($propertyFetch->var, 'this')) {
76+
return null;
77+
}
78+
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+
]);
129+
}
130+
}

0 commit comments

Comments
 (0)