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 |
||
| 24 | final class StyleAsset extends BaseAsset { |
||
| 25 | |||
| 26 | const MEDIA_ALL = 'all'; |
||
| 27 | const MEDIA_PRINT = 'print'; |
||
| 28 | const MEDIA_SCREEN = 'screen'; |
||
| 29 | const DEPENDENCIES = []; |
||
| 30 | const VERSION = Plugin::VERSION; |
||
| 31 | const DISABLEABLE = false; |
||
| 32 | |||
| 33 | const DEFAULT_EXTENSION = 'css'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Source location of the asset. |
||
| 37 | * |
||
| 38 | * @since %VERSION% |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $source; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Dependencies of the asset. |
||
| 46 | * |
||
| 47 | * @since %VERSION% |
||
| 48 | * |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | protected $dependencies; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Version of the asset. |
||
| 55 | * |
||
| 56 | * @since %VERSION% |
||
| 57 | * |
||
| 58 | * @var string|bool|null |
||
| 59 | */ |
||
| 60 | protected $version; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Media for which the asset is defined. |
||
| 64 | * |
||
| 65 | * @since %VERSION% |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $media; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Whether this asset can be disabled. |
||
| 73 | * |
||
| 74 | * @since %VERSION% |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $disableable; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Instantiate a StyleAsset object. |
||
| 82 | * |
||
| 83 | * @since %VERSION% |
||
| 84 | * |
||
| 85 | * @param string $handle Handle of the asset. |
||
| 86 | * @param string $source Source location of the asset. |
||
| 87 | * @param array $dependencies Optional. Dependencies of the asset. |
||
| 88 | * @param string|bool|null $version Optional. Version of the asset. |
||
| 89 | * @param string $media Media for which the asset is defined. |
||
| 90 | * @param bool $disableable Whether this script can be disabled. |
||
| 91 | */ |
||
| 92 | public function __construct( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the enqueue closure to use. |
||
| 110 | * |
||
| 111 | * @since %VERSION% |
||
| 112 | * |
||
| 113 | * @return Closure |
||
| 114 | */ |
||
| 115 | View Code Duplication | protected function get_register_closure() { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get the enqueue closure to use. |
||
| 137 | * |
||
| 138 | * @since %VERSION% |
||
| 139 | * |
||
| 140 | * @return Closure |
||
| 141 | */ |
||
| 142 | protected function get_enqueue_closure() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the dequeue closure to use. |
||
| 150 | * |
||
| 151 | * @since %VERSION% |
||
| 152 | * |
||
| 153 | * @return Closure |
||
| 154 | */ |
||
| 155 | protected function get_dequeue_closure() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Whether the current style is disabled. |
||
| 163 | * |
||
| 164 | * @since %VERSION% |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | private function is_disabled() { |
||
| 175 | } |
||
| 176 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.