| Total Complexity | 53 |
| Total Lines | 275 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Characters 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 Characters, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Characters |
||
| 6 | { |
||
| 7 | private $object; |
||
| 8 | private $chars; |
||
| 9 | private $times; |
||
| 10 | private $atFirst; |
||
| 11 | private $atEnd; |
||
| 12 | private $between; |
||
| 13 | private $langsRegex; |
||
| 14 | private $languages; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Constructor |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | public function __construct($object, $value) |
||
| 21 | { |
||
| 22 | $this->object = $object; |
||
| 23 | $this->chars = $this->object->chars->value ?? $this->object->chars->chars ?? null; |
||
| 24 | $this->times = $this->object->chars->times ?? null; |
||
| 25 | $this->atFirst = $this->object->chars->atFirst ?? null; |
||
| 26 | $this->atEnd = $this->object->chars->atEnd ?? null; |
||
| 27 | $this->between = $this->object->chars->between ?? null; |
||
| 28 | $this->languages = $this->object->languages ?? 'english'; |
||
| 29 | $this->langsRegex = $this->object->languages ?? $this->languagesArray('english'); |
||
| 30 | $this->value = $value ?? null; |
||
|
|
|||
| 31 | |||
| 32 | $this->setChars(); |
||
| 33 | $this->setLanguages(); |
||
| 34 | } |
||
| 35 | |||
| 36 | private function isObject() |
||
| 37 | { |
||
| 38 | return is_object($this->object) && count((array) $this->object); |
||
| 39 | } |
||
| 40 | |||
| 41 | private function isCharsString() |
||
| 42 | { |
||
| 43 | return is_string($this->chars); |
||
| 44 | } |
||
| 45 | |||
| 46 | private function isCharsAnArray() |
||
| 47 | { |
||
| 48 | return is_array($this->chars); |
||
| 49 | } |
||
| 50 | |||
| 51 | private function canCharsSeparateViaComma() |
||
| 54 | } |
||
| 55 | |||
| 56 | private function formatCharsViaComma($comma) |
||
| 64 | } |
||
| 65 | |||
| 66 | private function formatCharsString() |
||
| 67 | { |
||
| 68 | return $this->formatCharsViaComma($this->canCharsSeparateViaComma()); |
||
| 69 | } |
||
| 70 | |||
| 71 | private function formatCharsArray() |
||
| 72 | { |
||
| 73 | return implode('', (array) $this->chars); |
||
| 74 | } |
||
| 75 | |||
| 76 | private function setChars() |
||
| 77 | { |
||
| 78 | if ($this->isObject()) { |
||
| 79 | if ($this->isCharsString()) { |
||
| 80 | $this->chars = $this->formatCharsString(); |
||
| 81 | } else if ($this->isCharsAnArray()) { |
||
| 82 | $this->chars = $this->formatCharsArray(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | private function languagesArray($language) |
||
| 88 | { |
||
| 89 | $languages = [ |
||
| 90 | 'all' => '\\p{L}', |
||
| 91 | 'arabic' => '\\x{0621}-\\x{064A}\\x{0660}-\\x{0669} ُ ْ َ ِ ّ~ ً ٍ ٌ', |
||
| 92 | 'english' => 'a-z', |
||
| 93 | 'spanish' => 'a-zñ', |
||
| 94 | 'french' => 'a-zàâçéèêëîïôûùüÿñæœ', |
||
| 95 | 'german' => 'a-zäüöß', |
||
| 96 | ]; |
||
| 97 | return $languages[$language] ?? $languages['english']; |
||
| 98 | } |
||
| 99 | |||
| 100 | private function isLangsAnArray() |
||
| 101 | { |
||
| 102 | return is_array($this->languages); |
||
| 103 | } |
||
| 104 | |||
| 105 | private function isLangsAnString() |
||
| 108 | } |
||
| 109 | |||
| 110 | private function canlangsSeparateViaComma() |
||
| 111 | { |
||
| 112 | return preg_match('/,/', $this->languages) && preg_match_all('/,/', $this->languages); |
||
| 113 | } |
||
| 114 | |||
| 115 | private function loopOverLangsViaComma($comma) |
||
| 116 | { |
||
| 117 | $loopLangs = $comma ? explode(',', $this->languages) : $this->languages; |
||
| 118 | $langsRegex = ''; |
||
| 119 | $languages = ''; |
||
| 120 | foreach ($loopLangs as $language) { |
||
| 121 | $langsRegex .= $this->languagesArray(trim($language)); |
||
| 122 | $languages .= "$language, "; |
||
| 123 | } |
||
| 124 | $languages = rtrim($languages, ", "); |
||
| 125 | return array('languages' => $languages, 'langsRegex' => $langsRegex); |
||
| 126 | } |
||
| 127 | |||
| 128 | private function formatLangsString() |
||
| 129 | { |
||
| 130 | if ($this->canlangsSeparateViaComma()) { |
||
| 131 | extract($this->loopOverLangsViaComma(true)); |
||
| 132 | } else { |
||
| 133 | $langsRegex = $this->languagesArray(trim($this->languages)); |
||
| 134 | $languages = $this->languages; |
||
| 135 | } |
||
| 136 | return array('languages' => $languages, 'langsRegex' => $langsRegex); |
||
| 137 | } |
||
| 138 | |||
| 139 | private function setLanguages() |
||
| 140 | { |
||
| 141 | if ($this->isLangsAnArray()) { |
||
| 142 | extract($this->loopOverLangsViaComma(false)); |
||
| 143 | } else if ($this->isLangsAnString()) { |
||
| 144 | extract($this->formatLangsString()); |
||
| 145 | } |
||
| 146 | $this->languages = $languages; |
||
| 147 | $this->langsRegex = $langsRegex; |
||
| 148 | $this->formatLangsRegex(); |
||
| 149 | } |
||
| 150 | |||
| 151 | private function formatLangsRegex() |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | public function variables() |
||
| 159 | { |
||
| 160 | $chars = $this->chars; |
||
| 161 | $langsRegex = $this->langsRegex; |
||
| 162 | $languages = $this->languages; |
||
| 163 | $times = $this->times; |
||
| 164 | $atFirst = $this->atFirst; |
||
| 165 | $atEnd = $this->atEnd; |
||
| 166 | $between = $this->between; |
||
| 167 | $methods = $this->charactersMethods([ |
||
| 168 | "times" => $times, |
||
| 169 | "atFirst" => $atFirst, |
||
| 170 | "atEnd" => $atEnd, |
||
| 171 | "between" => $between, |
||
| 172 | "chars" => $chars, |
||
| 173 | "value" => $this->value, |
||
| 174 | ]); |
||
| 175 | return [ |
||
| 176 | 'chars' => $chars, |
||
| 177 | 'langsRegex' => $langsRegex, |
||
| 178 | 'languages' => $languages, |
||
| 179 | 'times' => $times, |
||
| 180 | 'atFirst' => $atFirst, |
||
| 181 | 'atEnd' => $atEnd, |
||
| 182 | 'between' => $between, |
||
| 183 | 'methods' => $methods, |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | |||
| 187 | private function charactersMethods($args) |
||
| 188 | { |
||
| 189 | extract($args); |
||
| 190 | return [ |
||
| 191 | 'charactersTimes' => [ |
||
| 192 | [$times, $chars, $value], |
||
| 193 | 'charachters are too many', |
||
| 194 | ], |
||
| 195 | 'charactersAtFirst' => [ |
||
| 196 | [$atFirst, $chars, $value], |
||
| 197 | 'charachters cant be at the first', |
||
| 198 | ], |
||
| 199 | 'charactersAtEnd' => [ |
||
| 200 | [$atEnd, $chars, $value], |
||
| 201 | 'charachters cant be at the end', |
||
| 202 | ], |
||
| 203 | 'charactersBetween' => [ |
||
| 204 | [$between, $chars, $value], |
||
| 205 | 'charachters cant be between', |
||
| 206 | ], |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | private function charactersFormatCharsRegex($chars) |
||
| 211 | { |
||
| 212 | if (strlen($chars) > 1) { |
||
| 213 | $chars = str_split($chars); |
||
| 214 | $chars = "\\" . implode('|\\', $chars); |
||
| 215 | } |
||
| 216 | return $chars; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function charactersAtFirst($atFirst, $chars, $value) |
||
| 220 | { |
||
| 221 | if ($atFirst === false) { |
||
| 222 | $chars = $this->charactersFormatCharsRegex($chars); |
||
| 223 | $re = "/^($chars" . "|\\s+\\$chars)/"; |
||
| 224 | if (preg_match_all($re, $value)) { |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | private function charactersFormatCharsMsg($chars) |
||
| 232 | { |
||
| 233 | $chars = explode('\\', $chars); |
||
| 234 | $chars = implode('', $chars); |
||
| 235 | $chars = $chars ? "[ $chars ] and" : ''; |
||
| 236 | return $chars; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function charactersAtEnd($atEnd, $chars, $value) |
||
| 240 | { |
||
| 241 | if ($atEnd === false) { |
||
| 242 | $chars = $this->charactersFormatCharsRegex($chars); |
||
| 243 | $re = "/($chars" . "|\\$chars\\s+)$/"; |
||
| 244 | if (preg_match_all($re, $value)) { |
||
| 245 | return true; |
||
| 246 | } |
||
| 247 | return false; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | public function charactersBetween($between, $chars, $value) |
||
| 252 | { |
||
| 253 | if ($between === false) { |
||
| 254 | $chars = $this->charactersFormatCharsRegex($chars); |
||
| 255 | $re = "/.+(${chars})(.+|\\s)/"; |
||
| 256 | if (preg_match_all($re, $value)) { |
||
| 257 | return true; |
||
| 258 | } |
||
| 259 | return false; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | public function charactersTimes($times, $chars, $value) |
||
| 264 | { |
||
| 265 | if ($times > 0) { |
||
| 266 | $chars = $this->charactersFormatCharsRegex($chars); |
||
| 267 | $re = "/($chars)/"; |
||
| 268 | if (preg_match($re, $value) && preg_match_all($re, $value) > $times) { |
||
| 269 | return true; |
||
| 270 | } |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function charactersMsg($chars, $languages, $msg) |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 |