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:
Complex classes like Register often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Register, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Register implements RegisterContract { |
||
| 15 | /** |
||
| 16 | * Url to the plugin directory. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $url; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Script/plugin version. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $version; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Array of script definition arrays. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $scripts = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Array of style definition arrays. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $styles = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Instantiates a new instance of the Register class. |
||
| 45 | * |
||
| 46 | * The URL param should be relative to the plugin directory. The URL |
||
| 47 | * form should always end with a '/'. All asset location definitions |
||
| 48 | * should not begin with a slash and should be relative to the plugin's |
||
| 49 | * root directory. The URL provided by default from the Application |
||
| 50 | * class is compatible. |
||
| 51 | * |
||
| 52 | * @param string $url |
||
| 53 | * @param string $version |
||
| 54 | */ |
||
| 55 | public function __construct( $url, $version = null ) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | 45 | * |
|
| 63 | 45 | * @param array $script |
|
| 64 | 45 | */ |
|
| 65 | 45 | public function register_script( $script ) { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | * |
||
| 72 | 42 | * @param array $style |
|
| 73 | 42 | */ |
|
| 74 | 42 | public function register_style( $style ) { |
|
| 77 | |||
| 78 | 42 | /** |
|
| 79 | * {@inheritDoc} |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function enqueue_web_scripts() { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritDoc} |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function enqueue_web_styles() { |
|
| 99 | |||
| 100 | /** |
||
| 101 | 15 | * {@inheritDoc} |
|
| 102 | 15 | * |
|
| 103 | 15 | * @param string $hook Passes a string representing the current page. |
|
| 104 | 15 | */ |
|
| 105 | 10 | View Code Duplication | public function enqueue_admin_scripts( $hook ) { |
| 112 | 12 | ||
| 113 | 12 | /** |
|
| 114 | 12 | * {@inheritDoc} |
|
| 115 | 12 | * |
|
| 116 | 8 | * @param string $hook Passes a string representing the current page. |
|
| 117 | 8 | */ |
|
| 118 | 12 | View Code Duplication | public function enqueue_admin_styles( $hook ) { |
| 125 | 6 | ||
| 126 | 6 | /** |
|
| 127 | 6 | * {@inheritDoc} |
|
| 128 | 6 | */ |
|
| 129 | 4 | public function register_blocks() { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritDoc} |
||
| 177 | * |
||
| 178 | * @return array[] |
||
| 179 | */ |
||
| 180 | public function action_hooks() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Enqueues an individual script if the style's condition is met. |
||
| 207 | * |
||
| 208 | * @param array $script The script attachment callback. |
||
| 209 | * @param string $hook The location hook. Only passed on admin side. |
||
| 210 | */ |
||
| 211 | View Code Duplication | protected function enqueue_script( $script, $hook = null ) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Enqueues an individual stylesheet if the style's condition is met. |
||
| 227 | * |
||
| 228 | * @param array $style The style attachment callback. |
||
| 229 | * @param string $hook The location hook. |
||
| 230 | */ |
||
| 231 | 18 | View Code Duplication | protected function enqueue_style( $style, $hook = null ) { |
| 242 | 8 | ||
| 243 | 18 | /** |
|
| 244 | * Registers the localization of the provided script. |
||
| 245 | * |
||
| 246 | * @param array $script Script defintion. |
||
| 247 | */ |
||
| 248 | protected function localize_script( $script ) { |
||
| 263 | } |
||
| 264 |
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.