matcherDefinitions = $matcherDefinitions; $this->validateMatcherDefinitions(); $this->initializeLastArrayKeyNameArray(); } /** * Called by PhpParser. */ public function enterNode(Node $node): null { if (!$this->isFileIgnored($node) && !$this->isLineIgnored($node) && $node instanceof ArrayDimFetch && isset($node->dim->value) && array_key_exists($node->dim->value, $this->flatMatcherDefinitions) ) { $match = [ 'restFiles' => [], 'line' => $node->getAttribute('startLine'), 'message' => 'Access to array key "' . $node->dim->value . '"', 'indicator' => 'weak', ]; foreach ($this->flatMatcherDefinitions[$node->dim->value]['candidates'] as $candidate) { $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles'])); } $this->matches[] = $match; } return null; } /** * Prepare 'lastKey' => [$details] array in flatMatcherDefinitions */ protected function initializeLastArrayKeyNameArray() { $methodNameArray = []; foreach ($this->matcherDefinitions as $fullArrayString => $details) { // Goal: find last part "foobar" of an array path "$foo['bar']['foobar']" // Reverse string $foo['bar']['foobar'] $lastKey = strrev($fullArrayString); // Cut off "['" $lastKey = substr($lastKey, 2); $lastKey = GeneralUtility::trimExplode('\'[', $lastKey); // Last key name $lastKey = $lastKey[0]; // And reverse key name again $lastKey = strrev($lastKey); if (!array_key_exists($lastKey, $methodNameArray)) { $methodNameArray[$lastKey]['candidates'] = []; } $methodNameArray[$lastKey]['candidates'][] = $details; } $this->flatMatcherDefinitions = $methodNameArray; } }