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:
Complex classes like NodeRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NodeRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class NodeRepository extends NestedTreeRepository |
||
21 | { |
||
22 | /** |
||
23 | * @param string $lang The locale |
||
24 | * @param string $permission The permission (read, write, ...) |
||
25 | * @param AclHelper $aclHelper The acl helper |
||
26 | * @param bool $includeHiddenFromNav include the hiddenfromnav nodes |
||
27 | * or not |
||
28 | * |
||
29 | * @return Node[] |
||
30 | */ |
||
31 | public function getTopNodes( |
||
32 | $lang, |
||
33 | $permission, |
||
34 | AclHelper $aclHelper, |
||
35 | $includeHiddenFromNav = false |
||
36 | ) { |
||
37 | $result = $this->getChildNodes( |
||
38 | null, |
||
39 | $lang, |
||
40 | $permission, |
||
41 | $aclHelper, |
||
42 | $includeHiddenFromNav |
||
43 | ); |
||
44 | |||
45 | return $result; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param int|null $parentId The parent node id |
||
50 | * @param string $lang The locale |
||
51 | * @param string $permission The permission (read, write, ...) |
||
52 | * @param AclHelper $aclHelper The acl helper |
||
53 | * @param bool $includeHiddenFromNav Include nodes hidden from |
||
54 | * navigation or not |
||
55 | * @param Node $rootNode Root node of the current tree |
||
|
|||
56 | * |
||
57 | * @return Node[] |
||
58 | */ |
||
59 | public function getChildNodes( |
||
112 | |||
113 | /** |
||
114 | * @param HasNodeInterface $hasNode |
||
115 | * |
||
116 | * @return Node|null |
||
117 | */ |
||
118 | public function getNodeFor(HasNodeInterface $hasNode) |
||
136 | |||
137 | /** |
||
138 | * @param int $id The id |
||
139 | * @param string $entityName The class name |
||
140 | * |
||
141 | * @return Node|null |
||
142 | */ |
||
143 | public function getNodeForIdAndEntityname($id, $entityName) |
||
157 | |||
158 | /** |
||
159 | * @param Node $parentNode The parent node (may be null) |
||
160 | * @param string $slug The slug |
||
161 | * |
||
162 | * @return Node|null |
||
163 | */ |
||
164 | public function getNodeForSlug(Node $parentNode, $slug) |
||
186 | |||
187 | /** |
||
188 | * @param HasNodeInterface $hasNode The object to link to |
||
189 | * @param string $lang The locale |
||
190 | * @param BaseUser $owner The user |
||
191 | * @param string $internalName The internal name (may be null) |
||
192 | * |
||
193 | * @throws \InvalidArgumentException |
||
194 | * |
||
195 | * @return Node |
||
196 | */ |
||
197 | public function createNodeFor( |
||
244 | |||
245 | /** |
||
246 | * Get all the information needed to build a menu tree with one query. |
||
247 | * We only fetch the fields we need, instead of fetching full objects to |
||
248 | * limit the memory usage. |
||
249 | * |
||
250 | * @param string $lang The locale |
||
251 | * @param string $permission The permission (read, |
||
252 | * write, ...) |
||
253 | * @param AclNativeHelper $aclNativeHelper The acl helper |
||
254 | * @param bool $includeHiddenFromNav Include nodes hidden from |
||
255 | * navigation or not |
||
256 | * @param Node $rootNode The root node of the |
||
257 | * current site |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getAllMenuNodes( |
||
262 | $lang, |
||
263 | $permission, |
||
264 | AclNativeHelper $aclNativeHelper, |
||
265 | $includeHiddenFromNav = false, |
||
266 | Node $rootNode = null |
||
267 | ) { |
||
268 | $connection = $this->_em->getConnection(); |
||
269 | $qb = $connection->createQueryBuilder(); |
||
270 | $databasePlatformName = $connection->getDatabasePlatform()->getName(); |
||
271 | $createIfStatement = function ( |
||
272 | $expression, |
||
273 | $trueValue, |
||
274 | $falseValue |
||
275 | ) use ($databasePlatformName) { |
||
276 | switch ($databasePlatformName) { |
||
277 | case 'sqlite': |
||
278 | $statement = 'CASE WHEN %s THEN %s ELSE %s END'; |
||
279 | |||
280 | break; |
||
281 | |||
282 | default: |
||
283 | $statement = 'IF(%s, %s, %s)'; |
||
284 | } |
||
285 | |||
286 | return sprintf($statement, $expression, $trueValue, $falseValue); |
||
287 | }; |
||
288 | |||
289 | $sql = <<<SQL |
||
290 | n.id, n.parent_id AS parent, t.url, t.id AS nt_id, |
||
291 | {$createIfStatement('t.weight IS NULL', 'v.weight', 't.weight')} AS weight, |
||
292 | {$createIfStatement('t.title IS NULL', 'v.title', 't.title')} AS title, |
||
293 | {$createIfStatement('t.online IS NULL', '0', 't.online')} AS online, |
||
294 | n.hidden_from_nav AS hidden, |
||
295 | n.ref_entity_name AS ref_entity_name |
||
296 | SQL; |
||
297 | |||
298 | $qb->select($sql) |
||
299 | ->from('kuma_nodes', 'n') |
||
300 | ->leftJoin( |
||
301 | 'n', |
||
302 | 'kuma_node_translations', |
||
303 | 't', |
||
304 | '(t.node_id = n.id AND t.lang = :lang)' |
||
305 | ) |
||
306 | ->leftJoin( |
||
307 | 'n', |
||
308 | 'kuma_node_translations', |
||
309 | 'v', |
||
310 | '(v.node_id = n.id AND v.lang <> :lang)' |
||
311 | ) |
||
312 | ->where('n.deleted = 0') |
||
313 | ->addGroupBy('n.id') |
||
314 | ->addOrderBy('t.weight', 'ASC') |
||
315 | ->addOrderBy('t.title', 'ASC'); |
||
316 | |||
317 | if (!$includeHiddenFromNav) { |
||
318 | $qb->andWhere('n.hidden_from_nav <> 0'); |
||
319 | } |
||
320 | |||
321 | if (!\is_null($rootNode)) { |
||
322 | $qb->andWhere('n.lft >= :left') |
||
323 | ->andWhere('n.rgt <= :right'); |
||
324 | } |
||
325 | |||
326 | $permissionDef = new PermissionDefinition(array($permission)); |
||
327 | $permissionDef->setEntity('Kunstmaan\NodeBundle\Entity\Node'); |
||
328 | $permissionDef->setAlias('n'); |
||
329 | $qb = $aclNativeHelper->apply($qb, $permissionDef); |
||
330 | |||
331 | $stmt = $this->_em->getConnection()->prepare($qb->getSQL()); |
||
332 | $stmt->bindValue(':lang', $lang); |
||
333 | if (!\is_null($rootNode)) { |
||
334 | $stmt->bindValue(':left', $rootNode->getLeft()); |
||
335 | $stmt->bindValue(':right', $rootNode->getRight()); |
||
336 | } |
||
337 | $stmt->execute(); |
||
338 | |||
339 | return $stmt->fetchAll(); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get all parents of a given node. We can go multiple levels up. |
||
344 | * |
||
345 | * @param Node $node |
||
346 | * @param string $lang |
||
347 | * |
||
348 | * @return Node[] |
||
349 | */ |
||
350 | View Code Duplication | public function getAllParents(Node $node = null, $lang = null) |
|
385 | |||
386 | /** |
||
387 | * Get the root node of a given node. |
||
388 | * |
||
389 | * @param Node $node |
||
390 | * @param string $lang |
||
391 | * |
||
392 | * @return Node |
||
393 | */ |
||
394 | View Code Duplication | public function getRootNodeFor(Node $node = null, $lang = null) |
|
428 | |||
429 | /** |
||
430 | * @return Node[] |
||
431 | */ |
||
432 | public function getAllTopNodes() |
||
448 | |||
449 | /** |
||
450 | * Get an array of Nodes based on the internal name. |
||
451 | * |
||
452 | * @param string $internalName The internal name of the node |
||
453 | * @param string $lang The locale |
||
454 | * @param int|null|bool $parentId The parent id |
||
455 | * @param bool $includeOffline Include offline nodes |
||
456 | * |
||
457 | * @return Node[] |
||
458 | */ |
||
459 | public function getNodesByInternalName( |
||
499 | |||
500 | /** |
||
501 | * Get a single node by internal name. |
||
502 | * |
||
503 | * @param string $internalName The internal name of the node |
||
504 | * |
||
505 | * @return Node |
||
506 | */ |
||
507 | public function getNodeByInternalName($internalName) |
||
517 | |||
518 | /** |
||
519 | * Finds all different page classes currently registered as nodes |
||
520 | * |
||
521 | * @return string[] |
||
522 | */ |
||
523 | public function findAllDistinctPageClasses() |
||
532 | |||
533 | public function getChildCount(Node $node, bool $direct = false, bool $includeDeleted = false): int |
||
549 | } |
||
550 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.