Completed
Push — master ( d6febc...001063 )
by recca
01:35
created

replaceServieProviderCallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
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 2
    public function process()
11
    {
12 2
        $path = Arr::get($this->config, 'path');
13
14 2
        if (empty($path) === true) {
15
            return;
16
        }
17
18 2
        $content = $this->files->get($path);
19
20 2
        if (strpos($content, '$this->registerRepositories') === false) {
21 2
            $content = preg_replace_callback('/public function register\(.+\n\s+{/', function ($m) {
22 2
                return $m[0]."\n".
23 2
                    str_repeat(' ', 8).
24 2
                    '$this->registerRepositories();';
25 2
            }, $content);
26
        }
27
28 2
        if (strpos($content, 'protected function registerRepositories()') === false) {
29 2
            $content = substr($content, 0, strrpos($content, '}')).
30 2
                "\n".str_repeat(' ', 4).
31 2
                'protected function registerRepositories()'.
32 2
                "\n".str_repeat(' ', 4).'{'.
33 2
                "\n".str_repeat(' ', 4).'}'.
34 2
                "\n}\n";
35
        }
36
37 2
        $qualifiedName = $this->attributes['qualified_name'];
38 2
        $class = $this->attributes['class'];
39
40 2 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 2
            $content = preg_replace_callback(
42 2
                '/namespace.+/',
43 2
                [$this, 'replaceServieProviderCallback'],
44 2
                $content
45
            );
46
        }
47
48 2 View Code Duplication
        if ($class && strpos($content, sprintf('$this->app->singleton(%sContract::class, %s::class);', $class, $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 2
            $content = preg_replace_callback(
50 2
                '/protected function registerRepositories.+\n\s+{/',
51 2
                [$this, 'replaceServieProviderCallback'],
52 2
                $content
53
            );
54
        }
55
56 2
        $this->files->put($path, $this->useSortFixer->fix($content));
0 ignored issues
show
Security Bug introduced by
It seems like $this->useSortFixer->fix($content) targeting Recca0120\Generator\Fixers\UseSortFixer::fix() can also be of type false; however, Illuminate\Filesystem\Filesystem::put() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
57 2
    }
58
59
    /**
60
     * replaceServieProviderCallback.
61
     *
62
     * @param array $match
63
     * @return string
64
     */
65 2
    private function replaceServieProviderCallback($match)
66
    {
67 2
        $qualifiedName = $this->attributes['qualified_name'];
68 2
        $class = $this->attributes['class'];
69 2
        $contractQualifiedName = $this->attributes['repository_contract_qualified_name'];
70
71 2
        if (Str::startsWith($match[0], 'namespace') === true) {
72 2
            return $match[0]."\n\n".
73 2
                sprintf("use %s as %sContract;\n", $contractQualifiedName, $class).
74 2
                sprintf("use %s;\n", $qualifiedName);
75
        }
76
77 2
        return $match[0]."\n".
78 2
            str_repeat(' ', 8).
79 2
            sprintf('$this->app->singleton(%sContract::class, %s::class);', $class, $class);
80
    }
81
}
82