Conditions | 8 |
Paths | 30 |
Total Lines | 62 |
Code Lines | 37 |
Lines | 16 |
Ratio | 25.81 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
46 | public function barcodeQRCode($data = '', $level = 'M', $modelo = 2, $wmod = 4) |
||
47 | { |
||
48 | //essa matriz especifica o numero máximo de caracteres alfanumericos que o |
||
49 | //modelo de QRCode suporta dependendo no nivel de correção. |
||
50 | //Cada matriz representa um nivel de correção e cada uma das 40 posições nessas |
||
51 | //matrizes indicam o numero do modelo do QRCode e o numero máximo de caracteres |
||
52 | //alfamunéricos suportados |
||
53 | //Quanto maior o nivel de correção menor é a quantidade de caracteres suportada |
||
54 | $aModels[0]=[25,47,77,114,154,195,224,279,335,395,468,535,619,667,758,854,938,1046,1153,1249,1352,1460,1588,1704,1853,1990,2132,2223,2369,2520,2677,2840,3009,3183,3351,3537,3729,3927,4087,4296]; |
||
|
|||
55 | $aModels[1]=[20,38,61,90,122,154,178,221,262,311,366,419,483,528,600,656,734,816,909,970,1035,1134,1248,1326,1451,1542,1637,1732,1839,1994,2113,2238,2369,2506,2632,2780,2894,3054,3220,3391]; |
||
56 | $aModels[2]=[16,29,47,67,87,108,125,157,189,221,259,296,352,376,426,470,531,574,644,702,742,823,890,963,1041,1094,1172,1263,1322,1429,1499,1618,1700,1787,1867,1966,2071,2181,2298,2420]; |
||
57 | $aModels[3]=[10,20,35,50,64,84,93,122,143,174,200,227,259,283,321,365,408,452,493,557,587,640,672,744,779,864,910,958,1016,1080,1150,1226,1307,1394,1431,1530,1591,1658,1774,1852]; |
||
58 | //n1 Error correction level (data restoration) |
||
59 | View Code Duplication | switch ($level) { |
|
60 | case 'L': |
||
61 | $n1 = 0; |
||
62 | break; |
||
63 | case "M": |
||
64 | $n1 = 1; |
||
65 | break; |
||
66 | case "Q": |
||
67 | $n1 = 2; |
||
68 | break; |
||
69 | case "H": |
||
70 | $n1 = 3; |
||
71 | break; |
||
72 | default: |
||
73 | $n1 = 0; |
||
74 | } |
||
75 | //n2 Module/cell size in pixels MSB 1 ≤ module size ≤ 127 LSB 0 QR or 1 MicroQR |
||
76 | $n2 = $wmod << 2; |
||
77 | //comprimento da mensagem |
||
78 | $length = strlen($data); |
||
79 | //seleciona matriz de modelos aplicavel pelo nivel de correção |
||
80 | $am = $aModels[$n1]; |
||
81 | $i = 0; |
||
82 | $flag = false; |
||
83 | foreach($am as $size) { |
||
84 | //verifica se o tamanho maximo é maior ou igual ao comprimento da mensagem |
||
85 | if ($size >= $length) { |
||
86 | $flag = true; |
||
87 | break; |
||
88 | } |
||
89 | $i++; |
||
90 | } |
||
91 | if (! $flag) { |
||
92 | throw new InvalidArgumentException('O numero de caracteres da mensagem é maior que a capacidade do QRCode'); |
||
93 | } |
||
94 | //n3 Version QRCode |
||
95 | //depende do comprimento dos dados e do nivel de correção |
||
96 | $n3 = ($i + 1); |
||
97 | //n4 Encoding modes |
||
98 | //0 – Numeric only Max. 7,089 characters |
||
99 | //1 – Alphanumeric Max. 4,296 characters |
||
100 | //2 – Binary (8 bits) Max. 2,953 bytes |
||
101 | //3 – Kanji, full-width Kana Max. 1,817 characters |
||
102 | $n4 = 1;//sempre será 1 apenas caracteres alfanumericos nesse caso |
||
103 | //n5 e n6 Indicate the number of bytes that will be coded, where total = n5 + n6 x 256, and total must be less than 7089. |
||
104 | $n6 = intval($length / 256); |
||
105 | $n5 = ($length % 256); |
||
106 | $this->buffer->write(self::GS."kQ" . chr($n1) . chr($n2) . chr($n3) . chr($n4) . chr($n5) . chr($n6) . $data); |
||
107 | } |
||
108 | } |
||
109 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.