Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

final class WithExistingCastOnTernary
{
public function run()
{
return $relpath
. '/' . rawurlencode((string) $this->Get('account_code') ?: $this->Get('customer_id'));
}

public function run2()
{
return $relpath
. '/' . rawurlencode((string) $this->Get('account_code') ?: null);
}

public function Get($fieldname)
{
return $this->{$fieldname} ?? null;
}
}

?>
-----
<?php

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

final class WithExistingCastOnTernary
{
public function run()
{
return $relpath
. '/' . rawurlencode((string) $this->Get('account_code') ?: (string) $this->Get('customer_id'));
}

public function run2()
{
return $relpath
. '/' . rawurlencode((string) $this->Get('account_code') ?: '');
}

public function Get($fieldname)
{
return $this->{$fieldname} ?? null;
}
}

?>
65 changes: 41 additions & 24 deletions rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\String_ as CastString_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -192,51 +192,68 @@ private function processNullToStrictStringOnNodePosition(

$argValue = $args[$position]->value;

if ($argValue instanceof ConstFetch && $this->valueResolver->isNull($argValue)) {
if ($this->valueResolver->isNull($argValue)) {
$args[$position]->value = new String_('');
$funcCall->args = $args;

return $funcCall;
}

$type = $this->nodeTypeResolver->getType($argValue);
if ($type->isString()->yes()) {
if ($this->shouldSkipValue($argValue, $scope, $isTrait)) {
return null;
}

$nativeType = $this->nodeTypeResolver->getNativeType($argValue);
if ($nativeType->isString()->yes()) {
return null;
$parameter = $parametersAcceptor->getParameters()[$position] ?? null;
if ($parameter instanceof ExtendedNativeParameterReflection && $parameter->getType() instanceof UnionType) {
$parameterType = $parameter->getType();
if (! $this->isValidUnionType($parameterType)) {
return null;
}
}

if ($this->shouldSkipType($type)) {
return null;
if ($argValue instanceof Ternary && ! $this->shouldSkipValue($argValue->else, $scope, $isTrait)) {
if ($this->valueResolver->isNull($argValue->else)) {
$argValue->else = new String_('');
} else {
$argValue->else = new CastString_($argValue->else);
}

$args[$position]->value = $argValue;
$funcCall->args = $args;
return $funcCall;
}

if ($argValue instanceof InterpolatedString) {
return null;
$args[$position]->value = new CastString_($argValue);
$funcCall->args = $args;

return $funcCall;
}

private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait): bool
{
$type = $this->nodeTypeResolver->getType($expr);
if ($type->isString()->yes()) {
return true;
}

if ($this->isAnErrorType($argValue, $nativeType, $scope)) {
return null;
$nativeType = $this->nodeTypeResolver->getNativeType($expr);
if ($nativeType->isString()->yes()) {
return true;
}

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

$parameter = $parametersAcceptor->getParameters()[$position] ?? null;
if ($parameter instanceof ExtendedNativeParameterReflection && $parameter->getType() instanceof UnionType) {
$parameterType = $parameter->getType();
if (! $this->isValidUnionType($parameterType)) {
return null;
}
if ($expr instanceof InterpolatedString) {
return true;
}

$args[$position]->value = new CastString_($argValue);
$funcCall->args = $args;
if ($this->isAnErrorType($expr, $nativeType, $scope)) {
return true;
}

return $funcCall;
return $this->shouldSkipTrait($expr, $type, $isTrait);
}

private function isValidUnionType(Type $type): bool
Expand Down