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:
| 1 | <?php |
||
| 30 | class QRCode{ |
||
| 31 | use ClassLoader; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * API constants |
||
| 35 | */ |
||
| 36 | const OUTPUT_MARKUP_HTML = 'html'; |
||
| 37 | const OUTPUT_MARKUP_SVG = 'svg'; |
||
| 38 | # const OUTPUT_MARKUP_XML = 'xml'; // anyone? |
||
| 39 | |||
| 40 | const OUTPUT_IMAGE_PNG = 'png'; |
||
| 41 | const OUTPUT_IMAGE_JPG = 'jpg'; |
||
| 42 | const OUTPUT_IMAGE_GIF = 'gif'; |
||
| 43 | |||
| 44 | const OUTPUT_STRING_JSON = 'json'; |
||
| 45 | const OUTPUT_STRING_TEXT = 'text'; |
||
| 46 | |||
| 47 | const VERSION_AUTO = -1; |
||
| 48 | const MASK_PATTERN_AUTO = -1; |
||
| 49 | |||
| 50 | const ECC_L = 0b01; // 7%. |
||
| 51 | const ECC_M = 0b00; // 15%. |
||
| 52 | const ECC_Q = 0b11; // 25%. |
||
| 53 | const ECC_H = 0b10; // 30%. |
||
| 54 | |||
| 55 | const DATA_NUMBER = 0b0001; |
||
| 56 | const DATA_ALPHANUM = 0b0010; |
||
| 57 | const DATA_BYTE = 0b0100; |
||
| 58 | const DATA_KANJI = 0b1000; |
||
| 59 | |||
| 60 | const ECC_MODES = [ |
||
| 61 | QRCode::ECC_L => 0, |
||
| 62 | QRCode::ECC_M => 1, |
||
| 63 | QRCode::ECC_Q => 2, |
||
| 64 | QRCode::ECC_H => 3, |
||
| 65 | ]; |
||
| 66 | |||
| 67 | const DATA_MODES = [ |
||
| 68 | self::DATA_NUMBER => 0, |
||
| 69 | self::DATA_ALPHANUM => 1, |
||
| 70 | self::DATA_BYTE => 2, |
||
| 71 | self::DATA_KANJI => 3, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | const OUTPUT_MODES = [ |
||
| 75 | QRMarkup::class => [ |
||
| 76 | self::OUTPUT_MARKUP_SVG, |
||
| 77 | self::OUTPUT_MARKUP_HTML, |
||
| 78 | ], |
||
| 79 | QRImage::class => [ |
||
| 80 | self::OUTPUT_IMAGE_PNG, |
||
| 81 | self::OUTPUT_IMAGE_GIF, |
||
| 82 | self::OUTPUT_IMAGE_JPG, |
||
| 83 | ], |
||
| 84 | QRString::class => [ |
||
| 85 | self::OUTPUT_STRING_JSON, |
||
| 86 | self::OUTPUT_STRING_TEXT, |
||
| 87 | ] |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var \chillerlan\QRCode\QROptions |
||
| 92 | */ |
||
| 93 | protected $options; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var \chillerlan\QRCode\Data\QRDataInterface |
||
| 97 | */ |
||
| 98 | protected $dataInterface; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * QRCode constructor. |
||
| 102 | * |
||
| 103 | * @param \chillerlan\QRCode\QROptions|null $options |
||
| 104 | */ |
||
| 105 | public function __construct(QROptions $options = null){ |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Sets the options, called internally by the constructor |
||
| 113 | * |
||
| 114 | * @param \chillerlan\QRCode\QROptions $options |
||
| 115 | * |
||
| 116 | * @return \chillerlan\QRCode\QRCode |
||
| 117 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 118 | */ |
||
| 119 | public function setOptions(QROptions $options):QRCode{ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Renders a QR Code for the given $data and QROptions |
||
| 142 | * |
||
| 143 | * @param string $data |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function render(string $data){ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns a QRMatrix object for the given $data and current QROptions |
||
| 153 | * |
||
| 154 | * @param string $data |
||
| 155 | * |
||
| 156 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 157 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
| 158 | */ |
||
| 159 | public function getMatrix(string $data):QRMatrix { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * shoves a QRMatrix through the MaskPatternTester to find the lowest penalty mask pattern |
||
| 186 | * |
||
| 187 | * @see \chillerlan\QRCode\Data\MaskPatternTester |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | protected function getBestMaskPattern():int{ |
||
| 208 | |||
| 209 | /** |
||
| 210 | * returns a fresh QRDataInterface for the given $data |
||
| 211 | * |
||
| 212 | * @param string $data |
||
| 213 | * |
||
| 214 | * @return \chillerlan\QRCode\Data\QRDataInterface |
||
| 215 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
| 216 | */ |
||
| 217 | public function initDataInterface(string $data):QRDataInterface{ |
||
| 236 | |||
| 237 | /** |
||
| 238 | * returns a fresh (built-in) QROutputInterface |
||
| 239 | * |
||
| 240 | * @param string $data |
||
| 241 | * |
||
| 242 | * @return \chillerlan\QRCode\Output\QROutputInterface |
||
| 243 | * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
||
| 244 | */ |
||
| 245 | protected function initOutputInterface(string $data):QROutputInterface{ |
||
| 257 | |||
| 258 | /** |
||
| 259 | * checks of a string qualifies as numeric |
||
| 260 | * |
||
| 261 | * @param string $string |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | View Code Duplication | public function isNumber(string $string):bool { |
|
| 277 | |||
| 278 | /** |
||
| 279 | * checks of a string qualifies as alphanumeric |
||
| 280 | * |
||
| 281 | * @param string $string |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function isAlphaNum(string $string):bool { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * checks of a string qualifies as Kanji |
||
| 299 | * |
||
| 300 | * @param string $string |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function isKanji(string $string):bool { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * a dummy |
||
| 323 | * |
||
| 324 | * @param $data |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | protected function isByte(string $data):bool{ |
||
| 331 | |||
| 332 | } |
||
| 333 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.