| Total Complexity | 43 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MetaValue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetaValue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class MetaValue |
||
| 34 | { |
||
| 35 | // Constants |
||
| 36 | // ========================================================================= |
||
| 37 | |||
| 38 | public const MAX_TEMPLATE_LENGTH = 4096; |
||
| 39 | public const MAX_PARSE_TRIES = 5; |
||
| 40 | // Semicolon because that is the resolved config key when rendering tags, |
||
| 41 | // kebab-case because that is the config keys as defined in the config files. |
||
| 42 | public const NO_ALIASES = [ |
||
| 43 | 'twitter:site', |
||
| 44 | 'twitter:creator', |
||
| 45 | 'twitterSite', |
||
| 46 | 'twitterCreator', |
||
| 47 | ]; |
||
| 48 | public const NO_PARSING = [ |
||
| 49 | 'siteLinksSearchTarget', |
||
| 50 | ]; |
||
| 51 | public const PARSE_ONCE = [ |
||
| 52 | 'target', |
||
| 53 | 'urlTemplate', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | // Static Properties |
||
| 57 | // ========================================================================= |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | public static $templateObjectVars; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | public static $templatePreviewVars = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var View |
||
| 71 | */ |
||
| 72 | public static $view; |
||
| 73 | |||
| 74 | // Static Methods |
||
| 75 | // ========================================================================= |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $metaValue |
||
| 79 | * @param bool $resolveAliases Whether @ aliases should be resolved in |
||
| 80 | * this string |
||
| 81 | * @param bool $parseAsTwig Whether items should be parsed as a Twig |
||
| 82 | * template in this string |
||
| 83 | * @param int $tries The number of times to parse the string |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public static function parseString( |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param array $metaArray |
||
| 110 | * @param bool $resolveAliases Whether @ aliases should be resolved in |
||
| 111 | * this array |
||
| 112 | * @param bool $parseAsTwig Whether items should be parsed as a Twig |
||
| 113 | * template in this array |
||
| 114 | * @param bool $recursive Whether to recursively parse the array |
||
| 115 | */ |
||
| 116 | public static function parseArray(array &$metaArray, bool $resolveAliases = true, bool $parseAsTwig = true, bool $recursive = false) |
||
| 117 | { |
||
| 118 | // Do this here as well so that parseString() won't potentially be constantly switching modes |
||
| 119 | // while parsing through the array |
||
| 120 | $oldTemplateMode = self::$view->getTemplateMode(); |
||
| 121 | // Render in site template mode so that we get globals injected |
||
| 122 | if ($oldTemplateMode !== self::$view::TEMPLATE_MODE_SITE) { |
||
| 123 | try { |
||
| 124 | self::$view->setTemplateMode(self::$view::TEMPLATE_MODE_SITE); |
||
| 125 | } catch (Exception $e) { |
||
| 126 | Craft::error($e->getMessage(), __METHOD__); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | foreach ($metaArray as $key => $value) { |
||
| 130 | if ($recursive && is_array($value)) { |
||
| 131 | self::parseArray($value, $resolveAliases, $parseAsTwig, $recursive); |
||
| 132 | } |
||
| 133 | $shouldParse = $parseAsTwig; |
||
| 134 | $shouldAlias = $resolveAliases; |
||
| 135 | $tries = self::MAX_PARSE_TRIES; |
||
| 136 | if (in_array($key, self::NO_ALIASES, true)) { |
||
| 137 | $shouldAlias = false; |
||
| 138 | } |
||
| 139 | if (in_array($key, self::NO_PARSING, true)) { |
||
| 140 | $shouldParse = false; |
||
| 141 | } |
||
| 142 | if (in_array($key, self::PARSE_ONCE, true)) { |
||
| 143 | $tries = 1; |
||
| 144 | if (is_string($value) && $value[0] !== '{') { |
||
| 145 | $shouldParse = false; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | if ($value !== null) { |
||
| 149 | $metaArray[$key] = self::parseString($value, $shouldAlias, $shouldParse, $tries); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | // Restore the template mode |
||
| 153 | if ($oldTemplateMode !== self::$view::TEMPLATE_MODE_SITE) { |
||
| 154 | try { |
||
| 155 | self::$view->setTemplateMode($oldTemplateMode); |
||
| 156 | } catch (Exception $e) { |
||
| 157 | Craft::error($e->getMessage(), __METHOD__); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // Remove any empty values |
||
| 162 | $metaArray = array_filter( |
||
| 163 | $metaArray, |
||
| 164 | [ArrayHelper::class, 'preserveNumerics'] |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the language from a siteId |
||
| 170 | * |
||
| 171 | * @param null|int $siteId |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public static function getSiteLanguage(int $siteId = null): string |
||
| 176 | { |
||
| 177 | if ($siteId === null) { |
||
| 178 | try { |
||
| 179 | $siteId = Craft::$app->getSites()->getCurrentSite()->id; |
||
| 180 | } catch (SiteNotFoundException $e) { |
||
| 181 | $siteId = 1; |
||
| 182 | Craft::error($e->getMessage(), __METHOD__); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $site = Craft::$app->getSites()->getSiteById($siteId); |
||
| 186 | if ($site) { |
||
| 187 | $language = $site->language; |
||
| 188 | } else { |
||
| 189 | $language = Craft::$app->language; |
||
| 190 | } |
||
| 191 | $language = strtolower($language); |
||
| 192 | $language = str_replace('_', '-', $language); |
||
| 193 | |||
| 194 | return $language; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Cache frequently accessed properties locally |
||
| 199 | */ |
||
| 200 | public static function cache() |
||
| 231 | } |
||
| 232 | |||
| 233 | // Protected Methods |
||
| 234 | // ========================================================================= |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string|object $metaValue |
||
| 238 | * @param bool $resolveAliases Whether @ aliases should be resolved |
||
| 239 | * in this string |
||
| 240 | * @param bool $parseAsTwig Whether items should be parsed as a |
||
| 241 | * Twig template in this string |
||
| 242 | * |
||
| 243 | * @return null|string |
||
| 244 | */ |
||
| 245 | protected static function parseMetaString($metaValue, bool $resolveAliases = true, bool $parseAsTwig = true) |
||
| 317 |