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 |
||
| 17 | abstract class BrowserAsset extends Asset |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array $attributes |
||
| 22 | */ |
||
| 23 | private $attributes = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array $allowed_attributes |
||
| 27 | */ |
||
| 28 | private static $allowed_attributes = [ |
||
| 29 | Asset::TYPE_CSS => [ |
||
| 30 | 'crossorigin', |
||
| 31 | 'media', |
||
| 32 | 'referrerpolicy', |
||
| 33 | 'sizes', |
||
| 34 | 'type', |
||
| 35 | ], |
||
| 36 | Asset::TYPE_JS => [ |
||
| 37 | 'async', |
||
| 38 | 'charset', |
||
| 39 | 'crossorigin', |
||
| 40 | 'defer', |
||
| 41 | 'type', |
||
| 42 | ] |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array $dependencies |
||
| 47 | */ |
||
| 48 | private $dependencies; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string $source |
||
| 52 | */ |
||
| 53 | private $source; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string $version |
||
| 57 | */ |
||
| 58 | private $version; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Asset constructor. |
||
| 63 | * |
||
| 64 | * @param string $type |
||
| 65 | * @param string $handle |
||
| 66 | * @param string $source |
||
| 67 | * @param array $dependencies |
||
| 68 | * @param DomainInterface $domain |
||
| 69 | * @param string $version |
||
| 70 | * @throws DomainException |
||
| 71 | * @throws InvalidDataTypeException |
||
| 72 | */ |
||
| 73 | public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain, $version = '') |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @since 4.9.62.p |
||
| 84 | */ |
||
| 85 | abstract public function enqueueAsset(); |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getAttributes() |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @param array $attributes |
||
| 99 | * @throws DomainException |
||
| 100 | * @since $VID:$ |
||
| 101 | */ |
||
| 102 | public function addAttributes(array $attributes) |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $attribute |
||
| 121 | * @return bool |
||
| 122 | * @throws DomainException |
||
| 123 | * @since $VID:$ |
||
| 124 | */ |
||
| 125 | private function validateAttribute($attribute) |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function dependencies() |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $dependencies |
||
| 151 | */ |
||
| 152 | protected function setDependencies(array $dependencies) |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @since 4.9.62.p |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function hasDependencies() |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function source() |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $source |
||
| 179 | * @throws InvalidDataTypeException |
||
| 180 | */ |
||
| 181 | private function setSource($source) |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | * @throws InvalidDataTypeException |
||
| 197 | * @throws DomainException |
||
| 198 | */ |
||
| 199 | public function version() |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $version |
||
| 207 | * @param bool $fluent |
||
| 208 | * @return BrowserAsset|null |
||
| 209 | * @throws DomainException |
||
| 210 | * @throws InvalidDataTypeException |
||
| 211 | */ |
||
| 212 | public function setVersion($version, $fluent = true) |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function isBuiltDistributionSource() { |
||
| 241 | } |
||
| 242 |