Conditions | 9 |
Paths | 56 |
Total Lines | 58 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 2 | 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 |
||
64 | public function saveLob(Builder $query, $sql, array $values, array $binaries) |
||
65 | { |
||
66 | $parameter = 0; |
||
67 | $lob = []; |
||
68 | $id = 0; |
||
69 | |||
70 | // begin transaction |
||
71 | $pdo = $query->getConnection()->getPdo(); |
||
72 | $inTransaction = $pdo->inTransaction(); |
||
73 | if (! $inTransaction) { |
||
74 | $pdo->beginTransaction(); |
||
75 | } |
||
76 | |||
77 | $this->prepareStatement($query, $sql); |
||
78 | foreach ($values as $value) { |
||
79 | $this->bindValue($parameter, $value); |
||
80 | $parameter++; |
||
81 | } |
||
82 | |||
83 | $binariesCount = count($binaries); |
||
84 | for ($i = 0; $i < $binariesCount; $i++) { |
||
85 | // bind blob descriptor |
||
86 | $this->statement->bindParam($parameter, $lob[$i], PDO::PARAM_LOB); |
||
87 | $parameter++; |
||
88 | } |
||
89 | |||
90 | // bind output param for the returning clause |
||
91 | $this->statement->bindParam($parameter, $id, PDO::PARAM_INT); |
||
92 | |||
93 | // execute statement |
||
94 | if (! $this->statement->execute()) { |
||
95 | $pdo->rollBack(); |
||
96 | |||
97 | return false; |
||
98 | } |
||
99 | |||
100 | for ($i = 0; $i < $binariesCount; $i++) { |
||
101 | // Discard the existing LOB contents |
||
102 | if (! $lob[$i]->truncate()) { |
||
103 | $pdo->rollBack(); |
||
104 | |||
105 | return false; |
||
106 | } |
||
107 | // save blob content |
||
108 | if (! $lob[$i]->save($binaries[$i])) { |
||
109 | $pdo->rollBack(); |
||
110 | |||
111 | return false; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if (! $inTransaction) { |
||
116 | // commit statements |
||
117 | $pdo->commit(); |
||
118 | } |
||
119 | |||
120 | return (int) $id; |
||
121 | } |
||
122 | |||
148 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: