Skip to content

Commit

Permalink
[Strict] Skip empty() from param doc on DisallowedEmptyRuleFixerRector (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jul 20, 2023
1 parent e2ec807 commit 8c5507f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 66 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

class SkipEmptyFromParamDoc
{
/**
* @param string $value
*/
public function getLabel($value)
{
if (empty($value)) {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

final class SkipMultiUnionTypesFromDoc
{
/**
* @param int|int[]|null|string $id
*/
public function get($id)
{
return empty($id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

final class SkipNegatedUnionTypeFromDoc
{
/**
* @param int|int[]|string $value
*/
public function run($value)
{
return ! empty($value);
}
}
10 changes: 10 additions & 0 deletions rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\Isset_;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -25,6 +26,7 @@ final class DisallowedEmptyRuleFixerRector extends AbstractFalsyScalarRuleFixerR
{
public function __construct(
private readonly ExactCompareFactory $exactCompareFactory,
private readonly ExprAnalyzer $exprAnalyzer
) {
}

Expand Down Expand Up @@ -98,6 +100,10 @@ private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope): Expr|
return $this->createDimFetchBooleanAnd($empty);
}

if ($this->exprAnalyzer->isNonTypedFromParam($empty->expr)) {
return null;
}

$emptyExprType = $scope->getType($empty->expr);

return $this->exactCompareFactory->createNotIdenticalFalsyCompare(
Expand All @@ -109,6 +115,10 @@ private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope): Expr|

private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpty): Expr|null
{
if ($this->exprAnalyzer->isNonTypedFromParam($empty->expr)) {
return null;
}

$exprType = $scope->getType($empty->expr);
return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty);
}
Expand Down

0 comments on commit 8c5507f

Please sign in to comment.