REDROOM
PHP 8.2.31
Path:
Logout
Edit File
Size: 3.04 KB
Close
/opt/cpanel/ea-wappspector/vendor/rector/rector/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php
Text
Base64
<?php declare (strict_types=1); namespace Rector\Strict\Rector\If_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Stmt\If_; use PHPStan\Analyser\Scope; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * Fixer Rector for PHPStan rules: * https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php * https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php * * @see \Rector\Tests\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector\BooleanInIfConditionRuleFixerRectorTest */ final class BooleanInIfConditionRuleFixerRector extends AbstractFalsyScalarRuleFixerRector { /** * @readonly * @var \Rector\Strict\NodeFactory\ExactCompareFactory */ private $exactCompareFactory; public function __construct(ExactCompareFactory $exactCompareFactory) { $this->exactCompareFactory = $exactCompareFactory; } public function getRuleDefinition() : RuleDefinition { $errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\BooleansInConditions\\BooleanInIfConditionRule'); return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE' final class NegatedString { public function run(string $name) { if ($name) { return 'name'; } return 'no name'; } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class NegatedString { public function run(string $name) { if ($name !== '') { return 'name'; } return 'no name'; } } CODE_SAMPLE , [self::TREAT_AS_NON_EMPTY => \false])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [If_::class]; } /** * @param If_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?If_ { $hasChanged = \false; // 1. if $ifCondExprType = $scope->getNativeType($node->cond); $notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($ifCondExprType, $node->cond, $this->treatAsNonEmpty); if ($notIdentical !== null) { $node->cond = $notIdentical; $hasChanged = \true; } // 2. elseifs foreach ($node->elseifs as $elseif) { $elseifCondExprType = $scope->getNativeType($elseif->cond); $notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($elseifCondExprType, $elseif->cond, $this->treatAsNonEmpty); if (!$notIdentical instanceof Expr) { continue; } $elseif->cond = $notIdentical; $hasChanged = \true; } if ($hasChanged) { return $node; } return null; } }
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 1
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
BooleanInIfConditionRuleFixerRector.php
3.04 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).