| Total Complexity | 40 |
| Total Lines | 451 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
Complex classes like SeoSettings 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 SeoSettings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class SeoSettings extends Field implements PreviewableFieldInterface |
||
| 47 | { |
||
| 48 | // Constants |
||
| 49 | // ========================================================================= |
||
| 50 | |||
| 51 | public const CACHE_KEY = 'seomatic_fieldmeta_'; |
||
| 52 | |||
| 53 | public const BUNDLE_COMPARE_FIELDS = [ |
||
| 54 | 'metaGlobalVars', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | // Public Properties |
||
| 58 | // ========================================================================= |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $elementDisplayPreviewType = 'google'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | public $generalTabEnabled = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | public $generalEnabledFields = [ |
||
| 74 | 'seoTitle', |
||
| 75 | 'seoDescription', |
||
| 76 | 'seoImage', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | public $twitterTabEnabled = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | public $twitterEnabledFields = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | public $facebookTabEnabled = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | public $facebookEnabledFields = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | public $sitemapTabEnabled = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | public $sitemapEnabledFields = []; |
||
| 108 | |||
| 109 | // Static Methods |
||
| 110 | // ========================================================================= |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritdoc |
||
| 114 | */ |
||
| 115 | public static function dbType(): array|string|null |
||
| 116 | { |
||
| 117 | return Schema::TYPE_TEXT; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | public static function displayName(): string |
||
| 124 | { |
||
| 125 | return Craft::t('seomatic', 'SEO Settings'); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | public static function icon(): string |
||
| 132 | { |
||
| 133 | return '@nystudio107/seomatic/icon-mask.svg'; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | public static function phpType(): string |
||
| 140 | { |
||
| 141 | return sprintf('\\%s', MetaBundle::class); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Public Methods |
||
| 145 | // ========================================================================= |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | public function rules(): array |
||
| 151 | { |
||
| 152 | $rules = parent::rules(); |
||
| 153 | $rules = array_merge($rules, [ |
||
| 154 | [ |
||
| 155 | [ |
||
| 156 | 'elementDisplayPreviewType', |
||
| 157 | ], |
||
| 158 | 'string', |
||
| 159 | ], |
||
| 160 | [ |
||
| 161 | [ |
||
| 162 | 'generalTabEnabled', |
||
| 163 | 'twitterTabEnabled', |
||
| 164 | 'facebookTabEnabled', |
||
| 165 | 'sitemapTabEnabled', |
||
| 166 | ], |
||
| 167 | 'boolean', |
||
| 168 | ], |
||
| 169 | [ |
||
| 170 | [ |
||
| 171 | 'generalEnabledFields', |
||
| 172 | 'twitterEnabledFields', |
||
| 173 | 'facebookEnabledFields', |
||
| 174 | 'sitemapEnabledFields', |
||
| 175 | ], |
||
| 176 | 'each', 'rule' => ['string'], |
||
| 177 | ], |
||
| 178 | |||
| 179 | ]); |
||
| 180 | |||
| 181 | return $rules; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | * @since 2.0.0 |
||
| 187 | */ |
||
| 188 | public function useFieldset(): bool |
||
| 189 | { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed |
||
| 197 | { |
||
| 198 | $config = []; |
||
| 199 | // Handle incoming values potentially being JSON, an array, or an object |
||
| 200 | if (!empty($value)) { |
||
| 201 | if (is_string($value)) { |
||
| 202 | // Decode any html entities |
||
| 203 | $value = html_entity_decode($value, ENT_NOQUOTES, 'UTF-8'); |
||
| 204 | $config = Json::decodeIfJson($value); |
||
| 205 | } |
||
| 206 | if (is_array($value)) { |
||
| 207 | $config = $value; |
||
| 208 | } |
||
| 209 | if (is_object($value) && $value instanceof MetaBundle) { |
||
| 210 | $config = $value->toArray(); |
||
| 211 | } |
||
| 212 | } else { |
||
| 213 | /** @var null|Element $element */ |
||
| 214 | $config = MigrationHelper::configFromSeomaticMeta( |
||
| 215 | $element, |
||
| 216 | MigrationHelper::FIELD_MIGRATION_CONTEXT |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | // If the config isn't empty, do some processing on the values |
||
| 220 | if (!empty($config)) { |
||
| 221 | $elementName = ''; |
||
| 222 | /** @var Element $element */ |
||
| 223 | if ($element !== null) { |
||
| 224 | $reflector = new ReflectionClass($element); |
||
| 225 | $elementName = strtolower($reflector->getShortName()); |
||
| 226 | } |
||
| 227 | // Handle the pull fields |
||
| 228 | if (!empty($config['metaGlobalVars']) && !empty($config['metaBundleSettings'])) { |
||
| 229 | PullFieldHelper::parseTextSources( |
||
| 230 | $elementName, |
||
| 231 | $config['metaGlobalVars'], |
||
| 232 | $config['metaBundleSettings'] |
||
| 233 | ); |
||
| 234 | PullFieldHelper::parseImageSources( |
||
| 235 | $elementName, |
||
| 236 | $config['metaGlobalVars'], |
||
| 237 | $config['metaBundleSettings'], |
||
| 238 | null |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | // Handle the mainEntityOfPage |
||
| 242 | $mainEntity = ''; |
||
| 243 | if (in_array('mainEntityOfPage', (array)$this->generalEnabledFields, false) && |
||
| 244 | !empty($config['metaBundleSettings'])) { |
||
| 245 | $mainEntity = SchemaHelper::getSpecificEntityType($config['metaBundleSettings'], true); |
||
| 246 | } |
||
| 247 | if (!empty($config['metaGlobalVars'])) { |
||
| 248 | /* @var array $config */ |
||
| 249 | $config['metaGlobalVars']['mainEntityOfPage'] = $mainEntity; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | // Create a new meta bundle with propagated defaults |
||
| 253 | $metaBundleDefaults = ArrayHelper::merge( |
||
| 254 | ConfigHelper::getConfigFromFile('fieldmeta/Bundle'), |
||
| 255 | $config |
||
| 256 | ); |
||
| 257 | |||
| 258 | return MetaBundle::create($metaBundleDefaults); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @inheritdoc |
||
| 263 | */ |
||
| 264 | public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed |
||
| 265 | { |
||
| 266 | $value = parent::serializeValue($value, $element); |
||
| 267 | if (!Craft::$app->getDb()->getSupportsMb4()) { |
||
| 268 | if (is_string($value)) { |
||
| 269 | // Encode any 4-byte UTF-8 characters. |
||
| 270 | $value = StringHelper::encodeMb4($value); |
||
| 271 | } |
||
| 272 | if (is_array($value)) { |
||
| 273 | array_walk_recursive($value, function(&$arrayValue, $arrayKey) { |
||
| 274 | if ($arrayValue !== null && is_string($arrayValue)) { |
||
| 275 | $arrayValue = StringHelper::encodeMb4($arrayValue); |
||
| 276 | } |
||
| 277 | }); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | return $value; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | */ |
||
| 287 | public function getSettingsHtml(): ?string |
||
| 288 | { |
||
| 289 | $variables = []; |
||
| 290 | $tagOptions = [ |
||
| 291 | 'depends' => [ |
||
| 292 | 'nystudio107\\seomatic\\assetbundles\\seomatic\\SeomaticAsset', |
||
| 293 | ], |
||
| 294 | ]; |
||
| 295 | // JS/CSS modules |
||
| 296 | try { |
||
| 297 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
| 298 | Seomatic::$plugin->vite->register('src/js/seomatic.js', false, $tagOptions, $tagOptions); |
||
| 299 | Seomatic::$plugin->vite->register('src/js/seomatic-meta.js', false, $tagOptions, $tagOptions); |
||
| 300 | } catch (InvalidConfigException $e) { |
||
| 301 | Craft::error($e->getMessage(), __METHOD__); |
||
| 302 | } |
||
| 303 | // Asset bundle |
||
| 304 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
| 305 | '@nystudio107/seomatic/web/assets/dist', |
||
| 306 | true |
||
| 307 | ); |
||
| 308 | $variables['field'] = $this; |
||
| 309 | |||
| 310 | // Render the settings template |
||
| 311 | return Craft::$app->getView()->renderTemplate( |
||
| 312 | 'seomatic/_components/fields/SeoSettings_settings', |
||
| 313 | $variables |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @inheritdoc |
||
| 319 | */ |
||
| 320 | public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
||
| 321 | { |
||
| 322 | $variables = []; |
||
| 323 | // JS/CSS modules |
||
| 324 | $tagOptions = [ |
||
| 325 | 'depends' => [ |
||
| 326 | 'nystudio107\\seomatic\\assetbundles\\seomatic\\SeomaticAsset', |
||
| 327 | ], |
||
| 328 | ]; |
||
| 329 | // JS/CSS modules |
||
| 330 | try { |
||
| 331 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
| 332 | Seomatic::$plugin->vite->register('src/js/seomatic.js', false, $tagOptions, $tagOptions); |
||
| 333 | Seomatic::$plugin->vite->register('src/js/seomatic-meta.js', false, $tagOptions, $tagOptions); |
||
| 334 | } catch (InvalidConfigException $e) { |
||
| 335 | Craft::error($e->getMessage(), __METHOD__); |
||
| 336 | } |
||
| 337 | // Asset bundle |
||
| 338 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
| 339 | '@nystudio107/seomatic/web/assets/dist', |
||
| 340 | true |
||
| 341 | ); |
||
| 342 | // Basic variables |
||
| 343 | $variables['name'] = $this->handle; |
||
| 344 | $variables['value'] = $value; |
||
| 345 | $variables['field'] = $this; |
||
| 346 | $variables['currentSourceBundleType'] = 'entry'; |
||
| 347 | $variables['entitySchemaPath'] = SchemaHelper::getEntityPath($value->metaBundleSettings); |
||
| 348 | |||
| 349 | // Get our id and namespace |
||
| 350 | $id = Craft::$app->getView()->formatInputId($this->handle); |
||
| 351 | $nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
||
| 352 | $variables['id'] = $id; |
||
| 353 | $variables['nameSpacedId'] = $nameSpacedId; |
||
| 354 | |||
| 355 | // Make sure the *Sources variables at least exist, for things like the QuickPost widget |
||
| 356 | $variables['textFieldSources'] = []; |
||
| 357 | $variables['assetFieldSources'] = []; |
||
| 358 | $variables['assetVolumeTextFieldSources'] = []; |
||
| 359 | $variables['userFieldSources'] = []; |
||
| 360 | // Pull field sources |
||
| 361 | if ($element !== null) { |
||
| 362 | /** @var Element $element */ |
||
| 363 | $this->setContentFieldSourceVariables($element, 'Entry', $variables); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** @var MetaBundle $value */ |
||
| 367 | $variables['elementType'] = Asset::class; |
||
| 368 | $variables['assetVolumeSources'] = AssetHelper::getAssetInputSources(); |
||
| 369 | $variables['parentBundles'] = []; |
||
| 370 | // Preview the containers so the preview is correct in the field |
||
| 371 | if ($element !== null && $element->uri !== null) { |
||
| 372 | Seomatic::$plugin->metaContainers->previewMetaContainers($element->uri, $element->siteId, true, true, $element); |
||
| 373 | $contentMeta = Seomatic::$plugin->metaBundles->getContentMetaBundleForElement($element); |
||
| 374 | $globalMeta = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($element->siteId); |
||
| 375 | $variables['parentBundles'] = [$contentMeta, $globalMeta]; |
||
| 376 | } |
||
| 377 | |||
| 378 | // Render the input template |
||
| 379 | return Craft::$app->getView()->renderTemplate( |
||
| 380 | 'seomatic/_components/fields/SeoSettings_input', |
||
| 381 | $variables |
||
| 382 | ); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @inheritdoc |
||
| 387 | */ |
||
| 388 | public function getPreviewHtml(mixed $value, ElementInterface $element): string |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @inheritdoc |
||
| 446 | */ |
||
| 447 | public function previewPlaceholderHtml(mixed $value, ?ElementInterface $element): string |
||
| 448 | { |
||
| 449 | if ($element !== null) { |
||
| 450 | return $this->getPreviewHtml($value, $element); |
||
| 451 | } |
||
| 452 | |||
| 453 | return ''; |
||
| 454 | } |
||
| 455 | |||
| 456 | // Protected Methods |
||
| 457 | // ========================================================================= |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param Element $element |
||
| 461 | * @param string $groupName |
||
| 462 | * @param array $variables |
||
| 463 | */ |
||
| 464 | protected function setContentFieldSourceVariables( |
||
| 497 | ) |
||
| 498 | ); |
||
| 499 | } |
||
| 500 | } |
||
| 501 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths