Conditions | 2 |
Paths | 2 |
Total Lines | 76 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
84 | public function pegar_venda_como_html($dadosVenda, $produtosVenda) |
||
85 | { |
||
86 | $html = ''; |
||
87 | $html .= '<html>'; |
||
88 | $html .= '<head>'; |
||
89 | $html .= ' <title></title>'; |
||
90 | $html .= '</head>'; |
||
91 | $html .= '<body>'; |
||
92 | $html .= ''; |
||
93 | $html .= ' <table style="background-color: #cacaca;" width="100%" valign="center" align="center">'; |
||
94 | $html .= ' <tr align="center">'; |
||
95 | $html .= ' <td>'; |
||
96 | $html .= ' <h2>Orçamento Pedido #' . $dadosVenda[0]['Venda']['id'] . '</h2>'; |
||
97 | $html .= ' </td>'; |
||
98 | $html .= ' </tr> '; |
||
99 | $html .= ' </table>'; |
||
100 | $html .= ' <br>'; |
||
101 | $html .= ' <table width="100%" valign="center" align="center">'; |
||
102 | $html .= ' <tr style="background-color: #ccc;">'; |
||
103 | $html .= ' <td>Total: </td>'; |
||
104 | $html .= ' <td>R$ ' . number_format($dadosVenda[0]['Venda']['valor'], 2, ',', '.') . '</td>'; |
||
105 | $html .= ' </tr>'; |
||
106 | $html .= ' <tr style="background-color: #ccc;">'; |
||
107 | $html .= ' <td>Valor a Pagar: </td>'; |
||
108 | $html .= ' <td>R$ ' . number_format($dadosVenda[0]['Venda']['valor'], 2, ',', '.') . '</td>'; |
||
109 | $html .= ' </tr>'; |
||
110 | $html .= ' </table>'; |
||
111 | $html .= ' <br>'; |
||
112 | $html .= ' <table width="100%" valign="center" align="center">'; |
||
113 | $html .= ' <tr style="background-color: #cacaca;" align="center">'; |
||
114 | $html .= ' <td>'; |
||
115 | $html .= ' <h2>Produtos</h2>'; |
||
116 | $html .= ' </td>'; |
||
117 | $html .= ' </tr> '; |
||
118 | $html .= ' </table>'; |
||
119 | $html .= ' <br>'; |
||
120 | $html .= ' <table width="100%" valign="center" align="center">'; |
||
121 | $html .= ' <tr>'; |
||
122 | $html .= ' <td>'; |
||
123 | $html .= ' <table width="100%" valign="center" align="center">'; |
||
124 | $html .= ' <tr style="background-color: #cacaca;">'; |
||
125 | $html .= ' <td>Nome Produto</td>'; |
||
126 | $html .= ' <td>Quantidade</td>'; |
||
127 | $html .= ' <td>Valor</td>'; |
||
128 | $html .= ' </tr>'; |
||
129 | $html .= ''; |
||
130 | $desconto = 0; |
||
131 | foreach ($produtosVenda as $i => $produto) { |
||
132 | $produtoInfos = $this->getInfoProdutos($produto['VendaItensProduto']['produto_id']); |
||
133 | |||
134 | $total = $produtoInfos['Produto']['preco'] * $produto['VendaItensProduto']['quantidade_produto']; |
||
135 | $total = number_format($total, 2, ',', '.'); |
||
136 | |||
137 | $html .= ' <tr>'; |
||
138 | $html .= ' <td>' . $produtoInfos['Produto']['nome'] . '</td>'; |
||
139 | $html .= ' <td>' . $produto['VendaItensProduto']['quantidade_produto'] . '</td>'; |
||
140 | $html .= ' <td>R$ ' . $total . '</td>'; |
||
141 | $html .= ' </tr>'; |
||
142 | } |
||
143 | $html .= ' </table>'; |
||
144 | $html .= ' </td>'; |
||
145 | $html .= ' </tr>'; |
||
146 | $html .= ' </table>'; |
||
147 | $html .= ' <br>'; |
||
148 | /* $html .= ' <table width="100%" valign="center" align="center">'; |
||
149 | $html .= ' <tr style="background-color: #ccc;">'; |
||
150 | $html .= ' <td>Total: </td>'; |
||
151 | $html .= ' <td>R$ ' . number_format($desconto, 2, ',', '.') . '</td>'; |
||
152 | $html .= ' </tr>'; |
||
153 | $html .= ' </table>'; */ |
||
154 | $html .= ''; |
||
155 | $html .= '</body>'; |
||
156 | $html .= '</html>'; |
||
157 | |||
158 | return $html; |
||
159 | } |
||
160 | |||
175 | } |
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.