Skip to content

Commit

Permalink
[Php81] Skip always string if else DomElement on NullToStrictStringFu…
Browse files Browse the repository at this point in the history
…ncCallArgRector (#5822)
  • Loading branch information
samsonasik committed Apr 15, 2024
1 parent 86eb512 commit 2b811af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

final class SkipAlwaysStringIfElseDomElement {
public function getTitle(string $html): string {
$dom = new \DOMDocument();
$dom->loadHTML($html);
$titleElement = $dom->getElementsByTagName('title')->item(0);
if ($titleElement?->nodeValue !== null) {
return trim($titleElement->nodeValue);
} else {
return 'Unnamed document';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

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

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 2b811af

Please sign in to comment.