Complex classes like PhoneNumberDesc 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PhoneNumberDesc, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PhoneNumberDesc |
||
| 9 | { |
||
| 10 | protected $hasNationalNumberPattern = false; |
||
| 11 | protected $nationalNumberPattern = ""; |
||
| 12 | protected $hasPossibleNumberPattern = false; |
||
| 13 | protected $possibleNumberPattern = ""; |
||
| 14 | protected $hasExampleNumber = false; |
||
| 15 | protected $exampleNumber = ""; |
||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $possibleLength; |
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $possibleLengthLocalOnly; |
||
| 24 | |||
| 25 | 1377 | public function __construct() |
|
| 26 | { |
||
| 27 | 1377 | $this->clear(); |
|
| 28 | 1377 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @return PhoneNumberDesc |
||
| 32 | */ |
||
| 33 | 1377 | public function clear() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | 3277 | public function getPossibleLength() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $possibleLength |
||
| 54 | */ |
||
| 55 | 1366 | public function setPossibleLength($possibleLength) |
|
| 59 | |||
| 60 | 8 | public function addPossibleLength($possibleLength) |
|
| 61 | { |
||
| 62 | 8 | if (!in_array($possibleLength, $this->possibleLength)) { |
|
| 63 | 8 | $this->possibleLength[] = $possibleLength; |
|
| 64 | 8 | } |
|
| 65 | 8 | } |
|
| 66 | |||
| 67 | 1377 | public function clearPossibleLength() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 3228 | public function getPossibleLengthLocalOnly() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param array $possibleLengthLocalOnly |
||
| 82 | */ |
||
| 83 | 1352 | public function setPossibleLengthLocalOnly($possibleLengthLocalOnly) |
|
| 87 | |||
| 88 | 4 | public function addPossibleLengthLocalOnly($possibleLengthLocalOnly) |
|
| 89 | { |
||
| 90 | 4 | if (!in_array($possibleLengthLocalOnly, $this->possibleLengthLocalOnly)) { |
|
| 91 | 4 | $this->possibleLengthLocalOnly[] = $possibleLengthLocalOnly; |
|
| 92 | 4 | } |
|
| 93 | 4 | } |
|
| 94 | |||
| 95 | 1377 | public function clearPossibleLengthLocalOnly() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | 485 | public function hasNationalNumberPattern() |
|
| 104 | { |
||
| 105 | 485 | return $this->hasNationalNumberPattern; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 2927 | public function getNationalNumberPattern() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $value |
||
| 118 | * @return PhoneNumberDesc |
||
| 119 | */ |
||
| 120 | 1356 | public function setNationalNumberPattern($value) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return PhoneNumberDesc |
||
| 130 | */ |
||
| 131 | 1377 | public function clearNationalNumberPattern() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | 485 | public function hasPossibleNumberPattern() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 1096 | public function getPossibleNumberPattern() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return PhoneNumberDesc |
||
| 156 | */ |
||
| 157 | 1377 | public function clearPossibleNumberPattern() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $value |
||
| 166 | * @return PhoneNumberDesc |
||
| 167 | */ |
||
| 168 | 1358 | public function setPossibleNumberPattern($value) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 5349 | public function hasExampleNumber() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | 3180 | public function getExampleNumber() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $value |
||
| 194 | * @return PhoneNumberDesc |
||
| 195 | */ |
||
| 196 | 1334 | public function setExampleNumber($value) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @return PhoneNumberDesc |
||
| 206 | */ |
||
| 207 | 1377 | public function clearExampleNumber() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param PhoneNumberDesc $other |
||
| 217 | * @return PhoneNumberDesc |
||
| 218 | */ |
||
| 219 | 3 | public function mergeFrom(PhoneNumberDesc $other) |
|
| 220 | { |
||
| 221 | 3 | if ($other->hasNationalNumberPattern()) { |
|
| 222 | 3 | $this->setNationalNumberPattern($other->getNationalNumberPattern()); |
|
| 223 | 3 | } |
|
| 224 | 3 | if ($other->hasPossibleNumberPattern()) { |
|
| 225 | 3 | $this->setPossibleNumberPattern($other->getPossibleNumberPattern()); |
|
| 226 | 3 | } |
|
| 227 | 3 | if ($other->hasExampleNumber()) { |
|
| 228 | 3 | $this->setExampleNumber($other->getExampleNumber()); |
|
| 229 | 3 | } |
|
| 230 | 3 | $this->setPossibleLength($other->getPossibleLength()); |
|
| 231 | 3 | $this->setPossibleLengthLocalOnly($other->getPossibleLengthLocalOnly()); |
|
| 232 | |||
| 233 | 3 | return $this; |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param PhoneNumberDesc $other |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | public function exactlySameAs(PhoneNumberDesc $other) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 482 | public function toArray() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $input |
||
| 271 | * @return PhoneNumberDesc |
||
| 272 | */ |
||
| 273 | 1348 | public function fromArray(array $input) |
|
| 289 | } |
||
| 290 |