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 |
||
16 | class CliCommand extends WP_CLI_Command { |
||
17 | /** |
||
18 | * Activate a branch version |
||
19 | * |
||
20 | * ## OPTIONS |
||
21 | * |
||
22 | * activate master: Get a version of the master branch built every 15 minutes |
||
23 | * activate stable: Get the latest stable version of Jetpack |
||
24 | * activate branch_name: Get a version of PR. PR must be built and unit-tested before it become availabe |
||
25 | * list: Get list of available jetpack branches to install |
||
26 | * |
||
27 | * ## EXAMPLES |
||
28 | * |
||
29 | * wp jetpack-beta branch activate master |
||
30 | * wp jetpack-beta branch activate stable |
||
31 | * wp jetpack-beta branch activate branch_name |
||
32 | * wp jetpack-beta branch list |
||
33 | * |
||
34 | * @param array $args arguments passed to CLI, per the examples above. |
||
35 | */ |
||
36 | public function branch( $args ) { |
||
58 | |||
59 | /** |
||
60 | * Validate that we can switch branches. |
||
61 | * |
||
62 | * @param array $args arguments passed to CLI. |
||
63 | */ |
||
64 | private function validation_checks( $args ) { |
||
82 | |||
83 | /** |
||
84 | * Install Jetpack using selected branch. |
||
85 | * |
||
86 | * @param array $branch is the selected branch. |
||
87 | * @param array $section what we're specifically installing (PR, master, stable, etc). |
||
88 | */ |
||
89 | private function install_jetpack( $branch, $section ) { |
||
100 | |||
101 | /** |
||
102 | * Display list of branches. |
||
103 | */ |
||
104 | private function branches_list() { |
||
118 | } |
||
119 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.