binaryOpManipulator = $binaryOpManipulator; } /** * getRuleDefinition * * @return RuleDefinition * @throws PoorDocumentationException */ public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( 'Change $conf->global to getDolGlobal', [new CodeSample('$conf->global->CONSTANT', 'getDolGlobalInt(\'CONSTANT\')' )]); } /** * getNodeTypes * * @return array */ public function getNodeTypes(): array { return [Equal::class, BooleanAnd::class]; } /** * refactor * * @param Node $node A node * @return Equal|void */ public function refactor(Node $node) { if ($node instanceof BooleanAnd) { $nodes = $this->resolveTwoNodeMatch($node); if (!isset($nodes)) { return; } /** @var Equal $node */ $node = $nodes->getFirstExpr(); } if (!$node instanceof Equal) { return; }; if (!$this->isGlobalVar($node->left)) { return; } switch ($node->right->getType()) { case 'Scalar_LNumber': $funcName = 'getDolGlobalInt'; break; case 'Scalar_String': $funcName = 'getDolGlobalString'; break; default: return; } $constName = $this->getName($node->left); if (empty($constName)) { return; } return new Equal( new FuncCall( new Name($funcName), [new Arg(new String_($constName))] ), $node->right ); } /** * Get nodes with check empty * * @param BooleanAnd $booleanAnd A BooleandAnd * @return TwoNodeMatch|null */ private function resolveTwoNodeMatch(BooleanAnd $booleanAnd): ?TwoNodeMatch { return $this->binaryOpManipulator->matchFirstAndSecondConditionNode( $booleanAnd, // $conf->global == $value function (Node $node): bool { if (!$node instanceof Equal) { return \false; } return $this->isGlobalVar($node->left); }, // !empty(...) || isset(...) function (Node $node): bool { if ($node instanceof BooleanNot && $node->expr instanceof Empty_) { return $this->isGlobalVar($node->expr->expr); } if (!$node instanceof Isset_) { return $this->isGlobalVar($node); } return \true; } ); } /** * Check node is global access * * @param $node A node * @return bool */ private function isGlobalVar($node) { if (!$node instanceof PropertyFetch) { return false; } if (!$this->isName($node->var, 'global')) { return false; } $global = $node->var; if (!$global instanceof PropertyFetch) { return false; } if (!$this->isName($global->var, 'conf')) { return false; } return true; } }