ServiceProviderRegister::process()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 53

Duplication

Lines 14
Ratio 26.42 %

Code Coverage

Tests 39
CRAP Score 9.0012

Importance

Changes 0
Metric Value
dl 14
loc 53
ccs 39
cts 40
cp 0.975
rs 7.4698
c 0
b 0
f 0
cc 9
nc 17
nop 0
crap 9.0012

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Recca0120\Generator\Plugins;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
class ServiceProviderRegister extends Plugin
9
{
10 3
    public function process()
11
    {
12 3
        $path = Arr::get($this->config, 'path');
13
14 3
        if (empty($path) === true) {
15
            return;
16
        }
17
18 3
        $content = $this->files->get($path);
19
20 3
        if (strpos($content, '$this->registerRepositories') === false) {
21 3
            $content = preg_replace_callback('/public function register\(.+\n\s+{/', function ($m) {
22 3
                return $m[0]."\n".
23 3
                    str_repeat(' ', 8).
24 3
                    '$this->registerRepositories();';
25 3
            }, $content);
26 3
        }
27
28 3
        if (strpos($content, 'private function registerRepositories()') === false) {
29 3
            $content = substr($content, 0, strrpos($content, '}')).
30 3
                "\n".str_repeat(' ', 4).
31 3
                'private function registerRepositories()'.
32 3
                "\n".str_repeat(' ', 4).'{'.
33 3
                "\n".str_repeat(' ', 4).'}'.
34 3
                "\n}\n";
35 3
        }
36
37 3
        $qualifiedName = $this->attributes['qualified_class'];
38 3
        $class = $this->attributes['class'];
39
40 3 View Code Duplication
        if ($qualifiedName && strpos($content, sprintf('use %s;', $qualifiedName)) === 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...
41 3
            $content = preg_replace_callback(
42 3
                '/namespace.+/',
43 3
                [$this, 'replaceServieProviderCallback'],
44
                $content
45 3
            );
46 3
        }
47
48 3 View Code Duplication
        if ($class && strpos($content, $this->singletonString($class)) === 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...
49 3
            $content = preg_replace_callback(
50 3
                '/protected function registerRepositories.+\n\s+{/',
51 3
                [$this, 'replaceServieProviderCallback'],
52
                $content
53 3
            );
54 3
        }
55
56 3
        $fixedContent = $this->useSortFixer->fix($content);
57
58 3
        $this->files->put(
59 3
            $path,
60 3
            $fixedContent ? $fixedContent : $content
61 3
        );
62 3
    }
63
64
    /**
65
     * replaceServieProviderCallback.
66
     *
67
     * @param array $match
68
     * @return string
69
     */
70 3
    private function replaceServieProviderCallback($match)
71
    {
72 3
        $qualifiedName = $this->attributes['qualified_class'];
73 3
        $class = $this->attributes['class'];
74 3
        $contractQualifiedName = $this->attributes['repository_contract_qualified_class'];
75
76 3
        if (Str::startsWith($match[0], 'namespace') === true) {
77 3
            return $match[0]."\n\n".
78 3
                sprintf("use %s as %sContract;\n", $contractQualifiedName, $class).
79 3
                sprintf("use %s;\n", $qualifiedName);
80
        }
81
82
        return $match[0]."\n".str_repeat(' ', 8).$this->singletonString($class);
83
    }
84
85 3
    private function singletonString($class)
86
    {
87 3
        return sprintf('$this->app->singleton(%sContract::class, %s::class);', $class, $class);
88
    }
89
}
90