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 |
||
20 | class DatetimeConnectionResolver extends AbstractConnectionResolver { |
||
21 | |||
22 | /** |
||
23 | * @return EEM_Datetime |
||
24 | * @throws EE_Error |
||
25 | * @throws InvalidArgumentException |
||
26 | * @throws InvalidDataTypeException |
||
27 | * @throws InvalidInterfaceException |
||
28 | */ |
||
29 | public function get_query() { |
||
32 | |||
33 | /** |
||
34 | * Return an array of items from the query |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | public function get_items() { |
||
44 | |||
45 | /** |
||
46 | * Determine whether the Query should execute. If it's determined that the query should |
||
47 | * not be run based on context such as, but not limited to, who the user is, where in the |
||
48 | * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
||
49 | * |
||
50 | * Return false to prevent the query from executing. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function should_execute() { |
||
62 | |||
63 | /** |
||
64 | * Here, we map the args from the input, then we make sure that we're only querying |
||
65 | * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
||
66 | * handle batch resolution of the posts. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function get_query_args() { |
||
133 | |||
134 | |||
135 | /** |
||
136 | * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query |
||
137 | * friendly keys. There's probably a cleaner/more dynamic way to approach this, but |
||
138 | * this was quick. I'd be down to explore more dynamic ways to map this, but for |
||
139 | * now this gets the job done. |
||
140 | * |
||
141 | * @param array $query_args |
||
142 | * @return array |
||
143 | */ |
||
144 | View Code Duplication | public function sanitize_input_fields(array $query_args) { |
|
157 | |||
158 | } |
||
159 |
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.