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 |
||
9 | class BladeLoopServiceProvider extends ServiceProvider |
||
10 | { |
||
11 | /** |
||
12 | * Register the service provider. |
||
13 | * |
||
14 | * @return void |
||
15 | */ |
||
16 | 10 | public function register() |
|
17 | { |
||
18 | 10 | $this->app->singleton('blade.loop', LoopFactory::class); |
|
19 | |||
20 | 5 | $this->app->extend('blade.compiler', function (BladeCompiler $blade, Application $app) { |
|
21 | 15 | $blade = $this->addLoopDirectives($blade); |
|
22 | |||
23 | 15 | if (version_compare($app->version(), '5.2.21', '<')) { |
|
24 | 5 | $blade = $this->addLoopControlDirectives($blade); |
|
25 | 5 | } |
|
26 | |||
27 | 15 | return $blade; |
|
28 | 15 | }); |
|
29 | 15 | } |
|
30 | |||
31 | /** |
||
32 | * Extend blade by loop directives. |
||
33 | * |
||
34 | * @param BladeCompiler $blade |
||
35 | * |
||
36 | * @return BladeCompiler |
||
37 | */ |
||
38 | 10 | View Code Duplication | private function addLoopDirectives(BladeCompiler $blade) |
65 | |||
66 | /** |
||
67 | * Extend blade by continue and break directives. |
||
68 | * |
||
69 | * @param BladeCompiler $blade |
||
70 | * |
||
71 | * @return BladeCompiler |
||
72 | */ |
||
73 | View Code Duplication | private function addLoopControlDirectives(BladeCompiler $blade) |
|
91 | } |
||
92 |
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.