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:
| 1 | <?php |
||
| 13 | class QueryImpl implements Query { |
||
| 14 | /** |
||
| 15 | * @var Graph |
||
| 16 | */ |
||
| 17 | protected $graph; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array[] |
||
| 21 | */ |
||
| 22 | protected $steps; |
||
| 23 | |||
| 24 | 56 | public function __construct(Graph $graph) { |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritdocs |
||
| 32 | */ |
||
| 33 | 36 | public function predicate_factory() { |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @inheritdocs |
||
| 39 | */ |
||
| 40 | 29 | public function expand(\Closure $expander) { |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @inheritdocs |
||
| 49 | */ |
||
| 50 | 55 | public function extract(\Closure $extractor) { |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @inheritdocs |
||
| 59 | */ |
||
| 60 | 49 | public function filter(Predicate $predicate) { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdocs |
||
| 69 | */ |
||
| 70 | 55 | public function run($result) { |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return Iterator<[Node,mixed]> |
||
| 96 | */ |
||
| 97 | 55 | protected function switch_run_command(\Iterator $nodes, $step) { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return Iterator<[Node,mixed]> |
||
| 115 | */ |
||
| 116 | 29 | protected function run_expand(\Iterator $nodes, \Closure $clsr) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return Iterator<[Node,mixed]> |
||
| 129 | */ |
||
| 130 | 55 | protected function run_extract(\Iterator $nodes, \Closure $clsr) { |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return Iterator<[Node,mixed]> |
||
| 144 | */ |
||
| 145 | 49 | protected function run_filter(\Iterator $nodes, Predicate $predicate) { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @return Iterator<[Node,mixed]> |
||
| 159 | */ |
||
| 160 | 55 | protected function add_result(\Iterator $nodes, &$result) { |
|
| 167 | |||
| 168 | // Convenience Functions |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritdocs |
||
| 172 | */ |
||
| 173 | 13 | public function filter_by_types(array $types) { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdocs |
||
| 181 | */ |
||
| 182 | 24 | public function expand_relations(array $types) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritdocs |
||
| 194 | */ |
||
| 195 | public function expand_target() { |
||
| 200 | } |
||
| 201 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: