| Conditions | 4 |
| Paths | 3 |
| Total Lines | 19 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | public function s_adicionar_variacao($variacoes, $produto_id, $usuario_id) { |
||
| 6 | if (empty($variacoes) || empty($produto_id)) { |
||
| 7 | return false; |
||
| 8 | } |
||
| 9 | |||
| 10 | foreach ($variacoes as $i => $variacao) { |
||
| 11 | $this->Variacao->create(); |
||
| 12 | |||
| 13 | $dados['produto_id'] = $produto_id; |
||
|
|
|||
| 14 | $dados['usuario_id'] = $usuario_id; |
||
| 15 | $dados['nome_variacao'] = $variacao['variacao']; |
||
| 16 | $dados['estoque'] = $variacao['estoque']; |
||
| 17 | $dados['ativo'] = 1; |
||
| 18 | |||
| 19 | $this->Variacao->save($dados); |
||
| 20 | } |
||
| 21 | |||
| 22 | return true; |
||
| 23 | } |
||
| 24 | |||
| 40 | } |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.