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 |
||
32 | class Shortcode implements ShortcodeInterface { |
||
33 | |||
34 | use ConfigTrait; |
||
35 | |||
36 | /** |
||
37 | * Name of the shortcode handler. |
||
38 | * |
||
39 | * @since 0.1.0 |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $shortcode_tag; |
||
44 | |||
45 | /** |
||
46 | * Parser to parse and validate the shortcode's attributes. |
||
47 | * |
||
48 | * @since 0.1.0 |
||
49 | * |
||
50 | * @var ShortcodeAttsParserInterface |
||
51 | */ |
||
52 | protected $atts_parser; |
||
53 | |||
54 | /** |
||
55 | * Dependencies of the shortcode. |
||
56 | * |
||
57 | * @since 0.1.0 |
||
58 | * |
||
59 | * @var DependencyManagerInterface |
||
60 | */ |
||
61 | protected $dependencies; |
||
62 | |||
63 | /** |
||
64 | * Instantiate Basic Shortcode. |
||
65 | * |
||
66 | * @since 0.1.0 |
||
67 | * |
||
68 | * @param string $shortcode_tag Tag that identifies |
||
69 | * the shortcode. |
||
70 | * @param ConfigInterface $config Configuration |
||
71 | * settings. |
||
72 | * @param ShortcodeAttsParserInterface $atts_parser Attributes parser and |
||
73 | * validator. |
||
74 | * @param DependencyManagerInterface $dependencies Dependencies of the |
||
75 | * shortcode. |
||
76 | * @throws RuntimeException If the config could not be processed. |
||
77 | */ |
||
78 | View Code Duplication | public function __construct( |
|
93 | |||
94 | /** |
||
95 | * Register the shortcode handler function with WordPress. |
||
96 | * |
||
97 | * @since 0.1.0 |
||
98 | * |
||
99 | * @param mixed $args Optional. Arguments to pass on to the Registrable. |
||
100 | * (Not used with Shortcode class) |
||
101 | * @return void |
||
102 | */ |
||
103 | public function register( $args = null ) { |
||
109 | |||
110 | /** |
||
111 | * Check whether the shortcode is needed. |
||
112 | * |
||
113 | * @since 0.2.0 |
||
114 | * |
||
115 | * @param mixed $context Data about the context in which the call is made. |
||
116 | * @return boolean Whether the shortcode is needed or not. |
||
117 | */ |
||
118 | View Code Duplication | protected function is_needed( $context = null ) { |
|
130 | |||
131 | /** |
||
132 | * Get the shortcode tag. |
||
133 | * |
||
134 | * @since 0.1.0 |
||
135 | * |
||
136 | * @return string Shortcode tag. |
||
137 | */ |
||
138 | public function get_tag() { |
||
141 | |||
142 | /** |
||
143 | * Render the shortcode. |
||
144 | * |
||
145 | * @since 0.1.0 |
||
146 | * |
||
147 | * @throws DomainException |
||
148 | * |
||
149 | * @param array $atts Attributes to modify the standard behavior |
||
150 | * of the shortcode. |
||
151 | * @param string|null $content Optional. Content between enclosing |
||
152 | * shortcodes. |
||
153 | * @param string|null $tag Optional. The tag of the shortcode to |
||
154 | * render. |
||
155 | * @return string The shortcode's HTML output. |
||
156 | */ |
||
157 | public function render( $atts, $content = null, $tag = null ) { |
||
174 | } |
||
175 |
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.