Skip to content

Commit

Permalink
[Php81] Allow nullable union on NullToStrictStringFuncCallArgRector (#…
Browse files Browse the repository at this point in the history
…5532)

* [Php81] Allow union on NullToStrictStringFuncCallArgRector

* [Php81] Allow union on NullToStrictStringFuncCallArgRector

* [ci-review] Rector Rectify

* [Php81] Allow union on NullToStrictStringFuncCallArgRector

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 31, 2024
1 parent b78db9f commit 27791d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

class WithNullableDocblock
{
/**
* @var string|null
*/
public $importedId;

/**
* @param string|null $importedId
*/
public function setImportedId($importedId) {
if (strlen($this->importedId) > 0) {
$this->importedId = $importedId;
}
}
}

?>
-----
<?php

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

class WithNullableDocblock
{
/**
* @var string|null
*/
public $importedId;

/**
* @param string|null $importedId
*/
public function setImportedId($importedId) {
if (strlen((string) $this->importedId) > 0) {
$this->importedId = $importedId;
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php81\Enum\NameNullToStrictNullFunctionMap;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\PhpVersionFeature;
Expand All @@ -43,7 +45,8 @@ public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly ValueResolver $valueResolver
private readonly ValueResolver $valueResolver,
private readonly UnionTypeAnalyzer $unionTypeAnalyzer
) {
}

Expand Down Expand Up @@ -185,7 +188,7 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

if (! $type instanceof MixedType && ! $type instanceof NullType) {
if (! $type instanceof MixedType && ! $type instanceof NullType && ! ($type instanceof UnionType && $this->unionTypeAnalyzer->isNullable($type))) {
return null;
}

Expand Down

0 comments on commit 27791d1

Please sign in to comment.