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