Completed
Pull Request — master (#4)
by Neo
03:24 queued 52s
created

LodashServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.09%

Importance

Changes 0
Metric Value
dl 20
loc 84
ccs 35
cts 46
cp 0.7609
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A register() 0 10 1
A registerCommands() 0 12 3
A registerBladeDirectives() 0 21 3
A registerRequestMacros() 20 28 5

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
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash;
13
14
use Blade;
15
use Illuminate\Http\Request;
16
use Illuminate\Support\ServiceProvider;
17
18
class LodashServiceProvider extends ServiceProvider
19
{
20 11
    public function boot(): void
21
    {
22 11
        $this->publishes([
23 11
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
24
        ]);
25 11
    }
26
27 11
    public function register(): void
28
    {
29 11
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'lodash');
30
31 11
        $this->registerCommands();
32
33 11
        $this->registerRequestMacros();
34
35 11
        $this->registerBladeDirectives();
36 11
    }
37
38 11
    protected function registerCommands(): void
39
    {
40 11
        if (empty($commands = (array) config('lodash.available_commands'))) {
41
            return;
42
        }
43
44 11
        foreach ($commands as $name => $class) {
45 11
            $this->app->singleton($name, $class);
46
        }
47
48 11
        $this->commands(array_keys($commands));
49 11
    }
50
51 11
    protected function registerBladeDirectives(): void
52
    {
53 11
        $register_directives = config('lodash.register_blade_directives');
54
55 11
        if ($register_directives['datetime'] ?? false) {
56
            Blade::directive('datetime', function ($expression) {
57
                return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
58
                    . '\' title=\'' . $expression . '\'>'
59
                    . with({$expression})->diffForHumans() . '</time>' ?>";
60 11
            });
61
        }
62
63 11
        if ($register_directives['plural'] ?? false) {
64
            Blade::directive('plural', function ($expression) {
65
                $expression = trim($expression, '()');
66
                list($count, $str, $spacer) = array_pad(preg_split('/,\s*/', $expression), 3, "' '");
67
    
68
                return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
69 11
            });
70
        }
71 11
    }
72
73 11
    protected function registerRequestMacros(): void
74
    {
75 11
        $register_request_macros = config('lodash.register_request_macros');
76
77 11 View Code Duplication
        if ($register_request_macros['getInt'] ?? false) {
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...
78
            Request::macro('getInt', function (string $name, int $default = 0): int {
79
                return (int) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80 11
            });
81
        }
82
83 11 View Code Duplication
        if ($register_request_macros['getBool'] ?? false) {
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...
84
            Request::macro('getBool', function (string $name, bool $default = false): bool {
85
                return (bool) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86 11
            });
87
        }
88
89 11 View Code Duplication
        if ($register_request_macros['getFloat'] ?? false) {
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...
90
            Request::macro('getFloat', function (string $name, float $default = 0): float {
91
                return (float) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92 11
            });
93
        }
94
95 11 View Code Duplication
        if ($register_request_macros['getString'] ?? false) {
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...
96 11
            Request::macro('getString', function (string $name, string $default = ''): string {
97
                return (string) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98 11
            });
99
        }
100 11
    }
101
}
102