| Total Complexity | 82 |
| Total Lines | 532 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Schema |
||
| 33 | { |
||
| 34 | // Constants |
||
| 35 | // ========================================================================= |
||
| 36 | |||
| 37 | public const SCHEMA_PATH_DELIMITER = '.'; |
||
| 38 | public const MENU_INDENT_STEP = 4; |
||
| 39 | |||
| 40 | public const SCHEMA_TYPES = [ |
||
| 41 | 'siteSpecificType', |
||
| 42 | 'siteSubType', |
||
| 43 | 'siteType', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | public const GLOBAL_SCHEMA_CACHE_TAG = 'seomatic_schema'; |
||
| 47 | public const SCHEMA_CACHE_TAG = 'seomatic_schema_'; |
||
| 48 | |||
| 49 | public const CACHE_KEY = 'seomatic_schema_'; |
||
| 50 | |||
| 51 | // Static Methods |
||
| 52 | // ========================================================================= |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Invalidate all of the schema caches |
||
| 56 | */ |
||
| 57 | public static function invalidateCaches(): void |
||
| 58 | { |
||
| 59 | $cache = Craft::$app->getCache(); |
||
| 60 | TagDependency::invalidate($cache, self::GLOBAL_SCHEMA_CACHE_TAG); |
||
| 61 | Craft::info( |
||
| 62 | 'All schema caches cleared', |
||
| 63 | __METHOD__ |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Return the most specific schema.org type possible from the $settings |
||
| 69 | * |
||
| 70 | * @param $settings |
||
| 71 | * |
||
| 72 | * @param bool $allowEmpty |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public static function getSpecificEntityType($settings, bool $allowEmpty = false): string |
||
| 76 | { |
||
| 77 | if (!empty($settings)) { |
||
| 78 | // Go from most specific type to least specific type |
||
| 79 | foreach (self::SCHEMA_TYPES as $schemaType) { |
||
| 80 | if (!empty($settings[$schemaType]) && ($settings[$schemaType] !== 'none')) { |
||
| 81 | return $settings[$schemaType]; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | return $allowEmpty ? '' : 'WebPage'; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return a period-delimited schema.org path from the $settings |
||
| 91 | * |
||
| 92 | * @param $settings |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public static function getEntityPath($settings): string |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the fully composed schema type |
||
| 113 | * |
||
| 114 | * @param $schemaType |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public static function getSchemaType(string $schemaType): array |
||
| 119 | { |
||
| 120 | $result = []; |
||
| 121 | $jsonLdType = MetaJsonLd::create($schemaType); |
||
| 122 | |||
| 123 | // Get the static properties |
||
| 124 | try { |
||
| 125 | $classRef = new ReflectionClass(get_class($jsonLdType)); |
||
| 126 | } catch (ReflectionException $e) { |
||
| 127 | $classRef = null; |
||
| 128 | } |
||
| 129 | if ($classRef) { |
||
| 130 | $result = $classRef->getStaticProperties(); |
||
| 131 | if (isset($result['schemaTypeDescription'])) { |
||
| 132 | $description = $result['schemaTypeDescription']; |
||
| 133 | $description = preg_replace("`\[\[([A-z]*)\]\]`", '[$1](https://schema.org/$1)', $description); |
||
| 134 | $description = preg_replace('/\s+/', ' ', $description); |
||
| 135 | $description = Markdown::process((string)$description); |
||
| 136 | $description = str_replace(['<p>', '</p>', '\n'], ['', '', ' '], $description); |
||
| 137 | $result['schemaTypeDescription'] = $description; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return $result; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the decomposed schema type |
||
| 146 | * |
||
| 147 | * @param string $schemaType |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public static function getDecomposedSchemaType(string $schemaType): array |
||
| 152 | { |
||
| 153 | $result = []; |
||
| 154 | while ($schemaType) { |
||
| 155 | $className = 'nystudio107\\seomatic\\models\\jsonld\\' . $schemaType; |
||
| 156 | if (class_exists($className)) { |
||
| 157 | $classRef = new ReflectionClass($className); |
||
| 158 | $staticProps = $classRef->getStaticProperties(); |
||
| 159 | |||
| 160 | foreach ($staticProps as $key => $value) { |
||
| 161 | if ($key[0] === '_') { |
||
| 162 | $newKey = ltrim($key, '_'); |
||
| 163 | $staticProps[$newKey] = $value; |
||
| 164 | unset($staticProps[$key]); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | $result[$schemaType] = $staticProps; |
||
| 168 | $schemaType = $staticProps['schemaTypeExtends']; |
||
| 169 | if ($schemaType === 'JsonLdType') { |
||
| 170 | $schemaType = null; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | return $result; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return a flattened, indented menu of the given $path |
||
| 180 | * |
||
| 181 | * @param string $path |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public static function getTypeMenu($path = ''): array |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Return a single menu of schemas starting at $path |
||
| 199 | * |
||
| 200 | * @param string $path |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public static function getSingleTypeMenu($path = ''): array |
||
| 205 | { |
||
| 206 | $result = []; |
||
| 207 | try { |
||
| 208 | $schemaTypes = self::getSchemaArray($path); |
||
| 209 | } catch (Exception $e) { |
||
| 210 | Craft::error($e->getMessage(), __METHOD__); |
||
| 211 | return []; |
||
| 212 | } |
||
| 213 | foreach ($schemaTypes as $key => $value) { |
||
| 214 | $result[$key] = $key; |
||
| 215 | } |
||
| 216 | |||
| 217 | return $result; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public static function getTypeTree(): array |
||
| 224 | { |
||
| 225 | try { |
||
| 226 | $schemaTypes = self::getSchemaTree(); |
||
| 227 | } catch (Exception $e) { |
||
| 228 | Craft::error($e->getMessage(), __METHOD__); |
||
| 229 | return []; |
||
| 230 | } |
||
| 231 | $schemaTypes = self::pruneSchemaTree($schemaTypes, ''); |
||
| 232 | |||
| 233 | // Ignore the top level "Thing" base schema |
||
| 234 | return $schemaTypes['children'] ?? []; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return a hierarchical array of schema types, starting at $path. The $path |
||
| 239 | * is specified as SchemaType.SubSchemaType using SCHEMA_PATH_DELIMITER as |
||
| 240 | * the delimiter. |
||
| 241 | * |
||
| 242 | * @param string $path |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | * @throws Exception |
||
| 246 | */ |
||
| 247 | public static function getSchemaArray($path = ''): array |
||
| 248 | { |
||
| 249 | $dependency = new TagDependency([ |
||
| 250 | 'tags' => [ |
||
| 251 | self::GLOBAL_SCHEMA_CACHE_TAG, |
||
| 252 | self::SCHEMA_CACHE_TAG . 'schemaArray', |
||
| 253 | ], |
||
| 254 | ]); |
||
| 255 | $cache = Craft::$app->getCache(); |
||
| 256 | $typesArray = $cache->getOrSet( |
||
| 257 | self::CACHE_KEY . 'schemaArray', |
||
| 258 | function() use ($path) { |
||
| 259 | Craft::info( |
||
| 260 | 'schemaArray cache miss' . $path, |
||
| 261 | __METHOD__ |
||
| 262 | ); |
||
| 263 | $filePath = Craft::getAlias('@nystudio107/seomatic/resources/schema/tree.jsonld'); |
||
| 264 | $schemaTypes = JsonHelper::decode(@file_get_contents($filePath)); |
||
| 265 | if (empty($schemaTypes)) { |
||
| 266 | throw new Exception(Craft::t('seomatic', 'Schema tree file not found')); |
||
| 267 | } |
||
| 268 | $schemaTypes = self::makeSchemaAssociative($schemaTypes); |
||
| 269 | $schemaTypes = self::orphanChildren($schemaTypes); |
||
| 270 | |||
| 271 | return $schemaTypes; |
||
| 272 | }, |
||
| 273 | Seomatic::$cacheDuration, |
||
| 274 | $dependency |
||
| 275 | ); |
||
| 276 | // Get just the appropriate sub-array |
||
| 277 | if (!empty($path)) { |
||
| 278 | $keys = explode(self::SCHEMA_PATH_DELIMITER, $path); |
||
| 279 | foreach ($keys as $key) { |
||
| 280 | if (!empty($typesArray[$key])) { |
||
| 281 | $typesArray = $typesArray[$key]; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | if (!is_array($typesArray)) { |
||
| 286 | $typesArray = []; |
||
| 287 | } |
||
| 288 | |||
| 289 | return $typesArray; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return the schema layer, and Google Rich Snippet info |
||
| 294 | * |
||
| 295 | * @param string $schemaName |
||
| 296 | * @return array |
||
| 297 | * @throws Exception |
||
| 298 | */ |
||
| 299 | public static function getTypeMetaInfo($schemaName): array |
||
| 300 | { |
||
| 301 | $metaInfo = [ |
||
| 302 | 'schemaPending' => false, |
||
| 303 | 'schemaRichSnippetUrls' => [], |
||
| 304 | ]; |
||
| 305 | $schemaTree = self::getSchemaTree(); |
||
| 306 | $schemaArray = self::pluckSchemaArray($schemaTree, $schemaName); |
||
| 307 | if (!empty($schemaArray)) { |
||
| 308 | $googleRichSnippetTypes = self::getGoogleRichSnippets(); |
||
| 309 | $metaInfo['schemaPending'] = $schemaArray['pending'] ?? false; |
||
| 310 | $metaInfo['schemaRichSnippetUrls'] = $googleRichSnippetTypes[$schemaArray['name']] ?? []; |
||
| 311 | } |
||
| 312 | |||
| 313 | return $metaInfo; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return array |
||
| 318 | * @throws Exception |
||
| 319 | */ |
||
| 320 | public static function getSchemaTree() |
||
| 321 | { |
||
| 322 | $dependency = new TagDependency([ |
||
| 323 | 'tags' => [ |
||
| 324 | self::GLOBAL_SCHEMA_CACHE_TAG, |
||
| 325 | self::SCHEMA_CACHE_TAG . 'schemaTree', |
||
| 326 | ], |
||
| 327 | ]); |
||
| 328 | $cache = Craft::$app->getCache(); |
||
| 329 | $typesArray = $cache->getOrSet( |
||
| 330 | self::CACHE_KEY . 'schemaTree', |
||
| 331 | function() { |
||
| 332 | Craft::info( |
||
| 333 | 'schemaArray cache miss', |
||
| 334 | __METHOD__ |
||
| 335 | ); |
||
| 336 | $filePath = Craft::getAlias('@nystudio107/seomatic/resources/schema/tree.jsonld'); |
||
| 337 | $schemaTree = JsonHelper::decode(@file_get_contents($filePath)); |
||
| 338 | if (empty($schemaTree)) { |
||
| 339 | throw new Exception(Craft::t('seomatic', 'Schema tree file not found')); |
||
| 340 | } |
||
| 341 | |||
| 342 | return $schemaTree; |
||
| 343 | }, |
||
| 344 | Seomatic::$cacheDuration, |
||
| 345 | $dependency |
||
| 346 | ); |
||
| 347 | if (!is_array($typesArray)) { |
||
| 348 | $typesArray = []; |
||
| 349 | } |
||
| 350 | |||
| 351 | return $typesArray; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Traverse the schema tree and pluck a single type array from it |
||
| 356 | * |
||
| 357 | * @param $schemaTree |
||
| 358 | * @param $schemaName |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | protected static function pluckSchemaArray($schemaTree, $schemaName): array |
||
| 362 | { |
||
| 363 | if (!empty($schemaTree['children']) && is_array($schemaTree['children'])) { |
||
| 364 | foreach ($schemaTree['children'] as $key => $value) { |
||
| 365 | if (!empty($value['name']) && $value['name'] === $schemaName) { |
||
| 366 | unset($value['children']); |
||
| 367 | return $value; |
||
| 368 | } |
||
| 369 | if (!empty($value['children'])) { |
||
| 370 | $result = self::pluckSchemaArray($value, $schemaName); |
||
| 371 | if (!empty($result)) { |
||
| 372 | unset($result['children']); |
||
| 373 | return $result; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | return []; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param array $typesArray |
||
| 384 | * @param $indentLevel |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | protected static function flattenSchemaArray(array $typesArray, int $indentLevel): array |
||
| 389 | { |
||
| 390 | $result = []; |
||
| 391 | foreach ($typesArray as $key => $value) { |
||
| 392 | $indent = html_entity_decode(str_repeat(' ', $indentLevel)); |
||
| 393 | if (is_array($value)) { |
||
| 394 | $result[$key] = $indent . $key; |
||
| 395 | $value = self::flattenSchemaArray($value, $indentLevel + self::MENU_INDENT_STEP); |
||
| 396 | $result = array_merge($result, $value); |
||
| 397 | } else { |
||
| 398 | $result[$key] = $indent . $value; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | return $result; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Reduce everything in the $schemaTypes array to a simple hierarchical |
||
| 407 | * array as 'SchemaType' => 'SchemaType' if it has no children, and if it |
||
| 408 | * has children, as 'SchemaType' = [] with an array of sub-types |
||
| 409 | * |
||
| 410 | * @param array $typesArray |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected static function orphanChildren(array $typesArray): array |
||
| 415 | { |
||
| 416 | $result = []; |
||
| 417 | |||
| 418 | if (!empty($typesArray['children']) && is_array($typesArray['children'])) { |
||
| 419 | foreach ($typesArray['children'] as $key => $value) { |
||
| 420 | $key = ''; |
||
| 421 | if (!empty($value['name'])) { |
||
| 422 | $key = $value['name']; |
||
| 423 | } |
||
| 424 | if (!empty($value['children'])) { |
||
| 425 | $value = self::orphanChildren($value); |
||
| 426 | } else { |
||
| 427 | $value = $key; |
||
| 428 | } |
||
| 429 | if (!empty($key)) { |
||
| 430 | $result[$key] = $value; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | return $result; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return a new array that has each type returned as an associative array |
||
| 440 | * as 'SchemaType' => [] rather than the way the tree.jsonld file has it |
||
| 441 | * stored as a non-associative array |
||
| 442 | * |
||
| 443 | * @param array $typesArray |
||
| 444 | * |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | protected static function makeSchemaAssociative(array $typesArray): array |
||
| 448 | { |
||
| 449 | $result = []; |
||
| 450 | |||
| 451 | foreach ($typesArray as $key => $value) { |
||
| 452 | if (isset($value['name'])) { |
||
| 453 | $key = $value['name']; |
||
| 454 | } |
||
| 455 | if (is_array($value)) { |
||
| 456 | $value = self::makeSchemaAssociative($value); |
||
| 457 | } |
||
| 458 | if (isset($value['layer']) && is_string($value['layer'])) { |
||
| 459 | if ($value['layer'] === 'core' || $value['layer'] === 'pending') { |
||
| 460 | $result[$key] = $value; |
||
| 461 | } |
||
| 462 | } else { |
||
| 463 | $result[$key] = $value; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | return $result; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Prune the schema tree by removing everything but `label`, `id`, and `children` |
||
| 472 | * in preparation for use by the treeselect component. Also make the id a namespaced |
||
| 473 | * path to the schema, with the first two higher level schema types, and then the |
||
| 474 | * third final type (skipping any in between) |
||
| 475 | * |
||
| 476 | * @param array $typesArray |
||
| 477 | * @param string $path |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | protected static function pruneSchemaTree(array $typesArray, string $path): array |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get the Google Rich Snippets types & URLs |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | protected static function getGoogleRichSnippets(): array |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | } |
||
| 567 |