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 |
||
9 | class ExplainQueryService |
||
10 | { |
||
11 | const VERBOSITY_QUERY_PLANNER = 'queryPlanner'; |
||
12 | const VERBOSITY_EXECUTION_STATS = 'executionStats'; |
||
13 | const VERBOSITY_ALL_PLAN_EXECUTION = 'allPlansExecution'; |
||
14 | |||
15 | public static $acceptedMethods= [ |
||
16 | 'count', |
||
17 | 'distinct', |
||
18 | 'find', |
||
19 | 'findOne', |
||
20 | 'findOneAndUpdate', |
||
21 | 'findOneAndDelete', |
||
22 | 'deleteOne', |
||
23 | 'deleteMany', |
||
24 | 'aggregate', |
||
25 | ]; |
||
26 | |||
27 | /** @var ClientRegistry */ |
||
28 | private $clientRegistry; |
||
29 | |||
30 | /** |
||
31 | * Constructs a explain command. |
||
32 | * |
||
33 | * Supported options: |
||
34 | * verbosity : queryPlanner | executionStats Mode | allPlansExecution (default) |
||
35 | * The explain command provides information on the execution of the following commands: |
||
36 | * count, distinct, group, find, findAndModify, delete, and update. |
||
37 | * |
||
38 | * @param ClientRegistry $clientRegistry |
||
39 | */ |
||
40 | public function __construct(ClientRegistry $clientRegistry) |
||
45 | |||
46 | /** |
||
47 | * Execute the operation. |
||
48 | * |
||
49 | * @param Query $query |
||
50 | * @param string $verbosity |
||
51 | * |
||
52 | * @return Cursor |
||
53 | */ |
||
54 | public function execute(Query $query, string $verbosity = self::VERBOSITY_ALL_PLAN_EXECUTION): Cursor |
||
67 | |||
68 | /** |
||
69 | * Create the explain command. |
||
70 | * |
||
71 | * @param Query $query |
||
72 | * @param string $verbosity |
||
73 | * |
||
74 | * @return Command |
||
75 | */ |
||
76 | private function createCommand(Query $query, string $verbosity) |
||
109 | |||
110 | private function getCountArgs(Query $query) |
||
122 | |||
123 | private function getFindArgs(Query $query) |
||
136 | |||
137 | private function getDistinctArgs(Query $query) |
||
144 | |||
145 | private function getDeleteArgs(Query $query) |
||
154 | } |
||
155 |
As per the PSR-2 coding standard, the
break
(or other terminating) statement must be on a line of its own.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.