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 |
||
| 22 | class QRString extends QROutputAbstract{ |
||
| 23 | |||
| 24 | protected string $defaultMode = QRCode::OUTPUT_STRING_TEXT; |
||
|
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * @inheritDoc |
||
| 28 | */ |
||
| 29 | protected function setModuleValues():void{ |
||
| 30 | |||
| 31 | foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){ |
||
| 32 | $v = $this->options->moduleValues[$M_TYPE] ?? null; |
||
| 33 | |||
| 34 | if(!is_string($v)){ |
||
| 35 | $this->moduleValues[$M_TYPE] = $defaultValue |
||
| 36 | ? $this->options->textDark |
||
| 37 | : $this->options->textLight; |
||
| 38 | } |
||
| 39 | else{ |
||
| 40 | $this->moduleValues[$M_TYPE] = $v; |
||
| 41 | } |
||
| 42 | |||
| 43 | } |
||
| 44 | |||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | protected function text():string{ |
||
| 51 | $str = []; |
||
| 52 | |||
| 53 | foreach($this->matrix->matrix() as $row){ |
||
| 54 | $r = []; |
||
| 55 | |||
| 56 | foreach($row as $M_TYPE){ |
||
| 57 | $r[] = $this->moduleValues[$M_TYPE]; |
||
| 58 | } |
||
| 59 | |||
| 60 | $str[] = implode('', $r); |
||
| 61 | } |
||
| 62 | |||
| 63 | return implode($this->options->eol, $str); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * |
||
| 68 | */ |
||
| 69 | protected function json():string{ |
||
| 70 | return json_encode($this->matrix->matrix()); |
||
| 71 | } |
||
| 72 | |||
| 73 | } |
||
| 74 |