BladeLoopServiceProvider::addLoopDirectives()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
crap 1
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