Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -134,6 +135,10 @@ private function shouldSkipType(Type $newType, ClassMethod $classMethod): bool
if ($this->isNewAndCurrentTypeBothCallable($newType, $classMethod)) {
return true;
}

if ($this->isMixedOfSpecificOverride($newType, $classMethod)) {
return true;
}
}

if ($newType instanceof ConstantArrayType) {
Expand Down Expand Up @@ -167,4 +172,23 @@ private function isNewAndCurrentTypeBothCallable(ArrayType $newArrayType, ClassM

return true;
}

private function isMixedOfSpecificOverride(ArrayType $arrayType, ClassMethod $classMethod): bool
{
if (! $arrayType->getItemType() instanceof MixedType) {
return false;
}

$currentPhpDocInfo = $this->getPhpDocInfo($classMethod);
if ($currentPhpDocInfo === null) {
return false;
}

$currentReturnType = $currentPhpDocInfo->getReturnType();
if (! $currentReturnType instanceof ArrayType) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function provideDataForTest(): Iterator

// skip
yield [__DIR__ . '/Fixture/skip_too_many.php.inc'];
yield [__DIR__ . '/Fixture/skip_mixed_of_specific_override.php.inc'];
yield [__DIR__ . '/Fixture/skip_closure_callable_override.php.inc'];
yield [__DIR__ . '/Fixture/skip_shorten_class_name.php.inc'];
yield [__DIR__ . '/Fixture/skip_constructor.php.inc'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;

class SkipMixedOfSpecificOverride
{
/**
* @return string[]
*/
public function provideForVendor(array $items): array
{
// exclude undesired packages
return array_diff($items, []);
}
}