1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ModuleRatings\Check; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ModuleRatings\Check; |
7
|
|
|
|
8
|
|
|
class CodingStandardCheck extends Check |
9
|
|
|
{ |
10
|
|
|
protected $points = 10; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The path to the PHPCS standards file |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $standardsFile = ''; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->setStandardsFile(realpath(__DIR__) . '/CodingStandardCheck/phpcs.xml.dist'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getKey() |
25
|
|
|
{ |
26
|
|
|
return 'coding_standards'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getDescription() |
30
|
|
|
{ |
31
|
|
|
return 'The PHP code in this module passes the SilverStripe lint rules (mostly PSR-2)'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get PHP CodeSniffer and run it over the current module. Assigns a successful result if the codebase passes |
36
|
|
|
* the linting check with no errors. |
37
|
|
|
*/ |
38
|
|
|
public function run() |
39
|
|
|
{ |
40
|
|
|
$standard = '--standard=' . escapeshellarg($this->getStandardsFile()); |
41
|
|
|
$path = $this->getSuite()->getModuleRoot(); |
42
|
|
|
|
43
|
|
|
$output = null; |
|
|
|
|
44
|
|
|
exec( |
45
|
|
|
'cd ' . $this->getProjectRoot() . ' && vendor/bin/phpcs -q ' |
46
|
|
|
. $standard . ' ' . $path, |
47
|
|
|
$ouput, |
48
|
|
|
$exitCode |
49
|
|
|
); |
50
|
|
|
if ($exitCode == 0) { |
51
|
|
|
$this->setSuccessful(true); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setStandardsFile($standardsFile) |
56
|
|
|
{ |
57
|
|
|
$this->standardsFile = $standardsFile; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getStandardsFile() |
62
|
|
|
{ |
63
|
|
|
return $this->standardsFile; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getProjectRoot() |
67
|
|
|
{ |
68
|
|
|
$base = dirname(__FILE__); |
69
|
|
|
if (file_exists($base . '/../../../../../vendor/autoload.php')) { |
70
|
|
|
// Installed in the vendor folder |
71
|
|
|
return $base . '/../../../../../'; |
72
|
|
|
} elseif (file_exists($base . '/../../vendor/autoload.php')) { |
73
|
|
|
// Installed as the project |
74
|
|
|
return $base . '/../../'; |
75
|
|
|
} |
76
|
|
|
throw new Exception('Could not find the project root folder!'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|