Complex classes like Phone 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 Phone, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Phone extends Forms\Controls\TextInput |
||
| 44 | 1 | { |
|
| 45 | /** |
||
| 46 | * Define filed attributes |
||
| 47 | */ |
||
| 48 | const FIELD_COUNTRY = 'country'; |
||
| 49 | const FIELD_NUMBER = 'number'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var IPub\Phone\Phone |
||
| 53 | */ |
||
| 54 | private $phoneUtils; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of allowed countries |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $allowedCountries = []; |
||
| 62 | 1 | ||
| 63 | /** |
||
| 64 | * List of allowed phone types |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $allowedTypes = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $number; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $country; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $defaultCountry; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | private static $registered = FALSE; |
||
| 89 | 1 | ||
| 90 | /** |
||
| 91 | * @param PhoneUtils $phoneUtils |
||
| 92 | * @param string|NULL $label |
||
| 93 | * @param int|NULL $maxLength |
||
| 94 | */ |
||
| 95 | public function __construct(PhoneUtils $phoneUtils, $label = NULL, $maxLength = NULL) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param array $countries |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | * |
||
| 107 | * @throws Exceptions\NoValidCountryException |
||
| 108 | */ |
||
| 109 | public function setCountries(array $countries = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $country |
||
| 132 | * |
||
| 133 | 1 | * @return $this |
|
| 134 | * |
||
| 135 | * @throws Exceptions\NoValidCountryException |
||
| 136 | */ |
||
| 137 | public function addCountry($country) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function getCountries() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string|NULL $country |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | * |
||
| 173 | * @throws Exceptions\NoValidCountryException |
||
| 174 | */ |
||
| 175 | public function setDefaultCountry($country = NULL) |
||
| 188 | |||
| 189 | 1 | /** |
|
| 190 | 1 | * @param array $types |
|
| 191 | * |
||
| 192 | * @return $this |
||
| 193 | * |
||
| 194 | * @throws Exceptions\NoValidTypeException |
||
| 195 | */ |
||
| 196 | public function setPhoneTypes(array $types = []) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $type |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | * |
||
| 217 | * @throws Exceptions\NoValidTypeException |
||
| 218 | */ |
||
| 219 | public function addPhoneType($type) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function getPhoneTypes() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | * |
||
| 243 | * @throws Exceptions\InvalidArgumentException |
||
| 244 | */ |
||
| 245 | public function setValue($value) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return string|NULL |
||
| 270 | */ |
||
| 271 | public function getValue() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Loads HTTP data |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function loadHttpData() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getControl() |
||
| 307 | 1 | ||
| 308 | /** |
||
| 309 | 1 | * @param string |
|
| 310 | * |
||
| 311 | * @return Utils\Html |
||
| 312 | * |
||
| 313 | * @throws Exceptions\InvalidArgumentException |
||
| 314 | */ |
||
| 315 | public function getControlPart($key) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return NULL |
||
| 385 | */ |
||
| 386 | public function getLabelPart() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $country |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | * |
||
| 396 | * @throws Exceptions\NoValidCountryException |
||
| 397 | */ |
||
| 398 | protected function validateCountry($country) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $type |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | * |
||
| 416 | * @throws Exceptions\NoValidTypeException |
||
| 417 | */ |
||
| 418 | protected function validateType($type) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param PhoneUtils $phoneUtils |
||
| 433 | * @param string $method |
||
| 434 | */ |
||
| 435 | public static function register(PhoneUtils $phoneUtils, $method = 'addPhone') |
||
| 454 | } |
||
| 455 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.