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 |
||
20 | class CdnFacade implements CdnFacadeInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $configurations; |
||
26 | |||
27 | /** |
||
28 | * @var \Vinelab\Cdn\Contracts\ProviderFactoryInterface |
||
29 | */ |
||
30 | protected $provider_factory; |
||
31 | |||
32 | /** |
||
33 | * instance of the default provider object. |
||
34 | * |
||
35 | * @var \Vinelab\Cdn\Providers\Contracts\ProviderInterface |
||
36 | */ |
||
37 | protected $provider; |
||
38 | |||
39 | /** |
||
40 | * @var \Vinelab\Cdn\Contracts\CdnHelperInterface |
||
41 | */ |
||
42 | protected $helper; |
||
43 | |||
44 | /** |
||
45 | * @var \Vinelab\Cdn\Validators\CdnFacadeValidator |
||
46 | */ |
||
47 | protected $cdn_facade_validator; |
||
48 | |||
49 | /** |
||
50 | * Calls the provider initializer. |
||
51 | * |
||
52 | * @param \Vinelab\Cdn\Contracts\ProviderFactoryInterface $provider_factory |
||
53 | * @param \Vinelab\Cdn\Contracts\CdnHelperInterface $helper |
||
54 | * @param \Vinelab\Cdn\Validators\CdnFacadeValidator $cdn_facade_validator |
||
55 | */ |
||
56 | public function __construct( |
||
67 | |||
68 | /** |
||
69 | * this function will be called from the 'views' using the |
||
70 | * 'Cdn' facade {{Cdn::asset('')}} to convert the path into |
||
71 | * it's CDN url. |
||
72 | * |
||
73 | * @param $path |
||
74 | * |
||
75 | * @return mixed |
||
76 | * |
||
77 | * @throws Exceptions\EmptyPathException |
||
78 | */ |
||
79 | public function asset($path) |
||
84 | |||
85 | /** |
||
86 | * this function will be called from the 'views' using the |
||
87 | * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into |
||
88 | * it's CDN url. |
||
89 | * |
||
90 | * @param $path |
||
91 | * |
||
92 | * @return mixed |
||
93 | * |
||
94 | * @throws Exceptions\EmptyPathException, \InvalidArgumentException |
||
95 | */ |
||
96 | View Code Duplication | public function elixir($path) |
|
107 | |||
108 | /** |
||
109 | * this function will be called from the 'views' using the |
||
110 | * 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix |
||
111 | * generated file path into it's CDN url. |
||
112 | * |
||
113 | * @param $path |
||
114 | * |
||
115 | * @return mixed |
||
116 | * |
||
117 | * @throws Exceptions\EmptyPathException, \InvalidArgumentException |
||
118 | */ |
||
119 | View Code Duplication | public function mix($path) |
|
130 | |||
131 | /** |
||
132 | * this function will be called from the 'views' using the |
||
133 | * 'Cdn' facade {{Cdn::path('')}} to convert the path into |
||
134 | * it's CDN url. |
||
135 | * |
||
136 | * @param $path |
||
137 | * |
||
138 | * @return mixed |
||
139 | * |
||
140 | * @throws Exceptions\EmptyPathException |
||
141 | */ |
||
142 | public function path($path) |
||
146 | |||
147 | /** |
||
148 | * check if package is surpassed or not then |
||
149 | * prepare the path before generating the url. |
||
150 | * |
||
151 | * @param $path |
||
152 | * @param string $prepend |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | private function generateUrl($path, $prepend = '') |
||
182 | |||
183 | /** |
||
184 | * Read the configuration file and pass it to the provider factory |
||
185 | * to return an object of the default provider specified in the |
||
186 | * config file. |
||
187 | */ |
||
188 | private function init() |
||
196 | } |
||
197 |
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.