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

Linter::validate()   C

Complexity

Conditions 13
Paths 26

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 5.1234
cc 13
eloc 15
nc 26
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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