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 Repository extends Queryable implements IRepository, Interfaces\IOrderedRepository |
||
14 | { |
||
15 | /** |
||
16 | * The repository provider for the current instance |
||
17 | * |
||
18 | * @var Providers\IRepositoryProvider |
||
19 | */ |
||
20 | protected $repositoryProvider; |
||
21 | |||
22 | public function __construct( |
||
32 | |||
33 | /** |
||
34 | * Executes the supplied operation query expression on the underlying repository provider. |
||
35 | * |
||
36 | * @param O\Expression $expression |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | private function executeQuery(O\Expression $expression) |
||
41 | { |
||
42 | $this->repositoryProvider->execute($expression); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | * @return IRepository |
||
48 | */ |
||
49 | protected function newMethodSegment($name, array $arguments = []) |
||
50 | { |
||
51 | return $this->repositoryProvider->createRepository($this->newMethod($name, $arguments)); |
||
52 | } |
||
53 | |||
54 | public function join($values) |
||
55 | { |
||
56 | return new Connectors\JoiningRepository( |
||
57 | $this->repositoryProvider, |
||
58 | $this->newMethod(__FUNCTION__, [$values])); |
||
59 | } |
||
60 | |||
61 | public function groupJoin($values) |
||
62 | { |
||
63 | return new Connectors\JoiningRepository( |
||
64 | $this->repositoryProvider, |
||
65 | $this->newMethod(__FUNCTION__, [$values])); |
||
66 | } |
||
67 | |||
68 | View Code Duplication | public function addRange($values) |
|
76 | |||
77 | public function apply(callable $function) |
||
81 | |||
82 | public function remove($value) |
||
86 | |||
87 | View Code Duplication | public function removeRange($values) |
|
95 | |||
96 | public function removeWhere(callable $predicate) |
||
100 | |||
101 | public function clear() |
||
105 | |||
106 | public function offsetSet($index, $value) |
||
107 | { |
||
108 | $this->executeQuery($this->newMethod(__FUNCTION__, [$index, $value])); |
||
109 | } |
||
110 | |||
111 | public function offsetUnset($index) |
||
115 | } |
||
116 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.