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 |
||
16 | class ElasticsearchModel extends Model |
||
17 | { |
||
18 | use Elasticsearchable, Mappingable, Relationshipable; |
||
19 | |||
20 | /** |
||
21 | * @var ElasticsearchDAL |
||
22 | */ |
||
23 | public $_dal; |
||
24 | |||
25 | public function __construct(array $attributes = []) |
||
31 | |||
32 | public function injectDependencies() |
||
38 | |||
39 | public static function findWithParentId($id, $parentId, array $columns = ['*']) |
||
50 | |||
51 | /** |
||
52 | * Execute the query and get the result. |
||
53 | * |
||
54 | * @param QueryBuilder|array $query |
||
55 | * @return ElasticsearchCollection|static[] |
||
56 | */ |
||
57 | public static function search($query = []) |
||
65 | |||
66 | /** |
||
67 | * Execute the query and get the first result. |
||
68 | * |
||
69 | * @param QueryBuilder|array $query |
||
70 | * @return static |
||
71 | */ |
||
72 | public static function first($query = []) |
||
81 | |||
82 | /** |
||
83 | * Execute the query and get the first result or throw an exception. |
||
84 | * |
||
85 | * @param QueryBuilder|array $query |
||
86 | * @return static |
||
87 | * @throws ModelNotFoundException |
||
88 | */ |
||
89 | public static function firstOrFail($query = []) |
||
98 | |||
99 | /** |
||
100 | * Apply the callback to the documents of the given query. |
||
101 | * |
||
102 | * @param QueryBuilder|array $query |
||
103 | * @param callable $callback |
||
104 | * @param int $limit |
||
105 | * @return int hits.total |
||
106 | */ |
||
107 | public static function map($query = [], callable $callback, $limit = -1) |
||
136 | |||
137 | /** |
||
138 | * Execute the query and get all items. |
||
139 | * |
||
140 | * @param QueryBuilder|array $query |
||
141 | * @return Collection |
||
142 | */ |
||
143 | public static function all($query = []) |
||
154 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.