Skip to content

Commit

Permalink
[Strict] Skip docblock on DisallowedShortTernaryRuleFixerRector (#4639)
Browse files Browse the repository at this point in the history
* [Strict] Skip docblock on DisallowedShortTernaryRuleFixerRector

* Fixed 🎉
  • Loading branch information
samsonasik committed Aug 3, 2023
1 parent 3c2fab2 commit 8f6b366
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector\Fixture;

final class SkipDocblock
{
/**
* @param array $array
*/
public function run($array)
{
return $array ?: 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
final class DisallowedShortTernaryRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
private bool $hasChanged = false;

public function __construct(
private readonly ExactCompareFactory $exactCompareFactory,
) {
Expand Down Expand Up @@ -75,6 +77,8 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope): ?Ternary
{
$this->hasChanged = false;

// skip non-short ternary
if ($node->if instanceof Expr) {
return null;
Expand All @@ -83,10 +87,15 @@ public function refactorWithScope(Node $node, Scope $scope): ?Ternary
// special case for reset() function
if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'reset')) {
$this->refactorResetFuncCall($node, $node->cond, $scope);

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

return $node;
}

$exprType = $scope->getType($node->cond);
$exprType = $scope->getNativeType($node->cond);
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare(
$exprType,
$node->cond,
Expand All @@ -112,7 +121,7 @@ private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall

$firstArgValue = $resetFuncCall->getArgs()[0]
->value;
$firstArgType = $scope->getType($firstArgValue);
$firstArgType = $scope->getNativeType($firstArgValue);

$falsyCompareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare(
$firstArgType,
Expand All @@ -125,5 +134,6 @@ private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall
}

$ternary->cond = $falsyCompareExpr;
$this->hasChanged = true;
}
}

0 comments on commit 8f6b366

Please sign in to comment.