Skip to content

Commit 180d36c

Browse files
authored
[TypeDeclarationDocblocks] Handle multiple objects on AddReturnDocblockDataProviderRector (#7298)
* [TypeDeclarationDocblocks] Handle multiple objects on AddReturnDocblockDataProviderRector * Fix * update complex array
1 parent 76dc419 commit 180d36c

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/Nesting/complex_array.php.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ComplexArray
88
{
99
return [
1010
'id' => new \stdClass(),
11+
'other' => false,
1112
'context' => [
1213
'id' => 1,
1314
'data' => [
@@ -17,6 +18,11 @@ class ComplexArray
1718
],
1819
'values' => [
1920
'name' => '111',
21+
'age' => 1,
22+
],
23+
'values_other' => [
24+
'name' => '111',
25+
'age' => 1,
2026
],
2127
];
2228
}
@@ -37,6 +43,7 @@ class ComplexArray
3743
{
3844
return [
3945
'id' => new \stdClass(),
46+
'other' => false,
4047
'context' => [
4148
'id' => 1,
4249
'data' => [
@@ -46,6 +53,11 @@ class ComplexArray
4653
],
4754
'values' => [
4855
'name' => '111',
56+
'age' => 1,
57+
],
58+
'values_other' => [
59+
'name' => '111',
60+
'age' => 1,
4961
],
5062
];
5163
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
4+
5+
use DateTime;
6+
use DateTimeImmutable;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class MultiObjects extends TestCase
11+
{
12+
#[DataProvider('provideData')]
13+
public function testSomething()
14+
{
15+
}
16+
17+
public static function provideData(): iterable
18+
{
19+
yield [[new DateTime(), 'format']];
20+
yield [[new DateTimeImmutable(), 'format']];
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
29+
30+
use DateTime;
31+
use DateTimeImmutable;
32+
use PHPUnit\Framework\Attributes\DataProvider;
33+
use PHPUnit\Framework\TestCase;
34+
35+
final class MultiObjects extends TestCase
36+
{
37+
#[DataProvider('provideData')]
38+
public function testSomething()
39+
{
40+
}
41+
42+
/**
43+
* @return \Iterator<array<int, array<int, (\DateTime | \DateTimeImmutable | string)>>>
44+
*/
45+
public static function provideData(): iterable
46+
{
47+
yield [[new DateTime(), 'format']];
48+
yield [[new DateTimeImmutable(), 'format']];
49+
}
50+
}
51+
52+
?>

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* @var int
2929
*/
30-
private const MAX_PRINTED_UNION_DOC_LENGHT = 60;
30+
private const MAX_PRINTED_UNION_DOC_LENGHT = 77;
3131

3232
public function __construct(
3333
private TypeFactory $typeFactory,

src/NodeTypeResolver/PHPStan/Type/TypeFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ private function unwrapConstantArrayTypes(ConstantArrayType $constantArrayType):
205205
$nestedFlattenKeyType = new MixedType();
206206
}
207207

208+
if ($nestedFlattenItemType instanceof ConstantArrayType) {
209+
$innerArrayTypes = $this->unwrapConstantArrayTypes($nestedFlattenItemType);
210+
foreach ($innerArrayTypes as $innerArrayType) {
211+
// preserve outer array -> inner array structure: array<outerKey, innerArray>
212+
$unwrappedTypes[] = new ArrayType($nestedFlattenKeyType, $innerArrayType);
213+
}
214+
215+
continue;
216+
}
217+
208218
$unwrappedTypes[] = new ArrayType($nestedFlattenKeyType, $nestedFlattenItemType);
209219
}
210220

0 commit comments

Comments
 (0)