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 | 63 | return $this->packInt($value); |
|
| 73 | } |
||
| 74 | 74 | if (\is_string($value)) { |
|
| 75 | 35 | if ($this->isForceStr) { |
|
| 76 | 3 | return $this->packStr($value); |
|
| 77 | } |
||
| 78 | 32 | if ($this->isDetectStrBin) { |
|
| 79 | 29 | return \preg_match(self::UTF8_REGEX, $value) |
|
| 80 | 22 | ? $this->packStr($value) |
|
| 81 | 29 | : $this->packBin($value); |
|
| 82 | } |
||
| 83 | |||
| 84 | 3 | return $this->packBin($value); |
|
| 85 | } |
||
| 86 | 51 | if (\is_array($value)) { |
|
| 87 | 28 | if ($this->isDetectArrMap) { |
|
| 88 | 21 | return \array_values($value) === $value |
|
| 89 | 12 | ? $this->packArray($value) |
|
| 90 | 21 | : $this->packMap($value); |
|
| 91 | } |
||
| 92 | |||
| 93 | 7 | return $this->isForceArr ? $this->packArray($value) : $this->packMap($value); |
|
| 94 | } |
||
| 95 | 29 | if (null === $value) { |
|
| 96 | 5 | return "\xc0"; |
|
| 97 | } |
||
| 98 | 28 | if (\is_bool($value)) { |
|
| 99 | 10 | return $value ? "\xc3" : "\xc2"; |
|
| 100 | } |
||
| 101 | 20 | if (\is_float($value)) { |
|
| 102 | 6 | return $this->packFloat($value); |
|
| 103 | } |
||
| 104 | 14 | if ($value instanceof Ext) { |
|
| 105 | 10 | return $this->packExt($value->type, $value->data); |
|
| 106 | } |
||
| 107 | 4 | if ($this->transformers) { |
|
| 108 | 2 | foreach ($this->transformers as $transformer) { |
|
| 109 | 2 | if (null !== $packed = $transformer->pack($this, $value)) { |
|
| 110 | 2 | return $packed; |
|
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | 3 | throw PackingFailedException::unsupportedType($value); |
|
| 116 | } |
||
| 117 | |||
| 118 | 1 | public function packNil() |
|
| 119 | { |
||
| 120 | 1 | return "\xc0"; |
|
| 121 | } |
||
| 122 | |||
| 123 | 2 | public function packBool($bool) |
|
| 127 | |||
| 128 | 98 | public function packInt($int) |
|
| 129 | { |
||
| 130 | 98 | if ($int >= 0) { |
|
| 131 | 66 | if ($int <= 0x7f) { |
|
| 132 | 40 | return \chr($int); |
|
| 133 | } |
||
| 134 | 34 | if ($int <= 0xff) { |
|
| 135 | 12 | return "\xcc".\chr($int); |
|
| 136 | } |
||
| 137 | 26 | if ($int <= 0xffff) { |
|
| 138 | 12 | return "\xcd".\chr($int >> 8).\chr($int); |
|
| 139 | } |
||
| 140 | 16 | if ($int <= 0xffffffff) { |
|
| 141 | 10 | return \pack('CN', 0xce, $int); |
|
| 142 | } |
||
| 143 | |||
| 144 | 8 | return \pack('CJ', 0xcf, $int); |
|
| 145 | } |
||
| 146 | |||
| 147 | 34 | if ($int >= -0x20) { |
|
| 148 | 8 | return \chr(0xe0 | $int); |
|
| 149 | } |
||
| 150 | 26 | if ($int >= -0x80) { |
|
| 151 | 6 | return "\xd0".\chr($int); |
|
| 152 | } |
||
| 153 | 20 | if ($int >= -0x8000) { |
|
| 154 | 6 | return "\xd1".\chr($int >> 8).\chr($int); |
|
| 155 | } |
||
| 156 | 14 | if ($int >= -0x80000000) { |
|
| 157 | 6 | return \pack('CN', 0xd2, $int); |
|
| 158 | } |
||
| 159 | |||
| 160 | 8 | return \pack('CJ', 0xd3, $int); |
|
| 161 | } |
||
| 162 | |||
| 163 | 9 | public function packFloat($float) |
|
| 164 | { |
||
| 165 | 9 | return $this->isForceFloat32 |
|
| 166 | 1 | ? "\xca".\pack('G', $float) |
|
| 167 | 9 | : "\xcb".\pack('E', $float); |
|
| 168 | } |
||
| 169 | |||
| 170 | 39 | public function packStr($str) |
|
| 171 | { |
||
| 172 | 39 | $length = \strlen($str); |
|
| 173 | |||
| 174 | 39 | if ($length < 32) { |
|
| 175 | 25 | return \chr(0xa0 | $length).$str; |
|
| 176 | } |
||
| 177 | 14 | if ($length <= 0xff) { |
|
| 178 | 8 | return "\xd9".\chr($length).$str; |
|
| 179 | } |
||
| 180 | 6 | if ($length <= 0xffff) { |
|
| 181 | 4 | return "\xda".\chr($length >> 8).\chr($length).$str; |
|
| 182 | } |
||
| 183 | |||
| 184 | 2 | return \pack('CN', 0xdb, $length).$str; |
|
| 185 | } |
||
| 186 | |||
| 187 | 21 | public function packBin($str) |
|
| 188 | { |
||
| 189 | 21 | $length = \strlen($str); |
|
| 190 | |||
| 191 | 21 | if ($length <= 0xff) { |
|
| 192 | 17 | return "\xc4".\chr($length).$str; |
|
| 193 | } |
||
| 194 | 4 | if ($length <= 0xffff) { |
|
| 195 | 2 | return "\xc5".\chr($length >> 8).\chr($length).$str; |
|
| 196 | } |
||
| 197 | |||
| 198 | 2 | return \pack('CN', 0xc6, $length).$str; |
|
| 199 | } |
||
| 200 | |||
| 201 | 20 | public function packArray(array $array) |
|
| 202 | { |
||
| 203 | 20 | $data = $this->packArrayHeader(\count($array)); |
|
| 204 | |||
| 205 | 20 | foreach ($array as $val) { |
|
| 206 | 18 | $data .= $this->pack($val); |
|
| 207 | } |
||
| 208 | |||
| 209 | 20 | return $data; |
|
| 210 | } |
||
| 211 | |||
| 212 | 20 | public function packArrayHeader($size) |
|
| 213 | { |
||
| 214 | 20 | if ($size <= 0xf) { |
|
| 215 | 14 | return \chr(0x90 | $size); |
|
| 216 | } |
||
| 217 | 6 | if ($size <= 0xffff) { |
|
| 218 | 4 | return "\xdc".\chr($size >> 8).\chr($size); |
|
| 219 | } |
||
| 220 | |||
| 221 | 2 | return \pack('CN', 0xdd, $size); |
|
| 222 | } |
||
| 223 | |||
| 224 | 24 | public function packMap(array $map) |
|
| 255 | |||
| 256 | 24 | public function packMapHeader($size) |
|
| 257 | { |
||
| 258 | 24 | if ($size <= 0xf) { |
|
| 259 | 18 | return \chr(0x80 | $size); |
|
| 260 | } |
||
| 261 | 6 | if ($size <= 0xffff) { |
|
| 262 | 4 | return "\xde".\chr($size >> 8).\chr($size); |
|
| 263 | } |
||
| 267 | |||
| 268 | 18 | 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.