| Conditions | 18 |
| Paths | 38 |
| Total Lines | 88 |
| Code Lines | 52 |
| 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 |
||
| 171 | public function save($relations = false) |
||
| 172 | { |
||
| 173 | if ($relations === true) { |
||
| 174 | $relations = array_keys($this->relations); |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($relations) { |
||
| 178 | $scheme = $this->getTable()->getScheme()['relations']; |
||
| 179 | |||
| 180 | foreach ($relations as $name) { |
||
|
|
|||
| 181 | if (!array_key_exists($name, $this->relations)) { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | if (!isset($scheme[$name])) { |
||
| 186 | throw new SimpleCrudException(sprintf('Invalid relation: %s', $name)); |
||
| 187 | } |
||
| 188 | |||
| 189 | $relation = $scheme[$name]; |
||
| 190 | |||
| 191 | if ($relation[0] === Scheme::HAS_ONE) { |
||
| 192 | $this->{$relation[1]} = ($this->relations[$name] === null) ? null : $this->relations[$name]->save()->id; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($this->changed) { |
||
| 198 | if (empty($this->id)) { |
||
| 199 | $this->id = $this->table->insert() |
||
| 200 | ->data($this->values) |
||
| 201 | ->run(); |
||
| 202 | } else { |
||
| 203 | $this->table->update() |
||
| 204 | ->data($this->values) |
||
| 205 | ->byId($this->id) |
||
| 206 | ->limit(1) |
||
| 207 | ->run(); |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->table->cache($this); |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($relations) { |
||
| 214 | $scheme = $this->getTable()->getScheme()['relations']; |
||
| 215 | |||
| 216 | foreach ($relations as $name) { |
||
| 217 | if (!array_key_exists($name, $this->relations)) { |
||
| 218 | continue; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (!isset($scheme[$name])) { |
||
| 222 | throw new SimpleCrudException(sprintf('Invalid relation: %s', $name)); |
||
| 223 | } |
||
| 224 | |||
| 225 | $relation = $scheme[$name]; |
||
| 226 | |||
| 227 | if ($relation[0] === Scheme::HAS_MANY) { |
||
| 228 | foreach ($this->relations[$name] as $row) { |
||
| 229 | $row->{$relation[1]} = $this->id; |
||
| 230 | $row->save(); |
||
| 231 | } |
||
| 232 | |||
| 233 | continue; |
||
| 234 | } |
||
| 235 | |||
| 236 | if ($relation[0] === Scheme::HAS_MANY_TO_MANY) { |
||
| 237 | $bridge = $this->getDatabase()->{$relation[1]}; |
||
| 238 | |||
| 239 | $bridge |
||
| 240 | ->delete() |
||
| 241 | ->by($relation[2], $this->id) |
||
| 242 | ->run(); |
||
| 243 | |||
| 244 | foreach ($this->relations[$name] as $row) { |
||
| 245 | $bridge |
||
| 246 | ->insert() |
||
| 247 | ->data([ |
||
| 248 | $relation[2] => $this->id, |
||
| 249 | $relation[3] => $row->id, |
||
| 250 | ]) |
||
| 251 | ->run(); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 308 |
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.