DeleteRelationAction::startOfMethod()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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