‪TYPO3CMS  10.4
ArrayDimensionMatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PhpParser\Node;
21 use PhpParser\Node\Expr\ArrayDimFetch;
23 
31 {
37  public function ‪__construct(array ‪$matcherDefinitions)
38  {
39  $this->matcherDefinitions = ‪$matcherDefinitions;
42  }
43 
49  public function ‪enterNode(Node $node)
50  {
51  if (!$this->‪isFileIgnored($node)
52  && !$this->‪isLineIgnored($node)
53  && $node instanceof ArrayDimFetch
54  && isset($node->dim->value)
55  && array_key_exists($node->dim->value, $this->flatMatcherDefinitions)
56  ) {
57  $match = [
58  'restFiles' => [],
59  'line' => $node->getAttribute('startLine'),
60  'message' => 'Access to array key "' . $node->dim->value . '"',
61  'indicator' => 'weak',
62  ];
63 
64  foreach ($this->flatMatcherDefinitions[$node->dim->value]['candidates'] as $candidate) {
65  $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
66  }
67  $this->matches[] = $match;
68  }
69  }
70 
75  {
76  $methodNameArray = [];
77  foreach ($this->matcherDefinitions as $fullArrayString => $details) {
78  // Goal: find last part "foobar" of an array path "$foo['bar']['foobar']"
79  // Reverse string $foo['bar']['foobar']
80  $lastKey = strrev($fullArrayString);
81  // Cut off "['"
82  $lastKey = substr($lastKey, 2);
83  $lastKey = ‪GeneralUtility::trimExplode('\'[', $lastKey);
84  // Last key name
85  $lastKey = $lastKey[0];
86  // And reverse key name again
87  $lastKey = strrev($lastKey);
88 
89  if (!array_key_exists($lastKey, $methodNameArray)) {
90  $methodNameArray[$lastKey]['candidates'] = [];
91  }
92  $methodNameArray[$lastKey]['candidates'][] = $details;
93  }
94  $this->flatMatcherDefinitions = $methodNameArray;
95  }
96 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcher.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\isLineIgnored
‪bool isLineIgnored(Node $node)
Definition: AbstractCoreMatcher.php:181
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\validateMatcherDefinitions
‪validateMatcherDefinitions(array $requiredArrayKeys=[])
Definition: AbstractCoreMatcher.php:88
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher\enterNode
‪enterNode(Node $node)
Definition: ArrayDimensionMatcher.php:49
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\isFileIgnored
‪bool isFileIgnored(Node $node)
Definition: AbstractCoreMatcher.php:214
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\$matcherDefinitions
‪array $matcherDefinitions
Definition: AbstractCoreMatcher.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher\initializeLastArrayKeyNameArray
‪initializeLastArrayKeyNameArray()
Definition: ArrayDimensionMatcher.php:74
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher
Definition: ArrayDimensionMatcher.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\ArrayDimensionMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: ArrayDimensionMatcher.php:37