Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip mixed in ParamTypeByMethodCallTypeRector as not specific #5715

Merged
merged 1 commit into from
Mar 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
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#'
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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