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 QRCode 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 QRCode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class QRCode{ |
||
| 23 | |||
| 24 | /** |
||
| 25 | * API constants |
||
| 26 | */ |
||
| 27 | const OUTPUT_STRING_TEXT = 'txt'; |
||
| 28 | const OUTPUT_STRING_JSON = 'json'; |
||
| 29 | |||
| 30 | const OUTPUT_MARKUP_HTML = 'html'; |
||
| 31 | const OUTPUT_MARKUP_SVG = 'svg'; |
||
| 32 | # const OUTPUT_MARKUP_XML = 'xml'; // anyone? |
||
| 33 | |||
| 34 | const OUTPUT_IMAGE_PNG = 'png'; |
||
| 35 | const OUTPUT_IMAGE_JPG = 'jpg'; |
||
| 36 | const OUTPUT_IMAGE_GIF = 'gif'; |
||
| 37 | |||
| 38 | const ERROR_CORRECT_LEVEL_L = 1; // 7%. |
||
| 39 | const ERROR_CORRECT_LEVEL_M = 0; // 15%. |
||
| 40 | const ERROR_CORRECT_LEVEL_Q = 3; // 25%. |
||
| 41 | const ERROR_CORRECT_LEVEL_H = 2; // 30%. |
||
| 42 | |||
| 43 | // max bits @ ec level L:07 M:15 Q:25 H:30 % |
||
| 44 | const TYPE_01 = 1; // 152 128 104 72 |
||
| 45 | const TYPE_02 = 2; // 272 224 176 128 |
||
| 46 | const TYPE_03 = 3; // 440 352 272 208 |
||
| 47 | const TYPE_04 = 4; // 640 512 384 288 |
||
| 48 | const TYPE_05 = 5; // 864 688 496 368 |
||
| 49 | const TYPE_06 = 6; // 1088 864 608 480 |
||
| 50 | const TYPE_07 = 7; // 1248 992 704 528 |
||
| 51 | const TYPE_08 = 8; // 1552 1232 880 688 |
||
| 52 | const TYPE_09 = 9; // 1856 1456 1056 800 |
||
| 53 | const TYPE_10 = 10; // 2192 1728 1232 976 |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $matrix = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $pixelCount = 0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $typeNumber; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $errorCorrectLevel; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $lostPoint; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | protected $darkCount; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var float |
||
| 87 | */ |
||
| 88 | protected $minLostPoint; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | protected $maskPattern; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var \chillerlan\QRCode\BitBuffer |
||
| 97 | */ |
||
| 98 | protected $bitBuffer; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var \chillerlan\QRCode\Data\QRDataInterface |
||
| 102 | */ |
||
| 103 | protected $qrDataInterface; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var \chillerlan\QRCode\Output\QROutputInterface |
||
| 107 | */ |
||
| 108 | protected $qrOutputInterface; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * QRCode constructor. |
||
| 112 | * |
||
| 113 | * @param string $data |
||
| 114 | * @param \chillerlan\QRCode\Output\QROutputInterface $output |
||
| 115 | * @param \chillerlan\QRCode\QROptions|null $options |
||
| 116 | */ |
||
| 117 | public function __construct($data, QROutputInterface $output, QROptions $options = null){ |
||
| 118 | $this->qrOutputInterface = $output; |
||
| 119 | $this->bitBuffer = new BitBuffer; |
||
| 120 | $this->setData($data, $options); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $data |
||
| 125 | * @param \chillerlan\QRCode\QROptions|null $options |
||
| 126 | * |
||
| 127 | * @return \chillerlan\QRCode\QRCode |
||
| 128 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 129 | */ |
||
| 130 | public function setData(string $data, QROptions $options = null):QRCode { |
||
| 131 | $data = trim($data); |
||
| 132 | |||
| 133 | if(empty($data)){ |
||
| 134 | throw new QRCodeException('No data given.'); |
||
| 135 | } |
||
| 136 | |||
| 137 | if(!$options instanceof QROptions){ |
||
| 138 | $options = new QROptions; |
||
| 139 | } |
||
| 140 | |||
| 141 | if(!in_array($options->errorCorrectLevel, QRConst::RSBLOCK, true)){ |
||
| 142 | throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel); |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->errorCorrectLevel = $options->errorCorrectLevel; |
||
| 146 | |||
| 147 | switch(true){ |
||
| 148 | case Util::isAlphaNum($data): |
||
| 149 | $mode = Util::isNumber($data) ? QRConst::MODE_NUMBER : QRConst::MODE_ALPHANUM; |
||
| 150 | break; |
||
| 151 | case Util::isKanji($data): |
||
| 152 | $mode = QRConst::MODE_KANJI; |
||
| 153 | break; |
||
| 154 | default: |
||
| 155 | $mode = QRConst::MODE_BYTE; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | |||
| 159 | // see, Scrunitizer, it is concrete! :P |
||
| 160 | $qrDataInterface = [ |
||
| 161 | QRConst::MODE_ALPHANUM => AlphaNum::class, |
||
| 162 | QRConst::MODE_BYTE => Byte::class, |
||
| 163 | QRConst::MODE_KANJI => Kanji::class, |
||
| 164 | QRConst::MODE_NUMBER => Number::class, |
||
| 165 | ][$mode]; |
||
| 166 | |||
| 167 | $this->qrDataInterface = new $qrDataInterface($data); |
||
| 168 | $this->typeNumber = $options->typeNumber; |
||
| 169 | |||
| 170 | if(!is_int($this->typeNumber) || $this->typeNumber < 1 || $this->typeNumber > 10){ |
||
| 171 | $this->typeNumber = $this->getTypeNumber($mode); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param int $mode |
||
| 179 | * |
||
| 180 | * @return int |
||
| 181 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 182 | */ |
||
| 183 | protected function getTypeNumber(int $mode):int { |
||
| 184 | $length = $this->qrDataInterface->dataLength; |
||
|
|
|||
| 185 | |||
| 186 | if($this->qrDataInterface->mode === QRConst::MODE_KANJI){ |
||
| 187 | $length = floor($length / 2); |
||
| 188 | } |
||
| 189 | |||
| 190 | foreach(range(1, 10) as $type){ |
||
| 191 | if($length <= Util::getMaxLength($type, $mode, $this->errorCorrectLevel)){ |
||
| 192 | return $type; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | throw new QRCodeException('Unable to determine type number.'); // @codeCoverageIgnore |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function output(){ |
||
| 203 | $this->qrOutputInterface->setMatrix($this->getRawData()); |
||
| 204 | |||
| 205 | return $this->qrOutputInterface->dump(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getRawData():array { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param array $range |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function testLevel1(array $range){ |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param array $range |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | protected function testLevel2(array $range){ |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $range1 |
||
| 292 | * @param array $range2 |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | protected function testLevel3(array $range1, array $range2){ |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param array $range |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | protected function testLevel4(array $range){ |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param int $pattern |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | protected function testPattern(int $pattern){ |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param bool $test |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | protected function setTypeNumber(bool $test){ |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param bool $test |
||
| 398 | * @param int $pattern |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | protected function setTypeInfo(bool $test, int $pattern){ |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return void |
||
| 430 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 431 | */ |
||
| 432 | protected function createData(){ |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return array |
||
| 480 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 481 | */ |
||
| 482 | protected function createBytes():array { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param int $pattern |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 552 | */ |
||
| 553 | protected function mapData(int $pattern){ |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @return void |
||
| 618 | */ |
||
| 619 | protected function setupPositionProbePattern(){ |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @return void |
||
| 645 | */ |
||
| 646 | protected function setupPositionAdjustPattern(){ |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @return void |
||
| 670 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 671 | */ |
||
| 672 | protected function setPattern(){ |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param bool $test |
||
| 689 | * @param int $maskPattern |
||
| 690 | * |
||
| 691 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 692 | */ |
||
| 693 | protected function getMatrix(bool $test, int $maskPattern){ |
||
| 704 | |||
| 705 | } |
||
| 706 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: