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 BusinessUnitRepository extends EntityRepository |
||
13 | { |
||
14 | /** |
||
15 | * Finds the first record |
||
16 | * |
||
17 | * @return BusinessUnit |
||
18 | */ |
||
19 | public function getFirst() |
||
20 | { |
||
21 | return $this->createQueryBuilder('businessUnit') |
||
22 | ->select('businessUnit') |
||
23 | ->orderBy('businessUnit.id') |
||
24 | ->getQuery() |
||
25 | ->setMaxResults(1) |
||
26 | ->getSingleResult(); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Build business units tree for user page |
||
31 | * |
||
32 | * @param User $user |
||
33 | * @param int|null $organizationId |
||
34 | * @return array |
||
35 | * |
||
36 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
37 | */ |
||
38 | public function getBusinessUnitsTree(User $user = null, $organizationId = null) |
||
39 | { |
||
40 | $businessUnits = $this->createQueryBuilder('businessUnit')->select( |
||
41 | array( |
||
42 | 'businessUnit.id', |
||
43 | 'businessUnit.name', |
||
44 | 'IDENTITY(businessUnit.owner) parent', |
||
45 | 'IDENTITY(businessUnit.organization) organization', |
||
46 | ) |
||
47 | ); |
||
48 | if ($user && $user->getId()) { |
||
49 | $units = $user->getBusinessUnits()->map( |
||
50 | function (BusinessUnit $businessUnit) { |
||
51 | return $businessUnit->getId(); |
||
52 | } |
||
53 | ); |
||
54 | $units = $units->toArray(); |
||
55 | if ($units) { |
||
|
|||
56 | $businessUnits->addSelect('CASE WHEN businessUnit.id IN (:userUnits) THEN 1 ELSE 0 END as hasUser'); |
||
57 | $businessUnits->setParameter(':userUnits', $units); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | if ($organizationId) { |
||
62 | $businessUnits->where('businessUnit.organization = :organizationId'); |
||
63 | $businessUnits->setParameter(':organizationId', $organizationId); |
||
64 | } |
||
65 | |||
66 | $businessUnits = $businessUnits->getQuery()->getArrayResult(); |
||
67 | $children = array(); |
||
68 | foreach ($businessUnits as &$businessUnit) { |
||
69 | $parent = $businessUnit['parent'] ? : 0; |
||
70 | $children[$parent][] = & $businessUnit; |
||
71 | } |
||
72 | unset($businessUnit); |
||
73 | foreach ($businessUnits as &$businessUnit) { |
||
74 | if (isset($children[$businessUnit['id']])) { |
||
75 | $businessUnit['children'] = $children[$businessUnit['id']]; |
||
76 | } |
||
77 | } |
||
78 | unset($businessUnit); |
||
79 | if (isset($children[0])) { |
||
80 | $children = $children[0]; |
||
81 | } |
||
82 | |||
83 | return $children; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Returns business units tree by organization |
||
88 | * Or returns business units tree for given organization. |
||
89 | * |
||
90 | * @param int|null $organizationId |
||
91 | * @param array $sortOrder array with order parameters. key - organization entity field, value - sort direction |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getOrganizationBusinessUnitsTree($organizationId = null, array $sortOrder = []) |
||
96 | { |
||
97 | $tree = []; |
||
98 | $businessUnits = $this->getBusinessUnitsTree(); |
||
99 | |||
100 | $organizations = $this->_em->getRepository('OroOrganizationBundle:Organization') |
||
101 | ->getOrganizationsPartialData( |
||
102 | ['id', 'name', 'enabled'], |
||
103 | $sortOrder, |
||
104 | $organizationId ? [$organizationId] : [] |
||
105 | ); |
||
106 | foreach ($organizations as $organizationItem) { |
||
107 | $tree[$organizationItem['id']] = array_merge($organizationItem, ['children' => []]); |
||
108 | } |
||
109 | |||
110 | foreach ($businessUnits as $businessUnit) { |
||
111 | if ($businessUnit['organization'] == null) { |
||
112 | continue; |
||
113 | } |
||
114 | $tree[$businessUnit['organization']]['children'][] = $businessUnit; |
||
115 | } |
||
116 | |||
117 | if ($organizationId && isset($tree[$organizationId])) { |
||
118 | return $tree[$organizationId]['children']; |
||
119 | } |
||
120 | |||
121 | return $tree; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get business units ids |
||
126 | * |
||
127 | * @param int|null $organizationId |
||
128 | * @return array |
||
129 | */ |
||
130 | public function getBusinessUnitIds($organizationId = null) |
||
131 | { |
||
132 | $result = []; |
||
133 | /** @var QueryBuilder $businessUnitsQB */ |
||
134 | $businessUnitsQB = $this->createQueryBuilder('businessUnit'); |
||
135 | $businessUnitsQB->select('businessUnit.id'); |
||
136 | if ($organizationId != null) { |
||
137 | $businessUnitsQB |
||
138 | ->where('businessUnit.organization = :organizationId') |
||
139 | ->setParameter(':organizationId', $organizationId); |
||
140 | } |
||
141 | $businessUnits = $businessUnitsQB |
||
142 | ->getQuery() |
||
143 | ->getArrayResult(); |
||
144 | |||
145 | foreach ($businessUnits as $buId) { |
||
146 | $result[] = $buId['id']; |
||
147 | } |
||
148 | |||
149 | return $result; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $businessUnits |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function getBusinessUnits(array $businessUnits) |
||
157 | { |
||
158 | return $this->createQueryBuilder('businessUnit') |
||
159 | ->select('businessUnit') |
||
160 | ->where('businessUnit.id in (:ids)') |
||
161 | ->setParameter('ids', $businessUnits) |
||
162 | ->getQuery() |
||
163 | ->execute(); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get count of business units |
||
168 | * |
||
169 | * @return int |
||
170 | */ |
||
171 | public function getBusinessUnitsCount() |
||
172 | { |
||
173 | $qb = $this->createQueryBuilder('businessUnit'); |
||
174 | $qb->select('count(businessUnit.id)'); |
||
175 | |||
176 | return $qb->getQuery()->getSingleScalarResult(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $field |
||
181 | * @param string $entity |
||
182 | * @param string $alias |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | public function getGridFilterChoices($field, $entity, $alias = 'bu') |
||
208 | |||
209 | /** |
||
210 | * @return QueryBuilder |
||
211 | */ |
||
212 | public function getQueryBuilder() |
||
213 | { |
||
214 | return $this->createQueryBuilder('businessUnit'); |
||
216 | } |
||
217 |
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.