Skip to content

Commit

Permalink
[Php81] Allow explicit mixed processed on trait on NullToStrictString…
Browse files Browse the repository at this point in the history
…FuncCallArgRector (#3212)

* [Php81] Allow explicit mixed processed on trait on NullToStrictStringFuncCallArgRector

* Fixed 🎉

* [ci-review] Rector Rectify

* Final touch: clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 17, 2022
1 parent dfad0c3 commit e35497e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

trait ExplicitMixedInTrait
{
public function setTitle(mixed $title)
{
$this->title = $title;
return trim($this->title);
}
}

?>
-----
<?php

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

trait ExplicitMixedInTrait
{
public function setTitle(mixed $title)
{
$this->title = $title;
return trim((string) $this->title);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

if ($this->shouldSkipTrait($argValue, $isTrait)) {
if ($this->shouldSkipTrait($argValue, $type, $isTrait)) {
return null;
}

Expand All @@ -490,13 +490,21 @@ private function processNullToStrictStringOnNodePosition(
return $funcCall;
}

private function shouldSkipTrait(Expr $expr, bool $isTrait): bool
private function shouldSkipTrait(Expr $expr, MixedType $mixedType, bool $isTrait): bool
{
if (! $isTrait) {
return false;
}

if ($mixedType->isExplicitMixed()) {
return false;
}

if (! $expr instanceof MethodCall) {
return $isTrait && $this->propertyFetchAnalyzer->isLocalPropertyFetch($expr);
return $this->propertyFetchAnalyzer->isLocalPropertyFetch($expr);
}

return $isTrait;
return true;
}

private function isCastedReassign(Expr $expr): bool
Expand Down

0 comments on commit e35497e

Please sign in to comment.