Skip to content

Commit fcd4e6b

Browse files
authored
NarrowPublicClassMethodParamTypeRule: Fix generic-object false-positive (#74)
* NarrowPublicClassMethodParamTypeRule: Fix generic-object false-positive * Update CollectorMetadataPrinter.php * Update CollectorMetadataPrinter.php
1 parent 620a995 commit fcd4e6b

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

src/Printer/CollectorMetadataPrinter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Reflection\ExtendedMethodReflection;
2020
use PHPStan\Reflection\ParametersAcceptorSelector;
2121
use PHPStan\Type\ClosureType;
22+
use PHPStan\Type\Generic\GenericObjectType;
2223
use PHPStan\Type\IntegerRangeType;
2324
use PHPStan\Type\IntersectionType;
2425
use PHPStan\Type\MixedType;
@@ -65,6 +66,10 @@ public function printArgTypesAsString(
6566
return ResolvedTypes::UNKNOWN_TYPES;
6667
}
6768

69+
if ($argType instanceof GenericObjectType) {
70+
return ResolvedTypes::UNKNOWN_TYPES;
71+
}
72+
6873
if ($argType instanceof PHPStanUnionType) {
6974
return ResolvedTypes::UNKNOWN_TYPES;
7075
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Fixture\Generics;
4+
5+
use Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Source\SkipSameGeneric\MyGroup;
6+
use Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Source\SkipSameGeneric\MyIterator;
7+
8+
final class SkipSameGeneric
9+
{
10+
/**
11+
* @param MyIterator<MyGroup> $_werbemasse
12+
*/
13+
public function setWerbemasse($_werbemasse): void
14+
{
15+
$this->_werbemasse = $_werbemasse;
16+
}
17+
}
18+
19+

tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ public static function provideData(): Iterator
160160

161161
$argErrorMessage = sprintf(NarrowPublicClassMethodParamTypeRule::ERROR_MESSAGE, 'bool');
162162
yield [[__DIR__ . '/Fixture/ExplicitlyNullableParams.php'], [[$argErrorMessage, 9]]];
163+
164+
// skip generics
165+
yield [[
166+
__DIR__ . '/Fixture/Generics/SkipSameGeneric.php',
167+
__DIR__ . '/Source/SkipSameGeneric/MyGroup.php',
168+
__DIR__ . '/Source/SkipSameGeneric/MyIterator.php',
169+
__DIR__ . '/Source/SkipSameGeneric/MyService.php',
170+
], []];
163171
}
164172

165173
/**
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+
namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Source\SkipSameGeneric;
6+
7+
final class MyGroup
8+
{
9+
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Source\SkipSameGeneric;
6+
7+
/**
8+
* @template T
9+
*/
10+
final class MyIterator
11+
{
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Source\SkipSameGeneric;
6+
7+
use Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Fixture\Generics\SkipSameGeneric;
8+
9+
final class MyService
10+
{
11+
12+
private SkipSameGeneric $articleModel;
13+
14+
private function populateCustomizationDimensions(int $spracheid): void
15+
{
16+
$this->articleModel->setWerbemasse($this->fetchCustomizationPositionsByArtidAndSprache());
17+
}
18+
19+
/**
20+
* @return MyIterator<MyGroup>
21+
*/
22+
public function fetchCustomizationPositionsByArtidAndSprache(int $artid, int $spracheid): MyIterator
23+
{
24+
}
25+
26+
}

0 commit comments

Comments
 (0)