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 |
||
12 | class PwaController extends Controller |
||
13 | { |
||
14 | /** |
||
15 | * Display a listing of the resource. |
||
16 | * |
||
17 | * @return \Illuminate\Http\Response |
||
18 | */ |
||
19 | public function index() |
||
25 | |||
26 | /** |
||
27 | * Store a newly created resource in storage. |
||
28 | * |
||
29 | * @param \Illuminate\Http\Request $request |
||
30 | * |
||
31 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
||
32 | */ |
||
33 | public function store(Request $request) |
||
76 | |||
77 | /** |
||
78 | * Activate PWA for the current domain. |
||
79 | * |
||
80 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
||
81 | */ |
||
82 | View Code Duplication | public function activate() |
|
90 | |||
91 | /** |
||
92 | * Deactivate PWA for the current domain. |
||
93 | * |
||
94 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
||
95 | */ |
||
96 | View Code Duplication | public function deactivate() |
|
104 | |||
105 | /** |
||
106 | * Update the specified resource in storage. |
||
107 | * |
||
108 | * @param \Illuminate\Http\Request $request |
||
109 | * @param \CodexShaper\PWA\Model\Setting $Setting |
||
110 | * |
||
111 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
||
112 | */ |
||
113 | public function update(Request $request, Setting $Setting) |
||
168 | |||
169 | /** |
||
170 | * Remove the specified resource from storage. |
||
171 | * |
||
172 | * @param \CodexShaper\PWA\Model\Setting $Setting |
||
173 | * |
||
174 | * @throws \Exception |
||
175 | * |
||
176 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
||
177 | */ |
||
178 | public function destroy(Setting $Setting) |
||
196 | |||
197 | /** |
||
198 | * Return manifest.josn content. |
||
199 | * |
||
200 | * @return \Illuminate\Http\Response |
||
201 | */ |
||
202 | public function manifest() |
||
210 | |||
211 | /** |
||
212 | * Display pwa offline resources. |
||
213 | * |
||
214 | * @return \Illuminate\Http\Response |
||
215 | */ |
||
216 | public function offline() |
||
220 | |||
221 | /** |
||
222 | * Prepare manifest data. |
||
223 | * |
||
224 | * @param array $manifest |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public static function prepareManifest($manifest) |
||
280 | |||
281 | /** |
||
282 | * Prepare manifest data from request for database. |
||
283 | * |
||
284 | * @param array $data |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | protected function getManifestData($data) |
||
352 | |||
353 | /** |
||
354 | * Return serviceworker.js content. |
||
355 | * |
||
356 | * @return \Illuminate\Http\Response |
||
357 | */ |
||
358 | View Code Duplication | public function serviceWorker() |
|
372 | |||
373 | /** |
||
374 | * Return serviceworker register content. |
||
375 | * |
||
376 | * @return \Illuminate\Http\Response |
||
377 | */ |
||
378 | View Code Duplication | public function serviceWorkerRegisterContent() |
|
392 | |||
393 | /** |
||
394 | * Generate service worker. |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | protected function generateServiceWorker() |
||
399 | { |
||
400 | $public_path = asset('/'); |
||
401 | $pwa_asset = pwa_asset(''); |
||
402 | $base_url = url('/'); |
||
403 | |||
404 | return <<<SERVICE_WORKER |
||
405 | var staticCacheName = "pwa-v" + new Date().getTime(); |
||
406 | var filesToCache = [ |
||
407 | '$base_url/offline', |
||
408 | '{$base_url}/css/app.css', |
||
409 | '{$base_url}/js/app.js', |
||
410 | '$pwa_asset/images/icons/icon-72x72.png', |
||
411 | '$pwa_asset/images/icons/icon-96x96.png', |
||
412 | '$pwa_asset/images/icons/icon-128x128.png', |
||
413 | '$pwa_asset/images/icons/icon-144x144.png', |
||
414 | '$pwa_asset/images/icons/icon-152x152.png', |
||
415 | '$pwa_asset/images/icons/icon-192x192.png', |
||
416 | '$pwa_asset/images/icons/icon-384x384.png', |
||
417 | '$pwa_asset/images/icons/icon-512x512.png', |
||
418 | ]; |
||
419 | |||
420 | // Cache on install |
||
421 | self.addEventListener("install", event => { |
||
422 | this.skipWaiting(); |
||
423 | event.waitUntil( |
||
424 | caches.open(staticCacheName) |
||
425 | .then(cache => { |
||
426 | return cache.addAll(filesToCache); |
||
427 | }) |
||
428 | ) |
||
429 | }); |
||
430 | |||
431 | // Clear cache on activate |
||
432 | self.addEventListener('activate', event => { |
||
433 | event.waitUntil( |
||
434 | caches.keys().then(cacheNames => { |
||
435 | return Promise.all( |
||
436 | cacheNames |
||
437 | .filter(cacheName => (cacheName.startsWith("pwa-"))) |
||
438 | .filter(cacheName => (cacheName !== staticCacheName)) |
||
439 | .map(cacheName => caches.delete(cacheName)) |
||
440 | ); |
||
441 | }) |
||
442 | ); |
||
443 | }); |
||
444 | |||
445 | // Serve from Cache |
||
446 | self.addEventListener("fetch", event => { |
||
447 | event.respondWith( |
||
448 | caches.match(event.request) |
||
449 | .then(response => { |
||
450 | return response || fetch(event.request); |
||
451 | }) |
||
452 | .catch(() => { |
||
453 | return caches.match('offline'); |
||
454 | }) |
||
455 | ) |
||
456 | }); |
||
457 | SERVICE_WORKER; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Register service worker. |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | protected function generateServiceWorkerRegister() |
||
466 | { |
||
467 | $serviceworker_route = route('pwa.serviceworker'); |
||
468 | $scope = config('pwa.scope', '.'); |
||
469 | |||
470 | return <<<REGISTER_SERVICE_WORKER |
||
471 | // Get serviceworker contents |
||
472 | var serviceworker = "$serviceworker_route"; |
||
473 | // Initialize the service worker |
||
474 | if ('serviceWorker' in navigator) { |
||
475 | navigator.serviceWorker.register(serviceworker, { |
||
476 | scope: '$scope' |
||
477 | }).then(function (registration) { |
||
478 | // Registration was successful |
||
479 | console.log('Laravel PWA enable successfully. Enjoy it!'); |
||
480 | }, function (err) { |
||
481 | // registration failed |
||
482 | console.log('Laravel PWA registration failed. Please check the error: ', err); |
||
483 | }); |
||
484 | } |
||
485 | REGISTER_SERVICE_WORKER; |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Get Setting instance. |
||
490 | * |
||
491 | * @return \CodexShaper\PWA\Model\Setting |
||
492 | */ |
||
493 | public function getPwaInstance() |
||
497 | |||
498 | /** |
||
499 | * Return storage asset. |
||
500 | * |
||
501 | * @return \Illuminate\Http\Response |
||
502 | */ |
||
503 | public function asset($path) |
||
511 | } |
||
512 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.