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 |
||
| 7 | abstract class CDNBase { |
||
| 8 | protected $version; |
||
| 9 | protected $provider; |
||
| 10 | protected $data; |
||
| 11 | protected $local; |
||
| 12 | protected $jsUrl; |
||
| 13 | |||
| 14 | public function __construct($version, $provider) { |
||
| 21 | |||
| 22 | public function getJsUrl() { |
||
| 23 | return $this->jsUrl; |
||
| 24 | } |
||
| 25 | |||
| 26 | View Code Duplication | public function setJsUrl($jsUrl, $local=null) { |
|
|
|
|||
| 27 | $this->jsUrl=$jsUrl; |
||
| 28 | if (isset($local)===false) { |
||
| 29 | $local=PhalconUtils::startsWith($jsUrl, "http")===false; |
||
| 30 | } |
||
| 31 | $this->setLocal($local); |
||
| 32 | return $this; |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function getUrlOrCss($element, $key) { |
||
| 36 | if (isset($element)) |
||
| 37 | return $element; |
||
| 38 | $version=$this->version; |
||
| 39 | if (array_search($version, $this->getVersions())===false) |
||
| 40 | $version=$this->getLastVersion(); |
||
| 41 | return $this->replaceVersion($this->data [$this->provider] [$key], $version); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function isLocal() { |
||
| 45 | return $this->local; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function setLocal($local) { |
||
| 49 | $this->local=$local; |
||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function replaceVersion($url, $version) { |
||
| 54 | return str_ireplace("%version%", $version, $url); |
||
| 55 | } |
||
| 56 | |||
| 57 | protected function replaceTheme($url, $theme) { |
||
| 58 | return str_ireplace("%theme%", $theme, $url); |
||
| 59 | } |
||
| 60 | |||
| 61 | protected function replaceVersionAndTheme($url, $version, $theme) { |
||
| 62 | if (isset($theme)) |
||
| 63 | return str_ireplace(array ( |
||
| 64 | "%theme%", |
||
| 65 | "%version%" |
||
| 66 | ), array ( |
||
| 67 | $theme, |
||
| 68 | $version |
||
| 69 | ), $url); |
||
| 70 | else |
||
| 71 | return $this->replaceVersion($url, $version); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getProviders() { |
||
| 75 | return array_keys($this->data); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getVersions($provider=NULL) { |
||
| 79 | if (isset($provider)) |
||
| 80 | return $this->data [$provider] ["versions"]; |
||
| 81 | else |
||
| 82 | return $this->data [$this->provider] ["versions"]; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getLastVersion($provider=NULL) { |
||
| 86 | return $this->getVersions($provider)[0]; |
||
| 87 | } |
||
| 88 | |||
| 89 | abstract public function getUrl(); |
||
| 90 | } |
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.