Complex classes like Protocol 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 Protocol, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Protocol implements ProtocolInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | protected $type; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $pid; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $destination; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $origin; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $message; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $exception; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $timestamp; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $type |
||
| 44 | * @param string $pid |
||
| 45 | * @param string $destination |
||
| 46 | * @param string $origin |
||
| 47 | * @param string $message |
||
| 48 | * @param string $exception |
||
| 49 | * @param int $timestamp |
||
| 50 | */ |
||
| 51 | 104 | public function __construct($type = '', $pid = '', $destination = '', $origin = '', $message = '', $exception = '', $timestamp = 0) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | */ |
||
| 65 | 104 | public function __destruct() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @override |
||
| 78 | * @inheritDoc |
||
| 79 | */ |
||
| 80 | 6 | public function setType($type, $reassign = false) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @override |
||
| 92 | * @inheritDoc |
||
| 93 | */ |
||
| 94 | 9 | public function setPid($pid, $reassign = false) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @override |
||
| 106 | * @inheritDoc |
||
| 107 | */ |
||
| 108 | 6 | public function setDestination($destination, $reassign = false) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @override |
||
| 120 | * @inheritDoc |
||
| 121 | */ |
||
| 122 | 5 | public function setOrigin($origin, $reassign = false) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @override |
||
| 134 | * @inheritDoc |
||
| 135 | */ |
||
| 136 | 4 | public function setMessage($message, $reassign = false) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @override |
||
| 148 | * @inheritDoc |
||
| 149 | */ |
||
| 150 | 4 | public function setException($exception, $reassign = false) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @override |
||
| 162 | * @inheritDoc |
||
| 163 | */ |
||
| 164 | 26 | public function setTimestamp($timestamp, $reassign = false) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @override |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | 9 | public function getType() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @override |
||
| 185 | * @inheritDoc |
||
| 186 | */ |
||
| 187 | 18 | public function getPid() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @override |
||
| 194 | * @inheritDoc |
||
| 195 | */ |
||
| 196 | 13 | public function getDestination() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @override |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | 14 | public function getOrigin() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @override |
||
| 212 | * @inheritDoc |
||
| 213 | */ |
||
| 214 | 19 | public function getMessage() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @override |
||
| 221 | * @inheritDoc |
||
| 222 | */ |
||
| 223 | 12 | public function getException() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @override |
||
| 230 | * @inheritDoc |
||
| 231 | */ |
||
| 232 | 12 | public function getTimestamp() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @override |
||
| 239 | * @inheritDoc |
||
| 240 | */ |
||
| 241 | 2 | public function setAll($args = [], $reassign = false) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @override |
||
| 256 | * @inheritDoc |
||
| 257 | */ |
||
| 258 | 3 | public function getAll() |
|
| 270 | } |
||
| 271 |