From af4d3db31bacab922d47b2551cec66644e3568b7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Feb 2024 17:31:57 +0700 Subject: [PATCH] [PHP81] Skip after is_string on object call on NullToStrictStringFuncCallArgRector (#5572) * [PHP81] Skip after is_string on object call on NullToStrictStringFuncCallArgRector * fix --- .../skip_after_is_string_on_object_call.php.inc | 14 ++++++++++++++ .../NullToStrictStringFuncCallArgRector.php | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_after_is_string_on_object_call.php.inc diff --git a/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_after_is_string_on_object_call.php.inc b/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_after_is_string_on_object_call.php.inc new file mode 100644 index 00000000000..250871baa20 --- /dev/null +++ b/rules-tests/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector/Fixture/skip_after_is_string_on_object_call.php.inc @@ -0,0 +1,14 @@ +stringNull) && trim($demo->stringNull) !== '') { + echo $demo->stringNull; +} diff --git a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php index 6ae7921cf82..968cd7fd33e 100644 --- a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php +++ b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php @@ -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; } @@ -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) {