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 |
||
12 | class LeadRepository extends EntityRepository |
||
13 | { |
||
14 | /** |
||
15 | * Returns top $limit opportunities grouped by lead source |
||
16 | * |
||
17 | * @param AclHelper $aclHelper |
||
18 | * @param int $limit |
||
19 | * @param array $dateRange |
||
20 | * |
||
21 | * @return array [itemCount, label] |
||
22 | */ |
||
23 | public function getOpportunitiesByLeadSource(AclHelper $aclHelper, $limit = 10, $dateRange = null, $owners = []) |
||
44 | |||
45 | /** |
||
46 | * @param array $rows |
||
47 | * @param int $limit |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | protected function processOpportunitiesByLeadSource(array $rows, $limit) |
||
94 | |||
95 | /** |
||
96 | * @param array $rows |
||
97 | * |
||
98 | * @return int |
||
99 | */ |
||
100 | protected function sumCount(array $rows) |
||
109 | |||
110 | /** |
||
111 | * @param array $rows |
||
112 | */ |
||
113 | protected function sortByCountReverse(array &$rows) |
||
126 | |||
127 | /** |
||
128 | * @param AclHelper $aclHelper |
||
129 | * @param \DateTime $start |
||
130 | * @param \DateTime $end |
||
131 | * @param int[] $owners |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function getLeadsCount(AclHelper $aclHelper, \DateTime $start = null, \DateTime $end = null, $owners = []) |
||
141 | |||
142 | /** |
||
143 | * @param AclHelper $aclHelper |
||
144 | * @param \DateTime $start |
||
145 | * @param \DateTime $end |
||
146 | * @param int[] $owners |
||
147 | * |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getNewLeadsCount(AclHelper $aclHelper, \DateTime $start = null, \DateTime $end = null, $owners = []) |
||
158 | |||
159 | /** |
||
160 | * @param \DateTime $start |
||
161 | * @param \DateTime $end |
||
162 | * @param int[] $owners |
||
163 | * |
||
164 | * @return QueryBuilder |
||
165 | */ |
||
166 | View Code Duplication | protected function createLeadsCountQb(\DateTime $start = null, \DateTime $end = null, $owners = []) |
|
190 | } |
||
191 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.