Complex classes like ContactTrait 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 ContactTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | trait ContactTrait |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * this was once needed as the conversion done by COZA was faulty |
||
| 20 | * the bug has since been fixed but this remains to allow testing |
||
| 21 | * set true to force ascii usage on type=loc (which should allow UTF8) |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $force_ascii = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * set true to skip the generation of type=int (like .MX) |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $skip_int = false; |
||
| 33 | |||
| 34 | abstract public function set($path = null, $value = null); |
||
| 35 | |||
| 36 | public function forceAscii() |
||
| 40 | |||
| 41 | public function skipInt() |
||
| 45 | |||
| 46 | public function appendId($path, $id) |
||
| 50 | |||
| 51 | public function appendName($path, $name) |
||
| 52 | { |
||
| 53 | if ($this->force_ascii) { |
||
| 54 | $this->set(sprintf($path, 'loc'), Translit::transliterate($name)); |
||
| 55 | } else { |
||
| 56 | $this->set(sprintf($path, 'loc'), $name); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (!$this->skip_int) { |
||
| 60 | $this->set(sprintf($path, 'int'), Translit::transliterate($name)); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | public function appendOrganization($path, $org) |
||
| 65 | { |
||
| 66 | if ($this->force_ascii) { |
||
| 67 | $this->set(sprintf($path, 'loc'), Translit::transliterate($org)); |
||
| 68 | } else { |
||
| 69 | $this->set(sprintf($path, 'loc'), $org); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!$this->skip_int) { |
||
| 73 | $this->set(sprintf($path, 'int'), Translit::transliterate($org)); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | public function appendStreet($path, $street) |
||
| 78 | { |
||
| 79 | if ($this->force_ascii) { |
||
| 80 | $this->set(sprintf($path, 'loc'), Translit::transliterate($street)); |
||
| 81 | } else { |
||
| 82 | $this->set(sprintf($path, 'loc'), $street); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (!$this->skip_int) { |
||
| 86 | $this->set(sprintf($path, 'int'), Translit::transliterate($street)); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | public function appendCity($path, $city) |
||
| 91 | { |
||
| 92 | if ($this->force_ascii) { |
||
| 93 | $this->set(sprintf($path, 'loc'), Translit::transliterate($city)); |
||
| 94 | } else { |
||
| 95 | $this->set(sprintf($path, 'loc'), $city); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!$this->skip_int) { |
||
| 99 | $this->set(sprintf($path, 'int'), Translit::transliterate($city)); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function appendProvince($path, $sp) |
||
| 104 | { |
||
| 105 | if ($this->force_ascii) { |
||
| 106 | $this->set(sprintf($path, 'loc'), Translit::transliterate($sp)); |
||
| 107 | } else { |
||
| 108 | $this->set(sprintf($path, 'loc'), $sp); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (!$this->skip_int) { |
||
| 112 | $this->set(sprintf($path, 'int'), Translit::transliterate($sp)); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | public function appendPostalCode($path, $pc) |
||
| 117 | { |
||
| 118 | if ($this->force_ascii) { |
||
| 119 | $this->set(sprintf($path, 'loc'), Translit::transliterate($pc)); |
||
| 120 | } else { |
||
| 121 | $this->set(sprintf($path, 'loc'), $pc); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (!$this->skip_int) { |
||
| 125 | $this->set(sprintf($path, 'int'), Translit::transliterate($pc)); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | public function appendCountryCode($path, $cc) |
||
| 145 | |||
| 146 | public function appendVoice($path, $voice) |
||
| 150 | |||
| 151 | public function appendFax($path, $fax) |
||
| 155 | |||
| 156 | public function appendEmail($path, $email) |
||
| 157 | { |
||
| 158 | if (!Validator::isEmail($email)) { |
||
| 159 | throw new Exception(sprintf('%s is not a valid email', $email)); |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->set($path, $email); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function appendAuthInfo($path, $pw = null) |
||
| 166 | { |
||
| 167 | if ($pw === null) { |
||
| 168 | $pw = Random::auth(12); |
||
| 169 | } |
||
| 170 | $this->set($path, $pw); |
||
| 171 | return $pw; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function appendDisclose($path) |
||
| 178 | |||
| 179 | public function appendRole($path, $role) |
||
| 180 | { |
||
| 181 | $role = Validator::isValidContactRole($role); |
||
| 182 | |||
| 183 | if ($role === false) { |
||
| 184 | throw new Exception(sprintf('%s is not a valid contact role', $role)); |
||
| 185 | } |
||
| 186 | |||
| 189 | |||
| 190 | public function appendType($path, $type) |
||
| 200 | |||
| 201 | public function appendLegalEmail($path, $email) |
||
| 211 | |||
| 212 | public function appendFirstName($path, $firstName) |
||
| 224 | |||
| 225 | public function appendLastName($path, $lastName) |
||
| 237 | |||
| 238 | public function appendRegisterNumber($path, $number) |
||
| 250 | |||
| 251 | public function appendIdentity($path, $identity) |
||
| 263 | |||
| 264 | private function appendIsFinish($path, $finish) |
||
| 276 | |||
| 277 | public function appendBirthDay($path, $date) |
||
| 289 | |||
| 290 | public function appendGender($path, $gender) |
||
| 302 | } |