parenthesizedNestedTernaryAnalyzer = $parenthesizedNestedTernaryAnalyzer; } public function provideMinPhpVersion() : int { return PhpVersionFeature::DEPRECATE_NESTED_TERNARY; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Add parentheses to nested ternary', [new CodeSample(<<<'CODE_SAMPLE' $value = $a ? $b : $a ?: null; CODE_SAMPLE , <<<'CODE_SAMPLE' $value = ($a ? $b : $a) ?: null; CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Ternary::class]; } /** * @param Ternary $node */ public function refactor(Node $node) : ?Node { if ($node->cond instanceof Ternary || $node->else instanceof Ternary) { if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node)) { return null; } // re-print with brackets $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); return $node; } return null; } }