Phpcs::convertToLines()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 9
nop 1
1
<?php
2
3
namespace GitScrum\Classes;
4
5
use Storage;
6
use GitScrum\CommitFile;
7
use GitScrum\CommitFilePhpcs;
8
9
class Phpcs
10
{
11
    private $id;
12
13
    public function init($fileContents, $commitFileId)
14
    {
15
        $this->id = $commitFileId;
16
        $filename = uniqid(strtotime('now'), true);
17
        Storage::disk('local_tmp')->put($filename.'.php', $fileContents);
18
        $result = shell_exec('phpcs --report=diff '.storage_path('tmp').DIRECTORY_SEPARATOR.$filename.'.php 2>&1; echo $?');
19
        $this->addSuggestionFix($result);
20
        $result = shell_exec('phpcs '.storage_path('tmp').DIRECTORY_SEPARATOR.$filename.'.php 2>&1; echo $?');
21
        Storage::delete(storage_path('tmp').DIRECTORY_SEPARATOR.$filename.'.php');
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
22
        $this->convertToLines($result);
23
    }
24
25
    private function addSuggestionFix($result)
26
    {
27
        $arr = explode('<br />', nl2br($result));
28
        $data = [];
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
        $rows = array_slice($arr, 2, (count($arr) - 5));
30
        $row = str_replace('<br />', '', implode('<br />', $rows));
31
        $commit = CommitFile::find($this->id)->first();
32
        $commit->phpcs = $row;
33
        $commit->save();
34
    }
35
36
    private function addPhpcs($data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
37
    {
38
        $commitReturn = $commit = CommitFilePhpcs::where('commit_file_id', '=', $data['commit_file_id'])
39
            ->where('line', '=', $data['line'])
40
            ->first();
41
        if ($commit === null) {
42
            return $commit = CommitFilePhpcs::create($data);
0 ignored issues
show
Unused Code introduced by
$commit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        } else {
44
            $commit->update($data);
45
46
            return $commitReturn;
47
        }
48
    }
49
50
    private function convertToLines($result)
51
    {
52
        $arr = explode('<br />', nl2br($result));
53
        $data = [];
54
        $row = '';
55
        foreach ($arr as $value) {
56
            $cols = explode('|', $value);
57
            foreach ($cols as $col) {
58
                $row .= trim(str_replace(['[ ]', '[x]'], '', $col)).'|';
59
            }
60
        }
61
        $rows = explode('|', str_replace('|||', ' ', $row));
62
        $rows = array_slice($rows, 5, (count($rows) - 14));
63
        $i = 0;
64
        $columns = ['line', 'type', 'message'];
65
        foreach ($rows as $row) {
66
            $data[$columns[$i]] = $row;
67
            if ($i == 2) {
68
                $data['commit_file_id'] = $this->id;
69
                $this->addPhpcs($data);
70
                $i = 0;
71
            } else {
72
                ++$i;
73
            }
74
        }
75
    }
76
}
77