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 |
||
11 | class ReadFileQueryCreator extends PaginatedQueryCreator |
||
|
|||
12 | { |
||
13 | |||
14 | public function attributes() |
||
20 | |||
21 | public function createConnection() |
||
22 | { |
||
23 | return Connection::create('readFiles') |
||
24 | ->setConnectionType(function () { |
||
25 | $unionType = new UnionType([ |
||
26 | 'name' => 'Result', |
||
27 | 'types' => [ |
||
28 | $this->manager->getType('File'), |
||
29 | $this->manager->getType('Folder') |
||
30 | ], |
||
31 | View Code Duplication | 'resolveType' => function ($object) { |
|
32 | if ($object instanceof Folder) { |
||
33 | return $this->manager->getType('Folder'); |
||
34 | } |
||
35 | if ($object instanceof File) { |
||
36 | return $this->manager->getType('File'); |
||
37 | } |
||
38 | return null; |
||
39 | } |
||
40 | ]); |
||
41 | return $unionType; |
||
42 | }) |
||
43 | ->setArgs(function () { |
||
44 | return [ |
||
45 | 'filter' => [ |
||
46 | 'type' => $this->manager->getType('FileFilterInput') |
||
47 | ] |
||
48 | ]; |
||
49 | }) |
||
50 | ->setSortableFields(['ID', 'Title', 'Created', 'LastEdited']) |
||
51 | ->setConnectionResolver(array($this, 'resolveConnection')); |
||
52 | } |
||
53 | |||
54 | public function resolveConnection($object, array $args, $context, $info) |
||
96 | } |
||
97 |