Complex classes like Inflater 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 Inflater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Inflater |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The smallest allowable match length. |
||
| 15 | * |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | const MIN_MATCH = 2; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The number of uncompressed character types. |
||
| 22 | * |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | const NUM_CHARS = 256; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Block type: verbatim. |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | const BLOCKTYPE_VERBATIM = 1; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Block type: aligned offset. |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | const BLOCKTYPE_ALIGNED = 2; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Block type: uncompressed. |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | const BLOCKTYPE_UNCOMPRESSED = 3; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The number of elements in the aligned offset tree. |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | const ALIGNED_NUM_ELEMENTS = 8; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Unknown. |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | const NUM_PRIMARY_LENGTHS = 7; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The number of elements in the length tree. |
||
| 64 | * |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | const NUM_SECONDARY_LENGTHS = 249; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The index matrix of the position slot bases. |
||
| 71 | * |
||
| 72 | * @var int[] |
||
| 73 | */ |
||
| 74 | protected static $POSITION_BASE = array( |
||
| 75 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, |
||
| 76 | 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, |
||
| 77 | 65536, 98304, 131072, 196608, 262144, 393216, 524288, 655360, 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864, 1703936, |
||
| 78 | 1835008, 1966080, 2097152, |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Number of needed bits for offset-from-base data. |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected static $EXTRA_BITS = array( |
||
| 87 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
||
| 88 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, |
||
| 89 | 15, 15, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
||
| 90 | 17, 17, 17, |
||
| 91 | ); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The size of the actual decoding window. |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected $windowSize; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The actual decoding window. |
||
| 102 | * |
||
| 103 | * @var int[] |
||
| 104 | */ |
||
| 105 | protected $window; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The current offset within the window. |
||
| 109 | * |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | protected $windowPosition; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The most recent non-repeated offset (LRU offset system). |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | protected $r0; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The second most recent non-repeated offset (LRU offset system). |
||
| 123 | * |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | protected $r1; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The third most recent non-repeated offset (LRU offset system). |
||
| 130 | * |
||
| 131 | * @var int |
||
| 132 | */ |
||
| 133 | protected $r2; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The number of main tree elements. |
||
| 137 | * |
||
| 138 | * @var int |
||
| 139 | */ |
||
| 140 | protected $mainElements; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Decoding has already been started? |
||
| 144 | * |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | protected $headerRead; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The type of the current block. |
||
| 151 | * |
||
| 152 | * @var int |
||
| 153 | */ |
||
| 154 | protected $blockType; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The uncompressed length of the current block. |
||
| 158 | * |
||
| 159 | * @var int |
||
| 160 | */ |
||
| 161 | protected $blockLength; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The number of ncompressed bytes still left to decode in the current block. |
||
| 165 | * |
||
| 166 | * @var int |
||
| 167 | */ |
||
| 168 | protected $remainingInBlock; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The number of CFDATA blocks processed. |
||
| 172 | * |
||
| 173 | * @var int |
||
| 174 | */ |
||
| 175 | protected $framesRead; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The magic header value used for transform (0 if not encoded). |
||
| 179 | * |
||
| 180 | * @var int |
||
| 181 | */ |
||
| 182 | protected $intelFilesize; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The current offset in transform space. |
||
| 186 | * |
||
| 187 | * @var int |
||
| 188 | */ |
||
| 189 | protected $intelCurrentPosition; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Have we seen any translatable data yet? |
||
| 193 | * |
||
| 194 | * @var bool |
||
| 195 | */ |
||
| 196 | protected $intelStarted; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * The main Huffman tree. |
||
| 200 | * |
||
| 201 | * @var \CHMLib\LZX\Tree |
||
| 202 | */ |
||
| 203 | protected $mainTree; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * The length Huffman tree. |
||
| 207 | * |
||
| 208 | * @var \CHMLib\LZX\Tree |
||
| 209 | */ |
||
| 210 | protected $lengthTree; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * The aligned offset Huffman tree. |
||
| 214 | * |
||
| 215 | * @var \CHMLib\LZX\Tree |
||
| 216 | */ |
||
| 217 | protected $alignedTree; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Initializes the instance. |
||
| 221 | * |
||
| 222 | * @param int $windowSize The window size (determines the number of window subdivisions, or "position slots"), |
||
| 223 | * |
||
| 224 | * @throws \Exception Throws an Exception in case of errors. |
||
| 225 | */ |
||
| 226 | 483 | public function __construct($windowSize) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Uncompress bytes. |
||
| 256 | * |
||
| 257 | * @param bool $reset Reset the LXZ state? |
||
| 258 | * @param \CHMLib\Reader\BitReader $reader The reader that provides the data. |
||
| 259 | * @param int $numberOfBytes The number of decompressed bytes to retrieve. |
||
| 260 | * |
||
| 261 | * @throws \Exception Throws an Exception in case of errors. |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 26 | public function inflate($reset, BitReader $reader, $numberOfBytes) |
|
| 475 | } |
||
| 476 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.