| Total Complexity | 40 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DBColour 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 DBColour, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class DBColour extends Color |
||
| 17 | { |
||
| 18 | private static $colour_picker_field_class_name = SelectedColourPickerFormFieldDropdown::class; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * please set |
||
| 22 | * must be defined as #AABB99 (hex codes). |
||
| 23 | * Needs to be set like this: |
||
| 24 | * ```php |
||
| 25 | * [ |
||
| 26 | * '#fff000' => 'My Colour 1', |
||
| 27 | * '#fff000' => 'My Colour 2', |
||
| 28 | * ] |
||
| 29 | * |
||
| 30 | * ``` |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private static $colours = self::DEFAULT_COLOURS; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * You can link colours to other colours. |
||
| 38 | * e.g. |
||
| 39 | * ```php |
||
| 40 | * '#ffffff' => [ |
||
| 41 | * 'link' => '#000000', |
||
| 42 | * 'foreground' => '#000000', |
||
| 43 | * 'background' => '#000000', |
||
| 44 | * ], |
||
| 45 | * '#aabbcc' => [ |
||
| 46 | * 'link' => '#123123', |
||
| 47 | * 'foreground' => '#123312', |
||
| 48 | * 'somethingelse' => '#000000', |
||
| 49 | * ], |
||
| 50 | * ``` |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | |||
| 55 | private static $linked_colours = [ |
||
| 56 | ]; |
||
| 57 | |||
| 58 | protected const DEFAULT_COLOURS = [ |
||
| 59 | '#FF0000' => 'Red', |
||
| 60 | '#0000FF' => 'Blue', |
||
| 61 | '#00FF00' => 'Green', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * please set. |
||
| 66 | * |
||
| 67 | * @var string` |
||
| 68 | */ |
||
| 69 | protected const CSS_CLASS_PREFIX = 'db-colour'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * please set. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected const IS_LIMITED_TO_OPTIONS = true; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * please set. |
||
| 80 | * |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | protected const IS_BG_COLOUR = true; |
||
| 84 | |||
| 85 | private static $casting = [ |
||
| 86 | 'CssVariableDefinition' => 'Varchar', |
||
| 87 | 'CssClass' => 'Varchar', |
||
| 88 | 'CssClassAlternative' => 'Boolean', |
||
| 89 | 'ReadableColor' => 'Varchar', |
||
| 90 | 'RelatedColourByName' => 'Varchar', |
||
| 91 | 'IsDarkColour' => 'Boolean', |
||
| 92 | 'IsLightColour' => 'Boolean', |
||
| 93 | 'Inverted' => 'Boolean', |
||
| 94 | ]; |
||
| 95 | |||
| 96 | |||
| 97 | public function __construct($name = null, $options = []) |
||
| 98 | { |
||
| 99 | parent::__construct($name, $options); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function getCssVariableDefinition(): string |
||
| 103 | { |
||
| 104 | return '--' . $this->getName() . ': ' . $this->getValue() . ';'; |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function my_colours(string $name): array |
||
| 108 | { |
||
| 109 | return self::get_colour_as_db_field(DBColour::class)->getColours(); |
||
| 110 | } |
||
| 111 | |||
| 112 | public static function get_swatches_field(string $name, string $value): LiteralField |
||
| 113 | { |
||
| 114 | return SelectedColourPickerFormFieldSwatches::get_swatches_field( |
||
| 115 | (string) $name, |
||
| 116 | (string) $value, |
||
| 117 | self::my_colours(($name)), |
||
| 118 | static::IS_BG_COLOUR |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * |
||
| 124 | * @param string $name |
||
| 125 | * @param string $title |
||
| 126 | * @return FormField |
||
| 127 | */ |
||
| 128 | public static function get_dropdown_field(string $name, ?string $title = '', ?bool $isBackgroundColour = null) |
||
| 129 | { |
||
| 130 | if($isBackgroundColour === null) { |
||
| 131 | $isBackgroundColour = static::IS_BG_COLOUR; |
||
| 132 | } |
||
| 133 | $className = Config::inst()->get(static::class, 'colour_picker_field_class_name'); |
||
| 134 | return $className::create( |
||
| 135 | $name, |
||
| 136 | $title |
||
| 137 | ) |
||
| 138 | ->setSource(self::my_colours($name)) |
||
| 139 | ->setLimitedToOptions(static::IS_LIMITED_TO_OPTIONS) |
||
| 140 | ->setIsBgColour($isBackgroundColour); |
||
| 141 | ; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | public function get_colours_for_dropdown(?string $name = 'default', ?bool $isBackgroundColour = null): ?array |
||
| 146 | { |
||
| 147 | if($isBackgroundColour === null) { |
||
| 148 | $isBackgroundColour = static::IS_BG_COLOUR; |
||
| 149 | } |
||
| 150 | $colours = self::my_colours($name); |
||
| 151 | if (!empty($colours)) { |
||
| 152 | $array = []; |
||
| 153 | |||
| 154 | foreach ($colours as $code => $label) { |
||
| 155 | $textColor = $this->getIsColorLight($code) ? '#000000' : '#FFFFFF'; |
||
| 156 | if($isBackgroundColour) { |
||
| 157 | $array[$code] = [ |
||
| 158 | 'label' => $label, |
||
| 159 | 'background_css' => $code, |
||
| 160 | 'color_css' => $textColor, |
||
| 161 | 'sample_text' => 'Aa', |
||
| 162 | ]; |
||
| 163 | |||
| 164 | } else { |
||
| 165 | $array[$code] = [ |
||
| 166 | 'label' => $label, |
||
| 167 | 'background_css' => $textColor, |
||
| 168 | 'color_css' => $code, |
||
| 169 | 'sample_text' => 'Aa', |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | return $array; |
||
| 175 | } |
||
| 176 | return null; |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Detects if the given color is light |
||
| 182 | * @param string $colour HEX color code |
||
| 183 | */ |
||
| 184 | public static function get_font_colour(?string $colour = ''): string |
||
| 185 | { |
||
| 186 | return self::is_light_colour((string) $colour) ? '#000000' : '#ffffff'; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $colour HEX color code |
||
| 191 | */ |
||
| 192 | public static function is_dark_colour(?string $colour = ''): bool |
||
| 193 | { |
||
| 194 | return self::is_light_colour((string) $colour) ? false : true; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Detects if the given color is light |
||
| 199 | * @param string $colour HEX color code |
||
| 200 | */ |
||
| 201 | public static function is_light_colour(?string $colour = ''): bool |
||
| 202 | { |
||
| 203 | return DBField::create_field(DBColour::class, $colour) |
||
| 204 | ->Luminance() > 0.5; |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | public static function check_colour(?string $colour, ?bool $isBackgroundColour = false): string |
||
| 209 | { |
||
| 210 | $colour = strtolower($colour); |
||
| 211 | if($colour === 'transparent') { |
||
| 212 | return 'transparent'; |
||
| 213 | } |
||
| 214 | if(! strpos($colour, '#')) { |
||
| 215 | $colour = '#' . $colour; |
||
| 216 | } |
||
| 217 | if(! $colour) { |
||
| 218 | if($isBackgroundColour) { |
||
| 219 | $colour = '#ffffff'; |
||
| 220 | } else { |
||
| 221 | $colour = '#000000'; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | return $colour; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function scaffoldFormField($title = null, $params = null) |
||
| 228 | { |
||
| 229 | return static::get_dropdown_field($this->name, $title); |
||
| 230 | } |
||
| 231 | |||
| 232 | public function getCssClass(?bool $isTransparent = false): string |
||
| 233 | { |
||
| 234 | $colours = $this->getColours(); |
||
| 235 | if($isTransparent) { |
||
| 236 | $name = 'transparent'; |
||
| 237 | } else { |
||
| 238 | $name = $colours[$this->value] ?? 'colour-error'; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $this->classCleanup($name); |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | public function getCssClassAlternative(?bool $isTransparent = false): string |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | public function getReadableColor(): string |
||
| 257 | { |
||
| 258 | // Remove '#' if it's present |
||
| 259 | return $this->getFontColour(); |
||
| 260 | } |
||
| 261 | |||
| 262 | public function getIsLightColour(): bool |
||
| 263 | { |
||
| 264 | return self::is_light_colour($this->value); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function getIsDarkColour(): bool |
||
| 268 | { |
||
| 269 | return self::is_light_colour($this->value) ? false : true; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function Inverted(): string |
||
| 290 | } |
||
| 291 | |||
| 292 | private function classCleanup(string $name): string |
||
| 293 | { |
||
| 294 | $name = str_replace('#', '', $name); |
||
| 295 | $name = preg_replace('#[^A-Za-z0-9]#', '-', $name); |
||
| 296 | |||
| 297 | return static::CSS_CLASS_PREFIX . '-' . trim(trim(strtolower($name), '-')); |
||
| 298 | } |
||
| 299 | |||
| 300 | |||
| 301 | public function getRelatedColourByName(string $name): string |
||
| 302 | { |
||
| 303 | $colours = Config::inst()->get(static::class, 'linked_colours'); |
||
| 304 | $colour = $colours($this->value)[$name] ?? 'error'; |
||
| 305 | return DBField::create_field(DBColour::class, $colour)->getValue(); |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | protected function getColours(): array |
||
| 310 | { |
||
| 311 | return $this->Config()->get('colours'); |
||
| 312 | } |
||
| 313 | |||
| 314 | protected function getMyColours(): array |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | protected static $object_cache = []; |
||
| 323 | |||
| 324 | protected static function get_colour_as_db_field(string $colour) |
||
| 330 | } |
||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | } |
||
| 336 |
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