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 |
||
19 | class HooksInlineDocsSniff extends AbstractFunctionRestrictionsSniff { |
||
20 | |||
21 | /** |
||
22 | * Array of WordPress hook execution functions. |
||
23 | * |
||
24 | * @var array WordPress hook execution function name => filter or action. |
||
25 | */ |
||
26 | protected $hook_functions = array( |
||
27 | 'apply_filters' => 'filter', |
||
28 | 'apply_filters_ref_array' => 'filter', |
||
29 | 'apply_filters_deprecated' => 'filter', |
||
30 | 'do_action' => 'action', |
||
31 | 'do_action_ref_array' => 'action', |
||
32 | 'do_action_deprecated' => 'action', |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Array of allowed exceptional version numbers. |
||
37 | * |
||
38 | * By default, X.Y.Z version numbers are required. If there are any exceptions, |
||
39 | * they can be passed in the ruleset XML file via: |
||
40 | * <rule ref="Jetpack.Commenting.HooksInlineDocs"> |
||
41 | * <properties> |
||
42 | * <property name="allowed_extra_versions" type="array"> |
||
43 | * <element key="0.71" value="0.71"/> |
||
44 | * <element key="MU (3.0.0)" value="MU (3.0.0)"/> |
||
45 | * </property> |
||
46 | * </rule> |
||
47 | * |
||
48 | * The key values are used to determine if a version is allowed outside of the X.Y.Z scheme. The value is not considered. |
||
49 | */ |
||
50 | public $allowed_extra_versions = array(); |
||
51 | |||
52 | /** |
||
53 | * Groups of functions to restrict. |
||
54 | * |
||
55 | * Example: groups => array( |
||
56 | * 'lambda' => array( |
||
57 | * 'type' => 'error' | 'warning', |
||
58 | * 'message' => 'Use anonymous functions instead please!', |
||
59 | * 'functions' => array( 'file_get_contents', 'create_function' ), |
||
60 | * ) |
||
61 | * ) |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getGroups() { |
||
72 | |||
73 | /** |
||
74 | * Process a matched token. |
||
75 | * |
||
76 | * @since 1.0.0 Logic split off from the `process_token()` method. |
||
77 | * |
||
78 | * @param int $stack_ptr The position of the current token in the stack. |
||
79 | * @param string $group_name The name of the group which was matched. |
||
80 | * @param string $matched_content The token content (function name) which was matched. |
||
81 | * |
||
82 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
83 | * normal file processing. |
||
84 | */ |
||
85 | public function process_matched_token( $stack_ptr, $group_name, $matched_content ) { |
||
154 | |||
155 | /** |
||
156 | * Helper function to identify the comment previous to a pointer reference. |
||
157 | * |
||
158 | * @param int $stack_ptr The position of the token in the stack. |
||
159 | */ |
||
160 | protected function return_previous_comment( $stack_ptr ) { |
||
163 | |||
164 | /** |
||
165 | * Returns the starting comment reference when passed an end reference. |
||
166 | * |
||
167 | * Used to help set bounds for searching through a docblock. |
||
168 | * |
||
169 | * @param int $end The position of the ending token in the stack. |
||
170 | */ |
||
171 | protected function return_comment_start( $end ) { |
||
174 | |||
175 | /** |
||
176 | * Determines if a filter docblock is referencing a complete docblock elsewhere. |
||
177 | * |
||
178 | * @param int $start The position of the starting token in the stack. |
||
179 | * @param int $end The position of the ending token in the stack. |
||
180 | * |
||
181 | * @return bool If docblock matches the previously documented convention. |
||
182 | */ |
||
183 | protected function is_previously_documented( $start, $end ) { |
||
198 | |||
199 | /** |
||
200 | * Verifies the match is valid and worthy of continued processing. |
||
201 | * |
||
202 | * @param int $stack_ptr The position of the token in the stack. |
||
203 | * |
||
204 | * @return bool True for valid. |
||
205 | */ |
||
206 | protected function verify_valid_match( $stack_ptr ) { |
||
218 | |||
219 | protected function array_begins_with( $string, $array ){ |
||
227 | } |
||
228 |