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 |
||
| 25 | class QRCode{ |
||
| 26 | |||
| 27 | /** |
||
| 28 | * API constants |
||
| 29 | */ |
||
| 30 | const OUTPUT_STRING_TEXT = 0; |
||
| 31 | const OUTPUT_STRING_JSON = 1; |
||
| 32 | const OUTPUT_STRING_HTML = 2; |
||
| 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 \chillerlan\QRCode\BitBuffer |
||
| 77 | */ |
||
| 78 | protected $bitBuffer; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \chillerlan\QRCode\Data\QRDataInterface |
||
| 82 | */ |
||
| 83 | protected $qrDataInterface; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \chillerlan\QRCode\Output\QROutputInterface |
||
| 87 | */ |
||
| 88 | protected $qrOutputInterface; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * QRCode constructor. |
||
| 92 | * |
||
| 93 | * @param string $data |
||
| 94 | * @param \chillerlan\QRCode\Output\QROutputInterface $output |
||
| 95 | * @param \chillerlan\QRCode\QROptions|null $options |
||
| 96 | */ |
||
| 97 | public function __construct($data, QROutputInterface $output, QROptions $options = null){ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $data |
||
| 105 | * @param \chillerlan\QRCode\QROptions|null $options |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 109 | */ |
||
| 110 | public function setData($data, QROptions $options = null){ |
||
| 111 | $data = trim($data); |
||
| 112 | |||
| 113 | if(empty($data)){ |
||
| 114 | throw new QRCodeException('No data given.'); |
||
| 115 | } |
||
| 116 | |||
| 117 | if(!$options instanceof QROptions){ |
||
| 118 | $options = new QROptions; |
||
| 119 | } |
||
| 120 | |||
| 121 | if(!in_array($options->errorCorrectLevel, QRConst::RSBLOCK, true)){ |
||
| 122 | throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel); |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->errorCorrectLevel = $options->errorCorrectLevel; |
||
| 126 | |||
| 127 | switch(true){ |
||
| 128 | case Util::isAlphaNum($data): |
||
| 129 | $mode = Util::isNumber($data) ? QRConst::MODE_NUMBER : QRConst::MODE_ALPHANUM; |
||
| 130 | break; |
||
| 131 | case Util::isKanji($data): |
||
| 132 | $mode = QRConst::MODE_KANJI; |
||
| 133 | break; |
||
| 134 | default: |
||
| 135 | $mode = QRConst::MODE_BYTE; |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | |||
| 139 | $qrDataInterface = [ |
||
| 140 | QRConst::MODE_ALPHANUM => AlphaNum::class, |
||
| 141 | QRConst::MODE_BYTE => Byte::class, |
||
| 142 | QRConst::MODE_KANJI => Kanji::class, |
||
| 143 | QRConst::MODE_NUMBER => Number::class, |
||
| 144 | ][$mode]; |
||
| 145 | |||
| 146 | $this->qrDataInterface = new $qrDataInterface($data); |
||
| 147 | $this->typeNumber = intval($options->typeNumber); |
||
| 148 | |||
| 149 | if($this->typeNumber < 1 || $this->typeNumber > 10){ |
||
| 150 | $this->typeNumber = $this->getTypeNumber($mode); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $mode |
||
| 158 | * |
||
| 159 | * @return int |
||
| 160 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 161 | */ |
||
| 162 | protected function getTypeNumber($mode){ |
||
| 163 | $length = $this->qrDataInterface->dataLength; |
||
|
|
|||
| 164 | |||
| 165 | if($this->qrDataInterface->mode === QRConst::MODE_KANJI){ |
||
| 166 | $length = floor($this->qrDataInterface->dataLength / 2); |
||
| 167 | } |
||
| 168 | |||
| 169 | foreach(range(1, 10) as $type){ |
||
| 170 | if($length <= Util::getMaxLength($type, $mode, $this->errorCorrectLevel)){ |
||
| 171 | return $type; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | throw new QRCodeException('Unable to determine type number.'); // @codeCoverageIgnore |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | public function output(){ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getRawData(){ |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param bool $test |
||
| 308 | */ |
||
| 309 | protected function setTypeNumber($test){ |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param bool $test |
||
| 323 | * @param int $pattern |
||
| 324 | */ |
||
| 325 | protected function setTypeInfo($test, $pattern){ |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 353 | */ |
||
| 354 | protected function createData(){ |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array |
||
| 402 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 403 | */ |
||
| 404 | protected function createBytes(){ |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param int $pattern |
||
| 477 | * |
||
| 478 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 479 | */ |
||
| 480 | protected function mapData($pattern){ |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 547 | */ |
||
| 548 | protected function setPattern(){ |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param bool $test |
||
| 609 | * @param int $maskPattern |
||
| 610 | * |
||
| 611 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 612 | */ |
||
| 613 | protected function getMatrix($test, $maskPattern){ |
||
| 624 | |||
| 625 | } |
||
| 626 |
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: