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 |
||
18 | class TicketConnectionResolver extends AbstractConnectionResolver { |
||
19 | |||
20 | /** |
||
21 | * @return EEM_Ticket |
||
22 | * @throws EE_Error |
||
23 | * @throws InvalidArgumentException |
||
24 | * @throws InvalidDataTypeException |
||
25 | * @throws InvalidInterfaceException |
||
26 | */ |
||
27 | public function get_query() { |
||
30 | |||
31 | /** |
||
32 | * Return an array of items from the query |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | public function get_items() { |
||
42 | |||
43 | /** |
||
44 | * Determine whether the Query should execute. If it's determined that the query should |
||
45 | * not be run based on context such as, but not limited to, who the user is, where in the |
||
46 | * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
||
47 | * |
||
48 | * Return false to prevent the query from executing. |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function should_execute() { |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Here, we map the args from the input, then we make sure that we're only querying |
||
64 | * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
||
65 | * handle batch resolution of the posts. |
||
66 | * |
||
67 | * @return array |
||
68 | * @throws EE_Error |
||
69 | * @throws InvalidArgumentException |
||
70 | * @throws ReflectionException |
||
71 | * @throws InvalidDataTypeException |
||
72 | * @throws InvalidInterfaceException |
||
73 | */ |
||
74 | public function get_query_args() { |
||
123 | |||
124 | |||
125 | /** |
||
126 | * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query |
||
127 | * friendly keys. There's probably a cleaner/more dynamic way to approach this, but |
||
128 | * this was quick. I'd be down to explore more dynamic ways to map this, but for |
||
129 | * now this gets the job done. |
||
130 | * |
||
131 | * @param array $query_args |
||
132 | * @return array |
||
133 | */ |
||
134 | View Code Duplication | public function sanitize_input_fields(array $query_args) { |
|
147 | |||
148 | } |
||
149 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.