1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SLLH\ComposerLint; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Sullivan Senechal <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
final class Linter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $config |
17
|
|
|
*/ |
18
|
|
|
public function __construct(array $config) |
19
|
|
|
{ |
20
|
|
|
$defaultConfig = array( |
21
|
|
|
'php' => true, |
22
|
|
|
'minimum-stability' => true, |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$this->config = array_merge($defaultConfig, $config); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $manifest composer.json file manifest |
30
|
|
|
* |
31
|
|
|
* @return string[] |
32
|
|
|
*/ |
33
|
|
|
public function validate($manifest) |
34
|
|
|
{ |
35
|
|
|
$errors = array(); |
36
|
|
|
|
37
|
|
|
if (isset($manifest['config']['sort-packages']) && $manifest['config']['sort-packages']) { |
38
|
|
|
foreach (array('require', 'require-dev', 'conflict', 'replace', 'provide', 'suggest') as $linksSection) { |
39
|
|
|
if (array_key_exists($linksSection, $manifest) && !$this->packagesAreSorted($manifest[$linksSection])) { |
40
|
|
|
array_push($errors, 'Links under '.$linksSection.' section are not sorted.'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (true === $this->config['php'] && |
46
|
|
|
(array_key_exists('require-dev', $manifest) || array_key_exists('require', $manifest))) { |
47
|
|
|
$isOnRequireDev = array_key_exists('require-dev', $manifest) && array_key_exists('php', $manifest['require-dev']); |
48
|
|
|
$isOnRequire = array_key_exists('require', $manifest) && array_key_exists('php', $manifest['require']); |
49
|
|
|
|
50
|
|
|
if ($isOnRequireDev) { |
51
|
|
|
array_push($errors, 'PHP requirement should be in the require section, not in the require-dev section.'); |
52
|
|
|
} elseif (!$isOnRequire) { |
53
|
|
|
array_push($errors, 'You must specifiy the PHP requirement.'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (true === $this->config['minimum-stability'] && array_key_exists('minimum-stability', $manifest) && |
58
|
|
|
array_key_exists('type', $manifest) && 'project' !== $manifest['type']) { |
59
|
|
|
array_push($errors, 'The minimum-stability should be only used for project packages.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $errors; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function packagesAreSorted(array $packages) |
66
|
|
|
{ |
67
|
|
|
$names = array_keys($packages); |
68
|
|
|
|
69
|
|
|
$hasPHP = in_array('php', $names); |
70
|
|
|
$extNames = array_filter($names, function ($name) { |
71
|
|
|
return 'ext-' === substr($name, 0, 4) && !strstr($name, '/'); |
72
|
|
|
}); |
73
|
|
|
sort($extNames); |
74
|
|
|
$vendorName = array_filter($names, function ($name) { |
75
|
|
|
return 'ext-' !== substr($name, 0, 4) && 'php' !== $name; |
76
|
|
|
}); |
77
|
|
|
sort($vendorName); |
78
|
|
|
|
79
|
|
|
$sortedNames = array_merge( |
80
|
|
|
$hasPHP ? array('php') : array(), |
81
|
|
|
$extNames, |
82
|
|
|
$vendorName |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $sortedNames === $names; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|