Skip to content

Commit

Permalink
[PHP81] Skip after is_string on object call on NullToStrictStringFunc…
Browse files Browse the repository at this point in the history
…CallArgRector (#5572)

* [PHP81] Skip after is_string on object call on NullToStrictStringFuncCallArgRector

* fix
  • Loading branch information
samsonasik committed Feb 7, 2024
1 parent a76ec29 commit af4d3db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

final class SkipAfterIsStringOnObjectCall
{
public ?string $stringNull = 'foo';
}

$demo = new SkipAfterIsStringOnObjectCall();

if (is_string($demo->stringNull) && trim($demo->stringNull) !== '') {
echo $demo->stringNull;
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

if (! $type instanceof MixedType && ! $type instanceof NullType && ! ($type instanceof UnionType && $this->unionTypeAnalyzer->isNullable(
$type
))) {
$nativeType = $this->nodeTypeResolver->getNativeType($argValue);
if ($nativeType->isString()->yes()) {
return null;
}

if ($this->shouldSkipType($type)) {
return null;
}

Expand All @@ -212,6 +215,13 @@ private function processNullToStrictStringOnNodePosition(
return $funcCall;
}

private function shouldSkipType(Type $type): bool
{
return ! $type instanceof MixedType &&
! $type instanceof NullType &&
! ($type instanceof UnionType && $this->unionTypeAnalyzer->isNullable($type));
}

private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait): bool
{
if (! $type instanceof MixedType) {
Expand Down

0 comments on commit af4d3db

Please sign in to comment.