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 CanPack[] |
||
39 | */ |
||
40 | private $transformers = []; |
||
41 | |||
42 | /** |
||
43 | * @param PackOptions|int|null $options |
||
44 | * @param CanPack[] $transformers |
||
45 | * |
||
46 | * @throws InvalidOptionException |
||
47 | */ |
||
48 | 185 | public function __construct($options = null, array $transformers = []) |
|
49 | { |
||
50 | 185 | if (null === $options) { |
|
51 | 184 | $options = PackOptions::fromDefaults(); |
|
52 | 20 | } elseif (!$options instanceof PackOptions) { |
|
53 | 20 | $options = PackOptions::fromBitmask($options); |
|
54 | } |
||
55 | |||
56 | 185 | $this->isDetectStrBin = $options->isDetectStrBinMode(); |
|
57 | 185 | $this->isForceStr = $options->isForceStrMode(); |
|
58 | 185 | $this->isDetectArrMap = $options->isDetectArrMapMode(); |
|
59 | 185 | $this->isForceArr = $options->isForceArrMode(); |
|
60 | 185 | $this->isForceFloat32 = $options->isForceFloat32Mode(); |
|
61 | |||
62 | 185 | if ([] !== $transformers) { |
|
63 | 1 | $this->transformers = $transformers; |
|
64 | } |
||
65 | 185 | } |
|
66 | |||
67 | 2 | public function extendWith(CanPack $transformer, CanPack ...$transformers) : self |
|
78 | |||
79 | 117 | public function pack($value) |
|
80 | { |
||
81 | 117 | if (\is_int($value)) { |
|
82 | 63 | return $this->packInt($value); |
|
83 | } |
||
84 | 77 | if (\is_string($value)) { |
|
85 | 35 | if ('' === $value) { |
|
86 | 1 | return $this->isForceStr || $this->isDetectStrBin ? "\xa0" : "\xc4\x00"; |
|
87 | } |
||
88 | 34 | if ($this->isForceStr) { |
|
89 | 3 | return $this->packStr($value); |
|
90 | } |
||
91 | 31 | if ($this->isDetectStrBin && \preg_match(self::UTF8_REGEX, $value)) { |
|
92 | 21 | return $this->packStr($value); |
|
93 | } |
||
94 | |||
95 | 14 | return $this->packBin($value); |
|
96 | } |
||
97 | 54 | if (\is_array($value)) { |
|
98 | 29 | if ([] === $value) { |
|
99 | 1 | return $this->isDetectArrMap || $this->isForceArr ? "\x90" : "\x80"; |
|
100 | } |
||
101 | 28 | if ($this->isDetectArrMap) { |
|
102 | 21 | if (!isset($value[0]) && !\array_key_exists(0, $value)) { |
|
103 | 13 | return $this->packMap($value); |
|
104 | } |
||
105 | |||
106 | 12 | return \array_values($value) === $value |
|
107 | 12 | ? $this->packArray($value) |
|
108 | 12 | : $this->packMap($value); |
|
109 | } |
||
110 | |||
111 | 7 | return $this->isForceArr ? $this->packArray($value) : $this->packMap($value); |
|
112 | } |
||
113 | 32 | if (null === $value) { |
|
114 | 7 | return "\xc0"; |
|
115 | } |
||
116 | 29 | if (\is_bool($value)) { |
|
117 | 10 | return $value ? "\xc3" : "\xc2"; |
|
118 | } |
||
119 | 21 | if (\is_float($value)) { |
|
120 | 6 | return $this->packFloat($value); |
|
121 | } |
||
122 | 15 | if ($value instanceof Ext) { |
|
123 | 10 | return $this->packExt($value->type, $value->data); |
|
124 | } |
||
125 | 5 | if ([] !== $this->transformers) { |
|
126 | 3 | foreach ($this->transformers as $transformer) { |
|
127 | 3 | if (null !== $packed = $transformer->pack($this, $value)) { |
|
128 | 2 | return $packed; |
|
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | 3 | throw PackingFailedException::unsupportedType($value); |
|
134 | } |
||
135 | |||
136 | 1 | public function packNil() |
|
137 | { |
||
138 | 1 | return "\xc0"; |
|
139 | } |
||
140 | |||
141 | 2 | public function packBool($bool) |
|
145 | |||
146 | 98 | public function packInt($int) |
|
147 | { |
||
148 | 98 | if ($int >= 0) { |
|
149 | 66 | if ($int <= 0x7f) { |
|
150 | 40 | return \chr($int); |
|
151 | } |
||
152 | 34 | if ($int <= 0xff) { |
|
153 | 12 | return "\xcc".\chr($int); |
|
154 | } |
||
155 | 26 | if ($int <= 0xffff) { |
|
156 | 12 | return "\xcd".\chr($int >> 8).\chr($int); |
|
157 | } |
||
158 | 16 | if ($int <= 0xffffffff) { |
|
159 | 10 | return \pack('CN', 0xce, $int); |
|
160 | } |
||
161 | |||
162 | 8 | return \pack('CJ', 0xcf, $int); |
|
163 | } |
||
164 | |||
165 | 34 | if ($int >= -0x20) { |
|
166 | 8 | return \chr(0xe0 | $int); |
|
167 | } |
||
168 | 26 | if ($int >= -0x80) { |
|
169 | 6 | return "\xd0".\chr($int); |
|
170 | } |
||
171 | 20 | if ($int >= -0x8000) { |
|
172 | 6 | return "\xd1".\chr($int >> 8).\chr($int); |
|
173 | } |
||
174 | 14 | if ($int >= -0x80000000) { |
|
175 | 6 | return \pack('CN', 0xd2, $int); |
|
176 | } |
||
177 | |||
178 | 8 | return \pack('CJ', 0xd3, $int); |
|
179 | } |
||
180 | |||
181 | 9 | public function packFloat($float) |
|
187 | |||
188 | 38 | public function packStr($str) |
|
204 | |||
205 | 21 | public function packBin($str) |
|
206 | { |
||
207 | 21 | $length = \strlen($str); |
|
208 | |||
209 | 21 | if ($length <= 0xff) { |
|
210 | 17 | return "\xc4".\chr($length).$str; |
|
211 | } |
||
212 | 4 | if ($length <= 0xffff) { |
|
213 | 2 | return "\xc5".\chr($length >> 8).\chr($length).$str; |
|
214 | } |
||
215 | |||
216 | 2 | return \pack('CN', 0xc6, $length).$str; |
|
217 | } |
||
218 | |||
219 | 21 | public function packArray($array) |
|
229 | |||
230 | 21 | public function packArrayHeader($size) |
|
241 | |||
242 | 24 | public function packMap($map) |
|
243 | { |
||
244 | 24 | $data = $this->packMapHeader(\count($map)); |
|
245 | |||
273 | |||
274 | 24 | public function packMapHeader($size) |
|
285 | |||
286 | 18 | public function packExt($type, $data) |
|
307 | } |
||
308 |