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 |
||
28 | class ShortcodeUI implements ShortcodeUIInterface { |
||
29 | |||
30 | use ConfigTrait; |
||
31 | |||
32 | /** |
||
33 | * Name of the shortcode handler. |
||
34 | * |
||
35 | * @since 0.1.0 |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $shortcode_tag; |
||
40 | |||
41 | /** |
||
42 | * Dependencies to be enqueued. |
||
43 | * |
||
44 | * @since 0.1.0 |
||
45 | * |
||
46 | * @var DependencyManagerInterface |
||
47 | */ |
||
48 | protected $dependencies; |
||
49 | |||
50 | /** |
||
51 | * Instantiate Basic Shortcode UI. |
||
52 | * |
||
53 | * @since 1.0. |
||
54 | * |
||
55 | * @param string $shortcode_tag Tag of the Shortcode. |
||
56 | * @param ConfigInterface $config Configuration settings. |
||
57 | * @param DependencyManager|null $dependencies Optional. Dependencies that |
||
58 | * need to be enqueued. |
||
59 | * @throws RuntimeException If the config could not be processed. |
||
60 | */ |
||
61 | View Code Duplication | public function __construct( |
|
|
|||
62 | $shortcode_tag, |
||
63 | ConfigInterface $config, |
||
64 | DependencyManager $dependencies = null |
||
65 | ) { |
||
66 | Assert\that( $shortcode_tag )->string()->notEmpty(); |
||
67 | |||
68 | $this->processConfig( $config ); |
||
69 | |||
70 | $this->shortcode_tag = $shortcode_tag; |
||
71 | $this->dependencies = $dependencies; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Register the shortcode UI handler function with WordPress. |
||
76 | * |
||
77 | * @since 0.1.0 |
||
78 | * |
||
79 | * @param mixed $context Data about the context in which the call is made. |
||
80 | * @return void |
||
81 | */ |
||
82 | public function register( $context = null ) { |
||
92 | |||
93 | /** |
||
94 | * Check whether the shortcode UI is needed. |
||
95 | * |
||
96 | * @since 0.1.0 |
||
97 | * |
||
98 | * @param mixed $context Data about the context in which the call is made. |
||
99 | * @return boolean Whether the shortcode UI is needed or not. |
||
100 | */ |
||
101 | View Code Duplication | protected function is_needed( $context = null ) { |
|
114 | } |
||
115 |
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.