| Total Complexity | 43 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 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) |
||
| 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 |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Cache frequently accessed properties locally |
||
| 199 | */ |
||
| 200 | public static function cache() |
||
| 201 | { |
||
| 202 | self::$templateObjectVars = [ |
||
| 203 | 'seomatic' => Seomatic::$seomaticVariable, |
||
| 204 | ]; |
||
| 205 | |||
| 206 | $element = Seomatic::$matchedElement; |
||
| 207 | /** @var Element $element */ |
||
| 208 | if ($element !== null) { |
||
| 209 | $refHandle = null; |
||
|
|
|||
| 210 | // Get a fallback from the element's root class name |
||
| 211 | $reflector = new ReflectionClass($element); |
||
| 212 | $refHandle = strtolower($reflector->getShortName()); |
||
| 213 | $elementRefHandle = $element::refHandle(); |
||
| 214 | // Use the SeoElement interface to get the refHandle |
||
| 215 | $metaBundleSourceType = Seomatic::$plugin->seoElements->getMetaBundleTypeFromElement($element); |
||
| 216 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundleSourceType); |
||
| 217 | if ($seoElement) { |
||
| 218 | $elementRefHandle = $seoElement::getElementRefHandle(); |
||
| 219 | } |
||
| 220 | // Prefer $element::refHandle() |
||
| 221 | $matchedElementType = $elementRefHandle ?? $refHandle; |
||
| 222 | if ($matchedElementType) { |
||
| 223 | self::$templateObjectVars[$matchedElementType] = $element; |
||
| 224 | self::$templatePreviewVars[$matchedElementType] = $element; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | self::$templatePreviewVars['object'] = self::$templateObjectVars; |
||
| 228 | self::$templatePreviewVars['seomatic'] = Seomatic::$seomaticVariable; |
||
| 229 | |||
| 230 | self::$view = Seomatic::$sandboxView; |
||
| 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) |
||
| 315 | } |
||
| 316 | } |
||
| 317 |