Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Packer 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 Packer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Packer |
||
| 18 | { |
||
| 19 | const FORCE_STR = 0b0001; |
||
| 20 | const FORCE_BIN = 0b0010; |
||
| 21 | const FORCE_ARR = 0b0100; |
||
| 22 | const FORCE_MAP = 0b1000; |
||
| 23 | |||
| 24 | private $strDetectionMode; |
||
| 25 | private $arrDetectionMode; |
||
| 26 | |||
| 27 | const NON_UTF8_REGEX = '/( |
||
| 28 | [\xC0-\xC1] # Invalid UTF-8 Bytes |
||
| 29 | | [\xF5-\xFF] # Invalid UTF-8 Bytes |
||
| 30 | | \xE0[\x80-\x9F] # Overlong encoding of prior code point |
||
| 31 | | \xF0[\x80-\x8F] # Overlong encoding of prior code point |
||
| 32 | | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start |
||
| 33 | | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start |
||
| 34 | | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start |
||
| 35 | | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle |
||
| 36 | | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence |
||
| 37 | | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence |
||
| 38 | | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence |
||
| 39 | | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) |
||
| 40 | )/x'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Collection |
||
| 44 | */ |
||
| 45 | private $transformers; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param int|null $typeDetectionMode |
||
| 49 | */ |
||
| 50 | 91 | public function __construct($typeDetectionMode = null) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param int $mode |
||
| 59 | */ |
||
| 60 | 13 | public function setTypeDetectionMode($mode) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param Collection|null $transformers |
||
| 72 | */ |
||
| 73 | 2 | public function setTransformers(Collection $transformers = null) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @return Collection|null |
||
| 80 | */ |
||
| 81 | 1 | public function getTransformers() |
|
| 85 | |||
| 86 | 85 | public function pack($value) |
|
| 134 | |||
| 135 | 10 | public function packArray(array $array) |
|
| 146 | |||
| 147 | 10 | View Code Duplication | private static function packArrayHeader($size) |
| 158 | |||
| 159 | 12 | public function packMap(array $map) |
|
| 171 | |||
| 172 | 12 | View Code Duplication | private static function packMapHeader($size) |
| 183 | |||
| 184 | 20 | public function packStr($str) |
|
| 200 | |||
| 201 | 11 | public function packBin($str) |
|
| 214 | |||
| 215 | 10 | public function packExt(Ext $ext) |
|
| 238 | |||
| 239 | 3 | public function packNil() |
|
| 243 | |||
| 244 | 6 | public function packBool($val) |
|
| 248 | |||
| 249 | 3 | public function packFloat($num) |
|
| 253 | |||
| 254 | 47 | public function packInt($num) |
|
| 288 | |||
| 289 | 6 | private static function packUint64($code, $num) |
|
| 296 | } |
||
| 297 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.