| Conditions | 18 |
| Paths | 30 |
| Total Lines | 90 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 24 | ||
| Bugs | 1 | Features | 2 |
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 |
||
| 161 | public function save($relations = false) |
||
| 162 | { |
||
| 163 | if ($relations === true) { |
||
| 164 | $relations = array_keys($this->relations); |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($relations) { |
||
| 168 | $scheme = $this->getTable()->getScheme()['relations']; |
||
| 169 | |||
| 170 | foreach ($relations as $name) { |
||
|
|
|||
| 171 | if (!array_key_exists($name, $this->relations)) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!isset($scheme[$name])) { |
||
| 176 | throw new SimpleCrudException(sprintf('Invalid relation: %s', $name)); |
||
| 177 | } |
||
| 178 | |||
| 179 | $relation = $scheme[$name]; |
||
| 180 | |||
| 181 | if ($relation[0] === Scheme::HAS_ONE) { |
||
| 182 | $this->{$relation[1]} = ($this->relations[$name] === null) ? null : $this->relations[$name]->save()->id; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | if (!$this->changed) { |
||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (empty($this->id)) { |
||
| 192 | $this->id = $this->table->insert() |
||
| 193 | ->data($this->values) |
||
| 194 | ->run(); |
||
| 195 | } else { |
||
| 196 | $this->table->update() |
||
| 197 | ->data($this->values) |
||
| 198 | ->byId($this->id) |
||
| 199 | ->limit(1) |
||
| 200 | ->run(); |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($relations) { |
||
| 204 | $scheme = $this->getTable()->getScheme()['relations']; |
||
| 205 | |||
| 206 | foreach ($relations as $name) { |
||
| 207 | if (!array_key_exists($name, $this->relations)) { |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | if (!isset($scheme[$name])) { |
||
| 212 | throw new SimpleCrudException(sprintf('Invalid relation: %s', $name)); |
||
| 213 | } |
||
| 214 | |||
| 215 | $relation = $scheme[$name]; |
||
| 216 | |||
| 217 | if ($relation[0] === Scheme::HAS_MANY) { |
||
| 218 | foreach ($this->relations[$name] as $row) { |
||
| 219 | $row->{$relation[1]} = $this->id; |
||
| 220 | $row->save(); |
||
| 221 | } |
||
| 222 | |||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($relation[0] === Scheme::HAS_MANY_TO_MANY) { |
||
| 227 | $bridge = $this->getDatabase()->{$relation[1]}; |
||
| 228 | |||
| 229 | $bridge |
||
| 230 | ->delete() |
||
| 231 | ->by($relation[2], $this->id) |
||
| 232 | ->run(); |
||
| 233 | |||
| 234 | foreach ($this->relations[$name] as $row) { |
||
| 235 | $bridge |
||
| 236 | ->insert() |
||
| 237 | ->data([ |
||
| 238 | $relation[2] => $this->id, |
||
| 239 | $relation[3] => $row->id, |
||
| 240 | ]) |
||
| 241 | ->run(); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | $this->table->cache($this); |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | } |
||
| 252 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.