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 |
||
| 18 | class HooksMustHaveDocblockSniff extends AbstractFunctionRestrictionsSniff { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Array of WordPress hook execution functions. |
||
| 22 | * |
||
| 23 | * @var array WordPress hook execution function name => filter or action. |
||
| 24 | */ |
||
| 25 | protected $hook_functions = array( |
||
| 26 | 'apply_filters' => 'filter', |
||
| 27 | 'apply_filters_ref_array' => 'filter', |
||
| 28 | 'apply_filters_deprecated' => 'filter', |
||
| 29 | 'do_action' => 'action', |
||
| 30 | 'do_action_ref_array' => 'action', |
||
| 31 | 'do_action_deprecated' => 'action', |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Groups of functions to restrict. |
||
| 36 | * |
||
| 37 | * Example: groups => array( |
||
| 38 | * 'lambda' => array( |
||
| 39 | * 'type' => 'error' | 'warning', |
||
| 40 | * 'message' => 'Use anonymous functions instead please!', |
||
| 41 | * 'functions' => array( 'file_get_contents', 'create_function' ), |
||
| 42 | * ) |
||
| 43 | * ) |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function getGroups() { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Process a matched token. |
||
| 57 | * |
||
| 58 | * @since 1.0.0 Logic split off from the `process_token()` method. |
||
| 59 | * |
||
| 60 | * @param int $stack_ptr The position of the current token in the stack. |
||
| 61 | * @param string $group_name The name of the group which was matched. |
||
| 62 | * @param string $matched_content The token content (function name) which was matched. |
||
| 63 | * |
||
| 64 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
| 65 | * normal file processing. |
||
| 66 | */ |
||
| 67 | public function process_matched_token( $stack_ptr, $group_name, $matched_content ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Helper function to identify the comment previous to a pointer reference. |
||
| 138 | * |
||
| 139 | * @param int $stack_ptr The position of the token in the stack. |
||
| 140 | */ |
||
| 141 | protected function return_previous_comment( $stack_ptr ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the starting comment reference when passed an end reference. |
||
| 147 | * |
||
| 148 | * Used to help set bounds for searching through a docblock. |
||
| 149 | * |
||
| 150 | * @param int $end The position of the ending token in the stack. |
||
| 151 | */ |
||
| 152 | protected function return_comment_start( $end ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Determines if a filter docblock is referencing a complete docblock elsewhere. |
||
| 158 | * |
||
| 159 | * @param int $start The position of the starting token in the stack. |
||
| 160 | * @param int $end The position of the ending token in the stack. |
||
| 161 | */ |
||
| 162 | protected function is_previously_documented( $start, $end ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Verifies the match is valid and worthy of continued processing. |
||
| 174 | * |
||
| 175 | * @param int $stack_ptr The position of the token in the stack. |
||
| 176 | * |
||
| 177 | * @return bool True for valid. |
||
| 178 | */ |
||
| 179 | protected function verify_valid_match( $stack_ptr ) { |
||
| 191 | } |
||
| 192 |