Skip to content

Commit

Permalink
[TypeDeclaration] Ensure return $node only when changed on AddParamTy…
Browse files Browse the repository at this point in the history
…peDeclarationRector (#2399)
  • Loading branch information
samsonasik committed May 31, 2022
1 parent 32afbbe commit cd405e6
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ final class AddParamTypeDeclarationRector extends AbstractRector implements Conf
*/
private array $addParamTypeDeclarations = [];

private bool $hasChanged = false;

public function __construct(
private readonly TypeComparator $typeComparator,
private readonly PhpVersionProvider $phpVersionProvider,
Expand Down Expand Up @@ -99,6 +101,10 @@ public function refactor(Node $node): ?Node
$this->refactorClassMethodWithTypehintByParameterPosition($node, $addParamTypeDeclaration);
}

if (! $this->hasChanged) {
return null;
}

return $node;
}

Expand Down Expand Up @@ -157,7 +163,6 @@ private function refactorClassMethodWithTypehintByParameterPosition(

private function refactorParameter(Param $param, AddParamTypeDeclaration $addParamTypeDeclaration): void
{

// already set → no change
if ($param->type !== null) {
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
Expand All @@ -171,6 +176,8 @@ private function refactorParameter(Param $param, AddParamTypeDeclaration $addPar
TypeKind::PARAM
);

$this->hasChanged = true;

// remove it
if ($addParamTypeDeclaration->getParamType() instanceof MixedType) {
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
Expand Down

0 comments on commit cd405e6

Please sign in to comment.