diff --git a/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_always_string_if_else_dom_element.php.inc b/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_always_string_if_else_dom_element.php.inc new file mode 100644 index 00000000000..1a3b8610916 --- /dev/null +++ b/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_always_string_if_else_dom_element.php.inc @@ -0,0 +1,16 @@ +loadHTML($html); + $titleElement = $dom->getElementsByTagName('title')->item(0); + if ($titleElement?->nodeValue !== null) { + return trim($titleElement->nodeValue); + } else { + return 'Unnamed document'; + } + } +} \ No newline at end of file diff --git a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php index a813fabb688..ad604062c8b 100644 --- a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php +++ b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php @@ -199,7 +199,7 @@ private function processNullToStrictStringOnNodePosition( return null; } - if ($this->isAnErrorTypeFromParentScope($argValue, $scope)) { + if ($this->isAnErrorType($argValue, $nativeType, $scope)) { return null; } @@ -251,10 +251,6 @@ private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait): bool return false; } - if ($type instanceof ErrorType) { - return true; - } - if ($type->isExplicitMixed()) { return false; } @@ -266,8 +262,12 @@ private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait): bool return true; } - private function isAnErrorTypeFromParentScope(Expr $expr, Scope $scope): bool + private function isAnErrorType(Expr $expr, Type $type, Scope $scope): bool { + if ($type instanceof ErrorType) { + return true; + } + $parentScope = $scope->getParentScope(); if ($parentScope instanceof Scope) { return $parentScope->getType($expr) instanceof ErrorType;