| Conditions | 12 |
| Paths | 10 |
| Total Lines | 62 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 63 | public function parse(string $content, ConditionMatcherInterface $matcher, ?Site $site = null): array |
||
| 64 | { |
||
| 65 | $hashOfContent = md5('PAGES:' . $content); |
||
| 66 | $cachedContent = $this->cache->get($hashOfContent); |
||
| 67 | // Something about this content has been cached before, lets verify the matchings, if they also apply |
||
| 68 | if (is_array($cachedContent) && is_array($cachedContent[0])) { |
||
| 69 | // Cache entry found, see if the "matching" section matches with the matcher |
||
| 70 | $storedData = $cachedContent[0]; |
||
| 71 | $storedMD5 = $cachedContent[1]; |
||
| 72 | $storedData['match'] = $this->matching($storedData['sections'] ?? [], $matcher); |
||
| 73 | $hashOfDataWithMatches = md5(json_encode($storedData)); |
||
| 74 | // The matches are the same, so nothing to do here |
||
| 75 | if ($hashOfDataWithMatches === $storedMD5) { |
||
| 76 | $result = $storedData['TSconfig']; |
||
| 77 | } else { |
||
| 78 | // Create a hash out of the content-hash PLUS the matching information and try again |
||
| 79 | $shash = md5($hashOfDataWithMatches . $hashOfContent); |
||
| 80 | $storedData = $this->cache->get($shash); |
||
| 81 | if (is_array($storedData)) { |
||
| 82 | $result = $storedData['TSconfig']; |
||
| 83 | } else { |
||
| 84 | // Create a new content with the matcher, and cache it as a new entry |
||
| 85 | $parsedAndMatchedData = $this->parseAndMatch($content, $matcher); |
||
| 86 | // Now store the full data from the parser (with matches) |
||
| 87 | $this->cache->set($shash, $parsedAndMatchedData, ['pageTSconfig'], 0); |
||
| 88 | $result = $parsedAndMatchedData['TSconfig']; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return $result; |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($site) { |
||
| 95 | $siteSettings = $site->getConfiguration()['settings'] ?? []; |
||
| 96 | if (!empty($siteSettings)) { |
||
| 97 | $siteSettings = ArrayUtility::flatten($siteSettings); |
||
| 98 | } |
||
| 99 | if (!empty($siteSettings)) { |
||
| 100 | // Recursive substitution of site settings (up to 10 nested levels) |
||
| 101 | // note: this code is more or less a duplicate of \TYPO3\CMS\Core\TypoScript\TemplateService::substituteConstants |
||
| 102 | for ($i = 0; $i < 10; $i++) { |
||
| 103 | $beforeSubstitution = $content; |
||
| 104 | $content = preg_replace_callback( |
||
| 105 | '/\\{\\$(.[^}]*)\\}/', |
||
| 106 | function (array $matches) use ($siteSettings): string { |
||
| 107 | return isset($siteSettings[$matches[1]]) && !is_array($siteSettings[$matches[1]]) |
||
| 108 | ? (string)$siteSettings[$matches[1]] : $matches[0]; |
||
|
|
|||
| 109 | }, |
||
| 110 | $content |
||
| 111 | ); |
||
| 112 | if ($beforeSubstitution === $content) { |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // Nothing found in cache for this content string, let's do everything. |
||
| 120 | $parsedAndMatchedData = $this->parseAndMatch($content, $matcher); |
||
| 121 | // ALL parts, including the matching part is cached. |
||
| 122 | $md5 = md5(json_encode($parsedAndMatchedData)); |
||
| 123 | $this->cache->set($hashOfContent, [$parsedAndMatchedData, $md5], ['pageTSconfig'], 0); |
||
| 124 | return $parsedAndMatchedData['TSconfig']; |
||
| 125 | } |
||
| 162 |