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 |
||
| 18 | class Packer |
||
| 19 | { |
||
| 20 | private const UTF8_REGEX = '/\A(?: |
||
| 21 | [\x00-\x7F]++ # ASCII |
||
| 22 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
||
| 23 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
||
| 24 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
||
| 25 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
||
| 26 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
||
| 27 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
||
| 28 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
||
| 29 | )*+\z/x'; |
||
| 30 | |||
| 31 | private $isDetectStrBin; |
||
| 32 | private $isForceStr; |
||
| 33 | private $isDetectArrMap; |
||
| 34 | private $isForceArr; |
||
| 35 | private $isForceFloat32; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Packable[]|null |
||
| 39 | */ |
||
| 40 | private $transformers; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param PackOptions|int|null $options |
||
| 44 | * |
||
| 45 | * @throws InvalidOptionException |
||
| 46 | */ |
||
| 47 | 182 | public function __construct($options = null) |
|
| 48 | { |
||
| 49 | 182 | if (null === $options) { |
|
| 50 | 181 | $options = PackOptions::fromDefaults(); |
|
| 51 | 20 | } elseif (!$options instanceof PackOptions) { |
|
| 52 | 20 | $options = PackOptions::fromBitmask($options); |
|
| 53 | } |
||
| 54 | |||
| 55 | 182 | $this->isDetectStrBin = $options->isDetectStrBinMode(); |
|
| 56 | 182 | $this->isForceStr = $options->isForceStrMode(); |
|
| 57 | 182 | $this->isDetectArrMap = $options->isDetectArrMapMode(); |
|
| 58 | 182 | $this->isForceArr = $options->isForceArrMode(); |
|
| 59 | 182 | $this->isForceFloat32 = $options->isForceFloat32Mode(); |
|
| 60 | 182 | } |
|
| 61 | |||
| 62 | 2 | public function registerTransformer(Packable $transformer) : self |
|
| 63 | { |
||
| 64 | 2 | $this->transformers[] = $transformer; |
|
| 65 | |||
| 66 | 2 | return $this; |
|
| 67 | } |
||
| 68 | |||
| 69 | 114 | public function pack($value) |
|
| 70 | { |
||
| 71 | 114 | if (\is_int($value)) { |
|
| 72 | 67 | return $this->packInt($value); |
|
| 73 | } |
||
| 74 | 76 | if (\is_string($value)) { |
|
| 75 | 39 | if ($this->isForceStr) { |
|
| 76 | 33 | return $this->packStr($value); |
|
| 77 | 24 | } |
|
| 78 | 33 | if ($this->isDetectStrBin) { |
|
| 79 | return \preg_match(self::UTF8_REGEX, $value) |
||
| 80 | ? $this->packStr($value) |
||
| 81 | 6 | : $this->packBin($value); |
|
| 82 | } |
||
| 83 | 51 | ||
| 84 | 28 | return $this->packBin($value); |
|
| 85 | 21 | } |
|
| 86 | 12 | if (\is_array($value)) { |
|
| 87 | 21 | if ($this->isDetectArrMap) { |
|
| 88 | return \array_values($value) === $value |
||
| 89 | ? $this->packArray($value) |
||
| 90 | 7 | : $this->packMap($value); |
|
| 91 | } |
||
| 92 | 29 | ||
| 93 | 5 | return $this->isForceArr ? $this->packArray($value) : $this->packMap($value); |
|
| 94 | } |
||
| 95 | 28 | if (null === $value) { |
|
| 96 | 10 | return "\xc0"; |
|
| 97 | } |
||
| 98 | 20 | if (\is_bool($value)) { |
|
| 99 | 6 | return $value ? "\xc3" : "\xc2"; |
|
| 100 | } |
||
| 101 | 14 | if (\is_float($value)) { |
|
| 102 | 10 | return $this->packFloat($value); |
|
| 103 | } |
||
| 104 | 4 | if ($value instanceof Ext) { |
|
| 105 | 2 | return $this->packExt($value->type, $value->data); |
|
| 106 | 2 | } |
|
| 107 | 2 | if ($this->transformers) { |
|
| 108 | foreach ($this->transformers as $transformer) { |
||
| 109 | if (null !== $packed = $transformer->pack($this, $value)) { |
||
| 110 | return $packed; |
||
| 111 | } |
||
| 112 | 3 | } |
|
| 113 | } |
||
| 114 | |||
| 115 | 1 | throw PackingFailedException::unsupportedType($value); |
|
| 116 | } |
||
| 117 | 1 | ||
| 118 | public function packNil() |
||
| 122 | 2 | ||
| 123 | public function packBool($bool) |
||
| 124 | { |
||
| 125 | 98 | return $bool ? "\xc3" : "\xc2"; |
|
| 126 | } |
||
| 127 | 98 | ||
| 128 | 66 | public function packInt($int) |
|
| 129 | 40 | { |
|
| 130 | if ($int >= 0) { |
||
| 131 | 34 | if ($int <= 0x7f) { |
|
| 132 | 12 | return \chr($int); |
|
| 133 | } |
||
| 134 | 26 | if ($int <= 0xff) { |
|
| 135 | 12 | return "\xcc".\chr($int); |
|
| 136 | } |
||
| 137 | 16 | if ($int <= 0xffff) { |
|
| 138 | 10 | return "\xcd".\chr($int >> 8).\chr($int); |
|
| 139 | } |
||
| 140 | if ($int <= 0xffffffff) { |
||
| 141 | 8 | return \pack('CN', 0xce, $int); |
|
| 142 | } |
||
| 143 | |||
| 144 | 34 | return \pack('CJ', 0xcf, $int); |
|
| 145 | 8 | } |
|
| 146 | |||
| 147 | 26 | if ($int >= -0x20) { |
|
| 148 | 6 | return \chr(0xe0 | $int); |
|
| 149 | } |
||
| 150 | 20 | if ($int >= -0x80) { |
|
| 151 | 6 | return "\xd0".\chr($int); |
|
| 152 | } |
||
| 153 | 14 | if ($int >= -0x8000) { |
|
| 154 | 6 | return "\xd1".\chr($int >> 8).\chr($int); |
|
| 155 | } |
||
| 156 | if ($int >= -0x80000000) { |
||
| 157 | 8 | return \pack('CN', 0xd2, $int); |
|
| 158 | } |
||
| 159 | |||
| 160 | 9 | return \pack('CJ', 0xd3, $int); |
|
| 161 | } |
||
| 162 | 9 | ||
| 163 | 1 | public function packFloat($float) |
|
| 169 | 39 | ||
| 170 | public function packStr($str) |
||
| 171 | 39 | { |
|
| 172 | 25 | $length = \strlen($str); |
|
| 173 | |||
| 174 | 14 | if ($length < 32) { |
|
| 186 | 21 | ||
| 187 | public function packBin($str) |
||
| 200 | 20 | ||
| 201 | public function packArray(array $array) |
||
| 211 | 20 | ||
| 212 | 14 | public function packArrayHeader($size) |
|
| 223 | 24 | ||
| 224 | public function packMap(array $map) |
||
| 255 | |||
| 256 | public function packMapHeader($size) |
||
| 267 | |||
| 268 | public function packExt($type, $data) |
||
| 289 | } |
||
| 290 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.