Skip to content

Commit

Permalink
[CodeQuality] No need to re-update doc when already union null doc on…
Browse files Browse the repository at this point in the history
… ExplicitReturnNullRector (#5757)
  • Loading branch information
samsonasik committed Mar 22, 2024
1 parent 576198c commit 61c7e92
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

class AlreadyUnionNull
{
/**
* @return ?string
*/
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

class AlreadyUnionNull
{
/**
* @return ?string
*/
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}
return null;
}
}

?>
Expand Up @@ -130,14 +130,20 @@ private function transformDocUnionVoidToUnionNull(ClassMethod $classMethod): voi
}

$newTypes = [];
$hasChanged = false;
foreach ($returnType->getTypes() as $type) {
if ($type instanceof VoidType) {
$type = new NullType();
$hasChanged = true;
}

$newTypes[] = $type;
}

if (! $hasChanged) {
return;
}

$type = $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($newTypes);
if (! $type instanceof UnionType) {
return;
Expand Down

0 comments on commit 61c7e92

Please sign in to comment.