Completed
Push — develop ( fea319...7a0090 )
by Mohamed
07:15
created

BladeServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 21.78 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.92%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 0
cbo 1
dl 22
loc 101
ccs 47
cts 49
cp 0.9592
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
B boot() 22 84 6

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
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Providers;
13
14
use Illuminate\Support\ServiceProvider;
15
16
/**
17
 * BladeServiceProvider is the blade service provider for extending blade template engine.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class BladeServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * Bootstrap any application services.
25
     */
26 53
    public function boot()
27
    {
28 53
        \Blade::directive(
29 53
            'macro',
30 View Code Duplication
            function ($expression) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31 2
                $pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim';
32 2
                $matches = [];
33 2
                preg_match_all($pattern, $expression, $matches);
34
35 2
                if (!isset($matches[3][0])) {
36
                    throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: macro%s', $expression));
37
                }
38
39 2
                return sprintf("<?php \$___tiny['%s']=function(%s)use(\$__env){ ob_start(); ?>\n", $matches[2][0], $matches[3][0]);
40 53
            }
41
        );
42
43 53
        \Blade::directive(
44 53
            'endmacro',
45
            function () {
46 2
                return "\n<?php return ob_get_clean();} ?>\n";
47 53
            }
48
        );
49
50 53
        \Blade::directive(
51 53
            'usemacro',
52 View Code Duplication
            function ($expression) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53 2
                $pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim';
54 2
                $matches = [];
55 2
                preg_match_all($pattern, $expression, $matches);
56
57 2
                if (!isset($matches[3][0])) {
58
                    throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: usemacro%s', $expression));
59
                }
60
61 2
                return sprintf("<?php echo \$___tiny['%s'](%s); ?>\n", $matches[2][0], $matches[3][0]);
62 53
            }
63
        );
64
65 53
        \Blade::directive(
66 53
            'permission',
67
            function ($expression) {
68 5
                return "<?php if(!Auth::guest() && Auth::user()->permission{$expression}): ?>";
69 53
            }
70
        );
71
72 53
        \Blade::directive(
73 53
            'endpermission',
74
            function () {
75 5
                return '<?php endif; ?>';
76 53
            }
77
        );
78
79 53
        \Blade::directive(
80 53
            'mailattrs',
81
            function ($expression) {
82
                // Get parameters
83 2
                $params = array_map('trim', explode('|', trim($expression, '()')));
84
                // Get style based on the first parameter
85 2
                $style = config('mailcss.' . $params[0]);
86
87 2
                if (!$style) {
88
                    return '';
89
                }
90
91
                // Convert the parameters starting from second into key => value array
92 2
                $override = array_reduce(array_slice($params, 1), function ($override, $param) {
93 2
                    $segments = array_map('trim', explode('=', $param));
94 2
                    $override[$segments[0]] = $segments[1];
95
96 2
                    return $override;
97 2
                }, []);
98
99
                // Style can be callback or an array. Merge and echo.
100 2
                if (is_callable($style)) {
101 2
                    $style = $style($override);
102 1
                } elseif (isset($params[1])) {
103
                    $style = array_merge($style, $override);
104
                }
105
106 2
                return "<?php echo '" . \Html::attributes($style) . "'; ?>";
107 53
            }
108
        );
109 53
    }
110
111
    /**
112
     * Register any application services.
113
     *
114
     * This service provider is a great spot to register your various container
115
     * bindings with the application. As you can see, we are registering our
116
     * "Registrar" implementation here. You can add your own bindings too!
117
     */
118 53
    public function register()
119
    {
120 53
    }
121
}
122