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 |
||
| 26 | class Loader { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The array of actions registered with WordPress. |
||
| 30 | * |
||
| 31 | * @since 1.0.0 |
||
| 32 | * @access protected |
||
| 33 | * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
||
| 34 | */ |
||
| 35 | protected $actions; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The array of filters registered with WordPress. |
||
| 39 | * |
||
| 40 | * @since 1.0.0 |
||
| 41 | * @access protected |
||
| 42 | * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
||
| 43 | */ |
||
| 44 | protected $filters; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Initialize the collections used to maintain the actions and filters. |
||
| 48 | * |
||
| 49 | * @since 1.0.0 |
||
| 50 | */ |
||
| 51 | public function __construct() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add a new action to the collection to be registered with WordPress. |
||
| 60 | * |
||
| 61 | * @since 1.0.0 |
||
| 62 | * @param string $hook The name of the WordPress action that is being registered. |
||
| 63 | * @param object $component A reference to the instance of the object on which the action is defined. |
||
| 64 | * @param string $callback The name of the function definition on the $component. |
||
| 65 | * @param int Optional $priority The priority at which the function should be fired. |
||
| 66 | * @param int Optional $accepted_args The number of arguments that should be passed to the $callback. |
||
| 67 | */ |
||
| 68 | public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Add a new filter to the collection to be registered with WordPress. |
||
| 74 | * |
||
| 75 | * @since 1.0.0 |
||
| 76 | * @param string $hook The name of the WordPress filter that is being registered. |
||
| 77 | * @param object $component A reference to the instance of the object on which the filter is defined. |
||
| 78 | * @param string $callback The name of the function definition on the $component. |
||
| 79 | * @param int Optional $priority The priority at which the function should be fired. |
||
| 80 | * @param int Optional $accepted_args The number of arguments that should be passed to the $callback. |
||
| 81 | */ |
||
| 82 | public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * A utility function that is used to register the actions and hooks into a single |
||
| 88 | * collection. |
||
| 89 | * |
||
| 90 | * @since 1.0.0 |
||
| 91 | * @access private |
||
| 92 | * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
||
| 93 | * @param string $hook The name of the WordPress filter that is being registered. |
||
| 94 | * @param object $component A reference to the instance of the object on which the filter is defined. |
||
| 95 | * @param string $callback The name of the function definition on the $component. |
||
| 96 | * @param int Optional $priority The priority at which the function should be fired. |
||
| 97 | * @param int Optional $accepted_args The number of arguments that should be passed to the $callback. |
||
| 98 | * @return type The collection of actions and filters registered with WordPress. |
||
|
|
|||
| 99 | */ |
||
| 100 | private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Register the filters and actions with WordPress. |
||
| 116 | * |
||
| 117 | * @since 1.0.0 |
||
| 118 | */ |
||
| 119 | public function run() { |
||
| 130 | |||
| 131 | } |
||
| 132 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.