Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Row often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Row, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Row extends AbstractRow |
||
| 11 | { |
||
| 12 | private $values = []; |
||
| 13 | private $relations = []; |
||
| 14 | private $changed = false; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | */ |
||
| 19 | public function __construct(Table $table) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Debug info. |
||
| 30 | * |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | public function __debugInfo() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Clear the current cache. |
||
| 43 | */ |
||
| 44 | public function clearCache() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Magic method to return properties or load them automatically. |
||
| 51 | * |
||
| 52 | * @param string $name |
||
| 53 | */ |
||
| 54 | public function __get($name) |
||
| 55 | { |
||
| 56 | //It's a field |
||
| 57 | if (array_key_exists($name, $this->values)) { |
||
| 58 | return $this->values[$name]; |
||
| 59 | } |
||
| 60 | |||
| 61 | //It's a relation |
||
| 62 | if (array_key_exists($name, $this->relations)) { |
||
| 63 | return $this->relations[$name] ?: new NullValue(); |
||
| 64 | } |
||
| 65 | |||
| 66 | //Load the relation |
||
| 67 | $scheme = $this->getTable()->getScheme(); |
||
| 68 | |||
| 69 | if (isset($scheme['relations'][$name])) { |
||
| 70 | return ($this->relations[$name] = call_user_func([$this, $name])->run()) ?: new NullValue(); |
||
| 71 | } |
||
| 72 | |||
| 73 | //Exists as a function |
||
| 74 | if (method_exists($this, $name)) { |
||
| 75 | return $this->$name(); |
||
| 76 | } |
||
| 77 | |||
| 78 | throw new SimpleCrudException(sprintf('Undefined property "%s"', $name)); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Magic method to store properties. |
||
| 83 | * |
||
| 84 | * @param string $name |
||
| 85 | * @param mixed $value |
||
| 86 | */ |
||
| 87 | public function __set($name, $value) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Magic method to check if a property is defined or not. |
||
| 122 | * |
||
| 123 | * @param string $name Property name |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function __isset($name) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function toArray(array $bannedEntities = []) |
||
| 154 | |||
| 155 | public function edit(array $values) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Saves this row in the database. |
||
| 166 | * |
||
| 167 | * @param bool|array $relations Set true to save the relations with other entities |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function save($relations = false) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Join this row with other row |
||
| 262 | * |
||
| 263 | * @param Row $row |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function join(Row $row) |
||
| 307 | } |
||
| 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.