BladeLoopServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 54.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 45
loc 83
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 14 2
B addLoopDirectives() 27 27 1
A addLoopControlDirectives() 18 18 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Advmaker\BladeLoop;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\View\Compilers\BladeCompiler;
8
9
class BladeLoopServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the service provider.
13
     *
14
     * @return void
15
     */
16 30
    public function register()
17
    {
18 30
        $this->app->singleton('blade.loop', LoopFactory::class);
19
20
        $this->app->extend('blade.compiler', function (BladeCompiler $blade, Application $app) {
21 21
            $blade = $this->addLoopDirectives($blade);
22
23 21
            if (version_compare($app->version(), '5.2.21', '<')) {
24 7
                $blade = $this->addLoopControlDirectives($blade);
25 7
            }
26
27 21
            return $blade;
28 30
        });
29 30
    }
30
31
    /**
32
     * Extend blade by loop directives.
33
     *
34
     * @param  BladeCompiler $blade
35
     *
36
     * @return BladeCompiler
37
     */
38 21 View Code Duplication
    private function addLoopDirectives(BladeCompiler $blade)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
39
    {
40
        $blade->extend(function ($value) {
41 21
            $pattern = '/(?<!\\w)(\\s*)@loop(?:\\s*)\\((.*)(?:\\sas\\s)([^)]*)\\)/';
42
            $replacement = <<<'EOT'
43
$1<?php
44
$loop = app('blade.loop')->newLoop($2);
45
foreach($loop->getItems() as $3):
46
    $loop = app('blade.loop')->loop();
47
?>
48 21
EOT;
49 21
            return preg_replace($pattern, $replacement, $value);
50 21
        });
51
52
        $blade->extend(function ($value) {
53 21
            $pattern = '/(?<!\\w)(\\s*)@endloop(\\s*)/';
54
            $replacement = <<<'EOT'
55
$1<?php
56
endforeach;
57
app('blade.loop')->endLoop($loop);
58
?>$2
59 21
EOT;
60 21
            return preg_replace($pattern, $replacement, $value);
61 21
        });
62
63 21
        return $blade;
64
    }
65
66
    /**
67
     * Extend blade by continue and break directives.
68
     *
69
     * @param  BladeCompiler $blade
70
     *
71
     * @return BladeCompiler
72
     */
73 7 View Code Duplication
    private function addLoopControlDirectives(BladeCompiler $blade)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        $blade->extend(function ($value) {
76 7
            $pattern = '/(?<!\\w)(\\s*)@continue\\s*\\(([^)]*)\\)/';
77 7
            $replacement = '$1<?php if ($2) { continue; } ?>';
78
79 7
            return preg_replace($pattern, $replacement, $value);
80 7
        });
81
82 7
        $blade->extend(function ($value) {
83 7
            $pattern = '/(?<!\\w)(\\s*)@break\\s*\\(([^)]*)\\)/';
84 7
            $replacement = '$1<?php if ($2) { break; } ?>';
85
86 7
            return preg_replace($pattern, $replacement, $value);
87 7
        });
88
89 7
        return $blade;
90
    }
91
}
92