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 |
||
20 | class MethodDocSniff implements PHP_CodeSniffer_Sniff |
||
21 | { |
||
22 | #region constants |
||
23 | /** |
||
24 | * Code for an empty doc block |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const CODE_EMPTY = 'Empty'; |
||
29 | |||
30 | /** |
||
31 | * Message for an empty doc block |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const MESSAGE_DOC_EMPTY = 'Doc comment is empty'; |
||
36 | |||
37 | /** |
||
38 | * Code for a Spacing Error after the Summary |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | const CODE_SPACING_AFTER = 'SpacingAfter'; |
||
43 | |||
44 | /** |
||
45 | * Message for a Spacing Error after the Summary |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const MESSAGE_SPACING_AFTER = 'Additional blank lines found at end of doc comment'; |
||
50 | |||
51 | /** |
||
52 | * Code for missing spacing between descriptions |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const CODE_SPACING_BETWEEN = 'SpacingBetween'; |
||
57 | |||
58 | /** |
||
59 | * Message for missing spacing between descriptions |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | const MESSAGE_SPACING_BETWEEN = 'There must be exactly one blank line between descriptions in a doc comment'; |
||
64 | |||
65 | /** |
||
66 | * Code for missing spacing before the tags |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | const CODE_SPACING_BEFORE_TAGS = 'SpacingBeforeTags'; |
||
71 | |||
72 | /** |
||
73 | * Message for missing spacing before the tags |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | const MESSAGE_SPACING_BEFORE_TAGS = 'There must be exactly one blank line before the tags in a doc comment'; |
||
78 | |||
79 | /** |
||
80 | * Code for missing short description (Summary) |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | const CODE_MISSING_SHORT = 'MissingShort'; |
||
85 | |||
86 | /** |
||
87 | * Message for missing short description (Summary) |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | const MESSAGE_MISSING_SHORT = 'Missing short description in doc comment'; |
||
92 | |||
93 | /** |
||
94 | * Code for not capitalized short description |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | const CODE_SHORT_NOT_CAPITAL = 'ShortNotCapital'; |
||
99 | |||
100 | /** |
||
101 | * Message for not capitalized short description |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | const MESSAGE_SHORT_NOT_CAPITAL = 'Doc comment short description must start with a capital letter'; |
||
106 | |||
107 | /** |
||
108 | * Code for not Capitalized long description |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | const CODE_LONG_NOT_CAPITAL = 'LongNotCapital'; |
||
113 | |||
114 | /** |
||
115 | * Message for not Capitalized long description |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | const MESSAGE_LONG_NOT_CAPITAL = 'Doc comment long description must start with a capital letter'; |
||
120 | |||
121 | /** |
||
122 | * Code for empty lines before the short description |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | const CODE_SPACING_BEFORE_SHORT = 'SpacingBeforeShort'; |
||
127 | |||
128 | /** |
||
129 | * Message for empty lines before the short description |
||
130 | * |
||
131 | * @var string |
||
132 | */ |
||
133 | const MESSAGE_CODE_SPACING_BEFORE = 'Doc comment short description must be on the first line'; |
||
134 | #endregion |
||
135 | |||
136 | /** |
||
137 | * A list of tokenizers this sniff supports. |
||
138 | * |
||
139 | * @var array |
||
140 | */ |
||
141 | public $supportedTokenizers = ['PHP', 'JS']; |
||
142 | |||
143 | /** |
||
144 | * Returns an array of tokens this test wants to listen for. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 8 | public function register() |
|
152 | |||
153 | /** |
||
154 | * Processes this test, when one of its tokens is encountered. |
||
155 | * |
||
156 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
157 | * @param int $stackPtr The position of the current token in the stack passed in $tokens. |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | 8 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
246 | } |
||
247 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.