Skip to content

Commit

Permalink
Revert removed ErrorType handling on NullToStrictStringFuncCallArgRec…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Aug 14, 2023
1 parent 514df0a commit d8d4e8d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;

use Nette\Utils\Strings;

final class SkipErrorType
{
public function run($regex)
{
Strings::replace('value', $regex, function (array $match) use (
&$string
): string {
$innerPattern = $match['content'];
$positionDelimiter = strpos($innerPattern, $this->delimiter);

if ($positionDelimiter > 0) {
$innerPattern = str_replace($this->delimiter, '\\' . $this->delimiter, $innerPattern);
}

// change delimiter
if (strlen($innerPattern) > 2 && $innerPattern[0] === $innerPattern[strlen($innerPattern) - 1]) {
return 'foo';
}
});
}
}
46 changes: 35 additions & 11 deletions rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
Expand All @@ -25,6 +26,8 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -381,21 +384,26 @@ public function refactor(Node $node): ?Node
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$args = $node->getArgs();
$positions = $this->argsAnalyzer->hasNamedArg($args)
? $this->resolveNamedPositions($node, $args)
: $this->resolveOriginalPositions($node);
: $this->resolveOriginalPositions($node, $scope);

if ($positions === []) {
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
$classReflection = $scope->getClassReflection();
$isTrait = $classReflection instanceof ClassReflection && $classReflection->isTrait();

$isChanged = false;
foreach ($positions as $position) {
$result = $this->processNullToStrictStringOnNodePosition($node, $args, $position, $isTrait);
$result = $this->processNullToStrictStringOnNodePosition($node, $args, $position, $isTrait, $scope);
if ($result instanceof Node) {
$node = $result;
$isChanged = true;
Expand Down Expand Up @@ -446,7 +454,8 @@ private function processNullToStrictStringOnNodePosition(
FuncCall $funcCall,
array $args,
int|string $position,
bool $isTrait
bool $isTrait,
Scope $scope
): ?FuncCall {
if (! isset($args[$position])) {
return null;
Expand Down Expand Up @@ -474,6 +483,10 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

if ($this->isAnErrorTypeFromParentScope($argValue, $scope)) {
return null;
}

if ($this->shouldSkipTrait($argValue, $type, $isTrait)) {
return null;
}
Expand Down Expand Up @@ -505,26 +518,37 @@ private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait): bool
return true;
}

private function isAnErrorTypeFromParentScope(Expr $expr, Scope $scope): bool
{
$parentScope = $scope->getParentScope();
if ($parentScope instanceof Scope) {
return $parentScope->getType($expr) instanceof ErrorType;
}

return false;
}

/**
* @return int[]|string[]
*/
private function resolveOriginalPositions(FuncCall $funcCall): array
private function resolveOriginalPositions(FuncCall $funcCall, Scope $scope): array
{
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($funcCall);
if (! $functionReflection instanceof NativeFunctionReflection) {
return [];
}

$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors(
$functionReflection->getVariants()
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$funcCall,
$scope
);

$functionName = $functionReflection->getName();
$argNames = self::ARG_POSITION_NAME_NULL_TO_STRICT_STRING[$functionName];
$positions = [];

foreach ($parametersAcceptorWithPhpDocs->getParameters() as $position => $parameterReflectionWithPhpDoc) {
if (in_array($parameterReflectionWithPhpDoc->getName(), $argNames, true)) {
foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
if (in_array($parameterReflection->getName(), $argNames, true)) {
$positions[] = $position;
}
}
Expand Down

0 comments on commit d8d4e8d

Please sign in to comment.