DeleteRelationAction   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 48.94 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 46
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 2
A removeLeading() 19 19 3
A removeTrailing() 19 19 3
A endOfMethod() 0 4 1
A startOfMethod() 8 8 3

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
namespace Mtolhuys\LaravelSchematics\Actions\Relation;
4
5
class DeleteRelationAction
6
{
7
    /**
8
     * @param $request
9
     * @return false|int
10
     */
11
    public function execute($request)
12
    {
13
        $file = $request['method']['file'];
14
        $lines = file($file, FILE_IGNORE_NEW_LINES);
15
        $index = $request['method']['line'] - 1;
16
17
        $removeLines = $this->removeLeading($lines, $index - 1);
18
        $removeLines = array_merge($removeLines, $this->removeTrailing($lines, $index));
19
20
        foreach ($removeLines as $removeLine) {
21
            unset($lines[$removeLine]);
22
        }
23
24
        return file_put_contents($file, implode("\n", $lines));
25
    }
26
27
    /**
28
     * @param $lines
29
     * @param $index
30
     * @return array
31
     */
32 View Code Duplication
    private function removeLeading($lines, $index): array
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...
33
    {
34
        $line = '';
35
        $remove = [];
36
37
        while(! $this->startOfMethod($line)) {
38
            $line = $lines[$index];
39
40
            $remove[] = $index;
41
42
            $index--;
43
        }
44
45
        if (trim($lines[$index]) === '') {
46
            $remove[] = $index;
47
        }
48
49
        return $remove;
50
    }
51
52
    /**
53
     * @param $lines
54
     * @param $index
55
     * @return array
56
     */
57 View Code Duplication
    private function removeTrailing($lines, $index): array
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...
58
    {
59
        $line = '';
60
        $remove = [];
61
62
        while(! $this->endOfMethod($line)) {
63
            $line = $lines[$index];
64
65
            $remove[] = $index;
66
67
            $index++;
68
        }
69
70
        if (trim($lines[$index]) === '') {
71
            $remove[] = $index;
72
        }
73
74
        return $remove;
75
    }
76
77
    /**
78
     * @param $line
79
     * @return bool
80
     */
81
    private function endOfMethod($line): bool
82
    {
83
        return str_replace(' ', '', $line) === '}';
84
    }
85
86
    /**
87
     * @param $line
88
     * @return bool
89
     */
90 View Code Duplication
    private function startOfMethod($line): bool
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...
91
    {
92
        $line = str_replace(' ', '', $line);
93
94
        return $line === '/**'
95
            || $line === '//'
96
            || $line === '}';
97
    }
98
}
99