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 OUTPUT_CUSTOM = 'custom'; |
||
48 | |||
49 | const VERSION_AUTO = -1; |
||
50 | const MASK_PATTERN_AUTO = -1; |
||
51 | |||
52 | const ECC_L = 0b01; // 7%. |
||
53 | const ECC_M = 0b00; // 15%. |
||
54 | const ECC_Q = 0b11; // 25%. |
||
55 | const ECC_H = 0b10; // 30%. |
||
56 | |||
57 | const DATA_NUMBER = 0b0001; |
||
58 | const DATA_ALPHANUM = 0b0010; |
||
59 | const DATA_BYTE = 0b0100; |
||
60 | const DATA_KANJI = 0b1000; |
||
61 | |||
62 | const ECC_MODES = [ |
||
63 | self::ECC_L => 0, |
||
64 | self::ECC_M => 1, |
||
65 | self::ECC_Q => 2, |
||
66 | self::ECC_H => 3, |
||
67 | ]; |
||
68 | |||
69 | const DATA_MODES = [ |
||
70 | self::DATA_NUMBER => 0, |
||
71 | self::DATA_ALPHANUM => 1, |
||
72 | self::DATA_BYTE => 2, |
||
73 | self::DATA_KANJI => 3, |
||
74 | ]; |
||
75 | |||
76 | const OUTPUT_MODES = [ |
||
77 | QRMarkup::class => [ |
||
78 | self::OUTPUT_MARKUP_SVG, |
||
79 | self::OUTPUT_MARKUP_HTML, |
||
80 | ], |
||
81 | QRImage::class => [ |
||
82 | self::OUTPUT_IMAGE_PNG, |
||
83 | self::OUTPUT_IMAGE_GIF, |
||
84 | self::OUTPUT_IMAGE_JPG, |
||
85 | ], |
||
86 | QRString::class => [ |
||
87 | self::OUTPUT_STRING_JSON, |
||
88 | self::OUTPUT_STRING_TEXT, |
||
89 | ] |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * @var \chillerlan\QRCode\QROptions |
||
94 | */ |
||
95 | protected $options; |
||
96 | |||
97 | /** |
||
98 | * @var \chillerlan\QRCode\Data\QRDataInterface |
||
99 | */ |
||
100 | protected $dataInterface; |
||
101 | |||
102 | /** |
||
103 | * QRCode constructor. |
||
104 | * |
||
105 | * @param \chillerlan\QRCode\QROptions|null $options |
||
106 | */ |
||
107 | public function __construct(QROptions $options = null){ |
||
112 | |||
113 | /** |
||
114 | * Sets the options, called internally by the constructor |
||
115 | * |
||
116 | * @param \chillerlan\QRCode\QROptions $options |
||
117 | * |
||
118 | * @return \chillerlan\QRCode\QRCode |
||
119 | * @throws \chillerlan\QRCode\QRCodeException |
||
120 | */ |
||
121 | public function setOptions(QROptions $options):QRCode{ |
||
141 | |||
142 | /** |
||
143 | * Renders a QR Code for the given $data and QROptions |
||
144 | * |
||
145 | * @param string $data |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public function render(string $data){ |
||
152 | |||
153 | /** |
||
154 | * Returns a QRMatrix object for the given $data and current QROptions |
||
155 | * |
||
156 | * @param string $data |
||
157 | * |
||
158 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
159 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
160 | */ |
||
161 | public function getMatrix(string $data):QRMatrix { |
||
185 | |||
186 | /** |
||
187 | * shoves a QRMatrix through the MaskPatternTester to find the lowest penalty mask pattern |
||
188 | * |
||
189 | * @see \chillerlan\QRCode\Data\MaskPatternTester |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | protected function getBestMaskPattern():int{ |
||
210 | |||
211 | /** |
||
212 | * returns a fresh QRDataInterface for the given $data |
||
213 | * |
||
214 | * @param string $data |
||
215 | * |
||
216 | * @return \chillerlan\QRCode\Data\QRDataInterface |
||
217 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
218 | */ |
||
219 | public function initDataInterface(string $data):QRDataInterface{ |
||
238 | |||
239 | /** |
||
240 | * returns a fresh (built-in) QROutputInterface |
||
241 | * |
||
242 | * @param string $data |
||
243 | * |
||
244 | * @return \chillerlan\QRCode\Output\QROutputInterface |
||
245 | * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
||
246 | */ |
||
247 | protected function initOutputInterface(string $data):QROutputInterface{ |
||
263 | |||
264 | /** |
||
265 | * checks if a string qualifies as numeric |
||
266 | * |
||
267 | * @param string $string |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | View Code Duplication | public function isNumber(string $string):bool { |
|
283 | |||
284 | /** |
||
285 | * checks if a string qualifies as alphanumeric |
||
286 | * |
||
287 | * @param string $string |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | View Code Duplication | public function isAlphaNum(string $string):bool { |
|
302 | |||
303 | /** |
||
304 | * checks if a string qualifies as Kanji |
||
305 | * |
||
306 | * @param string $string |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function isKanji(string $string):bool { |
||
326 | |||
327 | /** |
||
328 | * a dummy |
||
329 | * |
||
330 | * @param $data |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | protected function isByte(string $data):bool{ |
||
337 | |||
338 | } |
||
339 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.