Skip to content

Commit

Permalink
Skip mixed in ParamTypeByMethodCallTypeRector as not specific (#5715)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 12, 2024
1 parent a775c65 commit c390eab
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -431,3 +431,4 @@ parameters:
-
message: '#Parameters should use "array" types as the only types passed to this method#'
path: src/Util/NodePrinter.php
- '#Cognitive complexity for "Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher\:\:matchCallParamType\(\)" is 12, keep it under 11#'
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

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

final class SkipMixedAsNotPrecise
{
public function run($value)
{
$this->processArray($value);
}

private function processArray(mixed $value)
{
}
}
16 changes: 16 additions & 0 deletions rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand Down Expand Up @@ -48,6 +49,11 @@ public function matchCallParamType(
}

if (! $param->default instanceof Expr && ! $callParam->default instanceof Expr) {
// skip as mixed is not helpful and possibly requires more precise change elsewhere
if ($this->isCallParamMixed($callParam)) {
return null;
}

return $callParam->type;
}

Expand Down Expand Up @@ -162,4 +168,14 @@ private function resolveParentMethodParam(Scope $scope, string $methodName, int

return null;
}

private function isCallParamMixed(Param $param): bool
{
if (! $param->type instanceof Node) {
return false;
}

$callParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
return $callParamType instanceof MixedType;
}
}
8 changes: 6 additions & 2 deletions src/Application/FileProcessor.php
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Application;

use Nette\Utils\Strings;
use PHPStan\AnalysedCodeException;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
Expand All @@ -24,7 +25,6 @@
use Rector\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Nette\Utils\Strings;

final readonly class FileProcessor
{
Expand Down Expand Up @@ -156,7 +156,11 @@ private function printFile(File $file, Configuration $configuration, string $fil

// handle space before <?php
$ltrimNewContent = Strings::replace($newContent, self::OPEN_TAG_SPACED_REGEX, '<?php');
$ltrimOriginalFileContent = Strings::replace($ltrimOriginalFileContent, self::OPEN_TAG_SPACED_REGEX, '<?php');
$ltrimOriginalFileContent = Strings::replace(
$ltrimOriginalFileContent,
self::OPEN_TAG_SPACED_REGEX,
'<?php'
);
if ($ltrimOriginalFileContent === $ltrimNewContent) {
return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Autoloading/BootstrapFilesIncluder.php
Expand Up @@ -47,10 +47,8 @@ private function requireRectorStubs(): void
return;
}

$dir = new RecursiveDirectoryIterator(
$stubsRectorDirectory,
RecursiveDirectoryIterator::SKIP_DOTS
);
$dir = new RecursiveDirectoryIterator($stubsRectorDirectory, RecursiveDirectoryIterator::SKIP_DOTS);

/** @var SplFileInfo[] $stubs */
$stubs = new RecursiveIteratorIterator($dir);

Expand Down

0 comments on commit c390eab

Please sign in to comment.