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 |
||
| 23 | abstract class BlockAssetManager extends AssetManager implements BlockAssetManagerInterface |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string $editor_script_handle |
||
| 28 | */ |
||
| 29 | private $editor_script_handle; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string $editor_style_handle |
||
| 33 | */ |
||
| 34 | private $editor_style_handle; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string $script_handle |
||
| 38 | */ |
||
| 39 | private $script_handle; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string $style_handle |
||
| 43 | */ |
||
| 44 | private $style_handle; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function getEditorScriptHandle() |
||
| 51 | { |
||
| 52 | return $this->editor_script_handle; |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $editor_script_handle |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function setEditorScriptHandle($editor_script_handle) |
|
| 60 | { |
||
| 61 | if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
||
| 62 | $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
||
| 63 | } |
||
| 64 | $this->editor_script_handle = $editor_script_handle; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getEditorStyleHandle() |
||
| 72 | { |
||
| 73 | return $this->editor_style_handle; |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $editor_style_handle |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function setEditorStyleHandle($editor_style_handle) |
|
| 81 | { |
||
| 82 | if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
||
| 83 | $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
||
| 84 | } |
||
| 85 | $this->editor_style_handle = $editor_style_handle; |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getScriptHandle() |
||
| 93 | { |
||
| 94 | return $this->script_handle; |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $script_handle |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function setScriptHandle($script_handle) |
|
| 102 | { |
||
| 103 | if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
||
| 104 | $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
||
| 105 | } |
||
| 106 | $this->script_handle = $script_handle; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getStyleHandle() |
||
| 114 | { |
||
| 115 | return $this->style_handle; |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $style_handle |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function setStyleHandle($style_handle) |
|
| 123 | { |
||
| 124 | if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
||
| 125 | $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
||
| 126 | } |
||
| 127 | $this->style_handle = $style_handle; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @since $VID:$ |
||
| 132 | * @throws InvalidDataTypeException |
||
| 133 | * @throws InvalidEntityException |
||
| 134 | * @throws DuplicateCollectionIdentifierException |
||
| 135 | */ |
||
| 136 | public function addAssets() |
||
| 137 | { |
||
| 138 | $this->addEditorScript($this->getEditorScriptHandle()); |
||
| 139 | $this->addEditorStyle($this->getEditorStyleHandle()); |
||
| 140 | $this->setScriptHandle($this->getScriptHandle()); |
||
| 141 | $this->setStyleHandle($this->getStyleHandle()); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * @param $handle |
||
| 147 | * @param array $dependencies |
||
| 148 | * @since $VID:$ |
||
| 149 | * @return JavascriptAsset |
||
| 150 | * @throws InvalidDataTypeException |
||
| 151 | * @throws InvalidEntityException |
||
| 152 | * @throws DuplicateCollectionIdentifierException |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function addEditorScript($handle, array $dependencies = array()) |
|
| 155 | { |
||
| 156 | if($this->assets->hasJavascriptAsset($handle)){ |
||
| 157 | return $this->assets->getJavascriptAsset($handle); |
||
| 158 | } |
||
| 159 | return parent::addJavascript( |
||
| 160 | $handle, |
||
| 161 | $this->registry->getJsUrl( |
||
| 162 | $this->domain->assetNamespace(), |
||
| 163 | $handle |
||
| 164 | ), |
||
| 165 | $this->addDefaultBlockScriptDependencies($dependencies) |
||
| 166 | ) |
||
| 167 | ->setRequiresTranslation(); |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $handle |
||
| 173 | * @param array $dependencies |
||
| 174 | * @since $VID:$ |
||
| 175 | * @return StylesheetAsset |
||
| 176 | * @throws InvalidDataTypeException |
||
| 177 | * @throws InvalidEntityException |
||
| 178 | * @throws DuplicateCollectionIdentifierException |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function addEditorStyle($handle, array $dependencies = array()) |
|
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $handle |
||
| 198 | * @param array $dependencies |
||
| 199 | * @since $VID:$ |
||
| 200 | * @return JavascriptAsset |
||
| 201 | * @throws InvalidDataTypeException |
||
| 202 | * @throws InvalidEntityException |
||
| 203 | * @throws DuplicateCollectionIdentifierException |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function addScript($handle, array $dependencies = array()) |
|
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $handle |
||
| 224 | * @param array $dependencies |
||
| 225 | * @since $VID:$ |
||
| 226 | * @return StylesheetAsset |
||
| 227 | * @throws InvalidDataTypeException |
||
| 228 | * @throws InvalidEntityException |
||
| 229 | * @throws DuplicateCollectionIdentifierException |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function addStyle($handle, array $dependencies = array()) |
|
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * @param array $dependencies |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | protected function addDefaultBlockScriptDependencies(array $dependencies) |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @return JavascriptAsset|null |
||
| 266 | */ |
||
| 267 | public function getEditorScript() |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @return StylesheetAsset|null |
||
| 275 | */ |
||
| 276 | public function getEditorStyle() |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @return JavascriptAsset|null |
||
| 284 | */ |
||
| 285 | public function getScript() |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @return StylesheetAsset|null |
||
| 293 | */ |
||
| 294 | public function getStyle() |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public function enqueueAssets() |
||
| 317 | |||
| 318 | } |
||
| 319 |