Completed
Push — master ( 975f5b...e3d381 )
by Sullivan
02:05
created

Linter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 14 6
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
     * @param array $manifest composer.json file manifest
12
     *
13
     * @return string[]
14
     */
15
    public function validate($manifest)
16
    {
17
        $errors = array();
18
19
        if (isset($manifest['config']['sort-packages']) && $manifest['config']['sort-packages']) {
20
            foreach (array('require', 'require-dev', 'conflict', 'replace', 'provide', 'suggest') as $linksSection) {
21
                if (array_key_exists($linksSection, $manifest) && !$this->packagesAreSorted($manifest[$linksSection])) {
22
                    array_push($errors, 'Links under '.$linksSection.' section are not sorted.');
23
                }
24
            }
25
        }
26
27
        return $errors;
28
    }
29
30
    private function packagesAreSorted(array $packages)
31
    {
32
        $names = array_keys($packages);
33
34
        $hasPHP = in_array('php', $names);
35
        $extNames = array_filter($names, function ($name) {
36
            return 'ext-' === substr($name, 0, 4) && !strstr($name, '/');
37
        });
38
        sort($extNames);
39
        $vendorName = array_filter($names, function ($name) {
40
            return 'ext-' !== substr($name, 0, 4) && 'php' !== $name;
41
        });
42
        sort($vendorName);
43
44
        $sortedNames = array_merge(
45
            $hasPHP ? array('php') : array(),
46
            $extNames,
47
            $vendorName
48
        );
49
50
        return $sortedNames === $names;
51
    }
52
}
53