Completed
Push — master ( b44515...9a1ee0 )
by Sullivan
02:01
created

Linter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C validate() 0 26 13
B packagesAreSorted() 0 22 4
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
        );
23
24
        $this->config = array_merge($defaultConfig, $config);
25
    }
26
27
    /**
28
     * @param array $manifest composer.json file manifest
29
     *
30
     * @return string[]
31
     */
32
    public function validate($manifest)
33
    {
34
        $errors = array();
35
36
        if (isset($manifest['config']['sort-packages']) && $manifest['config']['sort-packages']) {
37
            foreach (array('require', 'require-dev', 'conflict', 'replace', 'provide', 'suggest') as $linksSection) {
38
                if (array_key_exists($linksSection, $manifest) && !$this->packagesAreSorted($manifest[$linksSection])) {
39
                    array_push($errors, 'Links under '.$linksSection.' section are not sorted.');
40
                }
41
            }
42
        }
43
44
        if (true === $this->config['php'] &&
45
            (array_key_exists('require-dev', $manifest) || array_key_exists('require', $manifest))) {
46
            $isOnRequireDev = array_key_exists('require-dev', $manifest) && array_key_exists('php', $manifest['require-dev']);
47
            $isOnRequire = array_key_exists('require', $manifest) && array_key_exists('php', $manifest['require']);
48
49
            if ($isOnRequireDev) {
50
                array_push($errors, 'PHP requirement should be in the require section, not in the require-dev section.');
51
            } elseif (!$isOnRequire) {
52
                array_push($errors, 'You must specifiy the PHP requirement.');
53
            }
54
        }
55
56
        return $errors;
57
    }
58
59
    private function packagesAreSorted(array $packages)
60
    {
61
        $names = array_keys($packages);
62
63
        $hasPHP = in_array('php', $names);
64
        $extNames = array_filter($names, function ($name) {
65
            return 'ext-' === substr($name, 0, 4) && !strstr($name, '/');
66
        });
67
        sort($extNames);
68
        $vendorName = array_filter($names, function ($name) {
69
            return 'ext-' !== substr($name, 0, 4) && 'php' !== $name;
70
        });
71
        sort($vendorName);
72
73
        $sortedNames = array_merge(
74
            $hasPHP ? array('php') : array(),
75
            $extNames,
76
            $vendorName
77
        );
78
79
        return $sortedNames === $names;
80
    }
81
}
82