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