| Total Complexity | 40 |
| Total Lines | 450 |
| 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 |
||
| 45 | class SeoSettings extends Field implements PreviewableFieldInterface |
||
| 46 | { |
||
| 47 | // Constants |
||
| 48 | // ========================================================================= |
||
| 49 | |||
| 50 | public const CACHE_KEY = 'seomatic_fieldmeta_'; |
||
| 51 | |||
| 52 | public const BUNDLE_COMPARE_FIELDS = [ |
||
| 53 | 'metaGlobalVars', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | // Public Properties |
||
| 57 | // ========================================================================= |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $elementDisplayPreviewType = 'google'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | public $generalTabEnabled = true; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | public $generalEnabledFields = [ |
||
| 73 | 'seoTitle', |
||
| 74 | 'seoDescription', |
||
| 75 | 'seoImage', |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | public $twitterTabEnabled = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | public $twitterEnabledFields = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | public $facebookTabEnabled = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | public $facebookEnabledFields = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | public $sitemapTabEnabled = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | public $sitemapEnabledFields = []; |
||
| 107 | |||
| 108 | // Static Methods |
||
| 109 | // ========================================================================= |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | public static function dbType(): array|string|null |
||
| 115 | { |
||
| 116 | return Schema::TYPE_TEXT; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritdoc |
||
| 121 | */ |
||
| 122 | public static function displayName(): string |
||
| 123 | { |
||
| 124 | return Craft::t('seomatic', 'SEO Settings'); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | public static function icon(): string |
||
| 131 | { |
||
| 132 | return '@nystudio107/seomatic/icon-mask.svg'; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritdoc |
||
| 137 | */ |
||
| 138 | public static function phpType(): string |
||
| 139 | { |
||
| 140 | return sprintf('\\%s', MetaBundle::class); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Public Methods |
||
| 144 | // ========================================================================= |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | public function rules(): array |
||
| 150 | { |
||
| 151 | $rules = parent::rules(); |
||
| 152 | $rules = array_merge($rules, [ |
||
| 153 | [ |
||
| 154 | [ |
||
| 155 | 'elementDisplayPreviewType', |
||
| 156 | ], |
||
| 157 | 'string', |
||
| 158 | ], |
||
| 159 | [ |
||
| 160 | [ |
||
| 161 | 'generalTabEnabled', |
||
| 162 | 'twitterTabEnabled', |
||
| 163 | 'facebookTabEnabled', |
||
| 164 | 'sitemapTabEnabled', |
||
| 165 | ], |
||
| 166 | 'boolean', |
||
| 167 | ], |
||
| 168 | [ |
||
| 169 | [ |
||
| 170 | 'generalEnabledFields', |
||
| 171 | 'twitterEnabledFields', |
||
| 172 | 'facebookEnabledFields', |
||
| 173 | 'sitemapEnabledFields', |
||
| 174 | ], |
||
| 175 | 'each', 'rule' => ['string'], |
||
| 176 | ], |
||
| 177 | |||
| 178 | ]); |
||
| 179 | |||
| 180 | return $rules; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritdoc |
||
| 185 | * @since 2.0.0 |
||
| 186 | */ |
||
| 187 | public function useFieldset(): bool |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed |
||
| 196 | { |
||
| 197 | $config = []; |
||
| 198 | // Handle incoming values potentially being JSON, an array, or an object |
||
| 199 | if (!empty($value)) { |
||
| 200 | if (is_string($value)) { |
||
| 201 | // Decode any html entities |
||
| 202 | $value = html_entity_decode($value, ENT_NOQUOTES, 'UTF-8'); |
||
| 203 | $config = Json::decodeIfJson($value); |
||
| 204 | } |
||
| 205 | if (is_array($value)) { |
||
| 206 | $config = $value; |
||
| 207 | } |
||
| 208 | if (is_object($value) && $value instanceof MetaBundle) { |
||
| 209 | $config = $value->toArray(); |
||
| 210 | } |
||
| 211 | } else { |
||
| 212 | /** @var null|Element $element */ |
||
| 213 | $config = MigrationHelper::configFromSeomaticMeta( |
||
| 214 | $element, |
||
| 215 | MigrationHelper::FIELD_MIGRATION_CONTEXT |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | // If the config isn't empty, do some processing on the values |
||
| 219 | if (!empty($config)) { |
||
| 220 | $elementName = ''; |
||
| 221 | /** @var Element $element */ |
||
| 222 | if ($element !== null) { |
||
| 223 | $reflector = new ReflectionClass($element); |
||
| 224 | $elementName = strtolower($reflector->getShortName()); |
||
| 225 | } |
||
| 226 | // Handle the pull fields |
||
| 227 | if (!empty($config['metaGlobalVars']) && !empty($config['metaBundleSettings'])) { |
||
| 228 | PullFieldHelper::parseTextSources( |
||
| 229 | $elementName, |
||
| 230 | $config['metaGlobalVars'], |
||
| 231 | $config['metaBundleSettings'] |
||
| 232 | ); |
||
| 233 | PullFieldHelper::parseImageSources( |
||
| 234 | $elementName, |
||
| 235 | $config['metaGlobalVars'], |
||
| 236 | $config['metaBundleSettings'], |
||
| 237 | null |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | // Handle the mainEntityOfPage |
||
| 241 | $mainEntity = ''; |
||
| 242 | if (in_array('mainEntityOfPage', (array)$this->generalEnabledFields, false) && |
||
| 243 | !empty($config['metaBundleSettings'])) { |
||
| 244 | $mainEntity = SchemaHelper::getSpecificEntityType($config['metaBundleSettings'], true); |
||
| 245 | } |
||
| 246 | if (!empty($config['metaGlobalVars'])) { |
||
| 247 | /* @var array $config */ |
||
| 248 | $config['metaGlobalVars']['mainEntityOfPage'] = $mainEntity; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | // Create a new meta bundle with propagated defaults |
||
| 252 | $metaBundleDefaults = ArrayHelper::merge( |
||
| 253 | ConfigHelper::getConfigFromFile('fieldmeta/Bundle'), |
||
| 254 | $config |
||
| 255 | ); |
||
| 256 | |||
| 257 | return MetaBundle::create($metaBundleDefaults); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritdoc |
||
| 262 | */ |
||
| 263 | public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed |
||
| 264 | { |
||
| 265 | $value = parent::serializeValue($value, $element); |
||
| 266 | if (!Craft::$app->getDb()->getSupportsMb4()) { |
||
| 267 | if (is_string($value)) { |
||
| 268 | // Encode any 4-byte UTF-8 characters. |
||
| 269 | $value = StringHelper::encodeMb4($value); |
||
| 270 | } |
||
| 271 | if (is_array($value)) { |
||
| 272 | array_walk_recursive($value, function(&$arrayValue, $arrayKey) { |
||
| 273 | if ($arrayValue !== null && is_string($arrayValue)) { |
||
| 274 | $arrayValue = StringHelper::encodeMb4($arrayValue); |
||
| 275 | } |
||
| 276 | }); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | return $value; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @inheritdoc |
||
| 285 | */ |
||
| 286 | public function getSettingsHtml(): ?string |
||
| 287 | { |
||
| 288 | $variables = []; |
||
| 289 | $tagOptions = [ |
||
| 290 | 'depends' => [ |
||
| 291 | 'nystudio107\\seomatic\\assetbundles\\seomatic\\SeomaticAsset', |
||
| 292 | ], |
||
| 293 | ]; |
||
| 294 | // JS/CSS modules |
||
| 295 | try { |
||
| 296 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
| 297 | Seomatic::$plugin->vite->register('src/js/seomatic.js', false, $tagOptions, $tagOptions); |
||
| 298 | Seomatic::$plugin->vite->register('src/js/seomatic-meta.js', false, $tagOptions, $tagOptions); |
||
| 299 | } catch (InvalidConfigException $e) { |
||
| 300 | Craft::error($e->getMessage(), __METHOD__); |
||
| 301 | } |
||
| 302 | // Asset bundle |
||
| 303 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
| 304 | '@nystudio107/seomatic/web/assets/dist', |
||
| 305 | true |
||
| 306 | ); |
||
| 307 | $variables['field'] = $this; |
||
| 308 | |||
| 309 | // Render the settings template |
||
| 310 | return Craft::$app->getView()->renderTemplate( |
||
| 311 | 'seomatic/_components/fields/SeoSettings_settings', |
||
| 312 | $variables |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @inheritdoc |
||
| 318 | */ |
||
| 319 | public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @inheritdoc |
||
| 386 | */ |
||
| 387 | public function getPreviewHtml(mixed $value, ElementInterface $element): string |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @inheritdoc |
||
| 444 | */ |
||
| 445 | public function previewPlaceholderHtml(mixed $value, ?ElementInterface $element): string |
||
| 446 | { |
||
| 447 | if ($element !== null) { |
||
| 448 | return $this->getPreviewHtml($value, $element); |
||
| 449 | } |
||
| 450 | |||
| 451 | return ''; |
||
| 452 | } |
||
| 453 | |||
| 454 | // Protected Methods |
||
| 455 | // ========================================================================= |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param Element $element |
||
| 459 | * @param string $groupName |
||
| 460 | * @param array $variables |
||
| 461 | */ |
||
| 462 | protected function setContentFieldSourceVariables( |
||
| 495 | ) |
||
| 496 | ); |
||
| 499 |
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