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'); |
|
|
|
|
22
|
|
|
$this->convertToLines($result); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function addSuggestionFix($result) |
26
|
|
|
{ |
27
|
|
|
$arr = explode('<br />', nl2br($result)); |
28
|
|
|
$data = []; |
|
|
|
|
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) |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.