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 |
||
28 | class TicketMutation |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Maps the GraphQL input to a format that the model functions can use |
||
33 | * |
||
34 | * @param array $input Data coming from the GraphQL mutation query input |
||
35 | * @return array |
||
36 | */ |
||
37 | public static function prepareFields(array $input) |
||
74 | |||
75 | /** |
||
76 | * Sets the related datetimes for the given ticket. |
||
77 | * |
||
78 | * @param EE_Ticket $entity The Ticket instance. |
||
79 | * @param array $datetimes Array of datetime IDs to relate. |
||
80 | */ |
||
81 | View Code Duplication | public static function setRelatedDatetimes($entity, array $datetimes) |
|
98 | |||
99 | /** |
||
100 | * Sets the related prices for the given ticket. |
||
101 | * |
||
102 | * @param EE_Ticket $entity The Ticket instance. |
||
103 | * @param array $prices Array of entity IDs to relate. |
||
104 | */ |
||
105 | View Code Duplication | public static function setRelatedPrices($entity, array $prices) |
|
122 | |||
123 | |||
124 | /** |
||
125 | * @param EE_Ticket $ticket_entity |
||
126 | * @param EEM_Ticket $ticket_model |
||
127 | * @throws DomainException |
||
128 | * @throws EE_Error |
||
129 | * @throws InvalidArgumentException |
||
130 | * @throws InvalidDataTypeException |
||
131 | * @throws InvalidInterfaceException |
||
132 | * @throws ModelConfigurationException |
||
133 | * @throws ReflectionException |
||
134 | * @throws RestException |
||
135 | * @throws UnexpectedEntityException |
||
136 | * @since $VID:$ |
||
137 | */ |
||
138 | public static function addDefaultPrices(EE_Ticket $ticket_entity, EEM_Ticket $ticket_model) |
||
147 | } |
||
148 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.