Completed
Pull Request — master (#428)
by personal
02:44 queued 01:01
created

Validator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 0 49 12
A help() 0 39 1
1
<?php
2
3
namespace Hal\Application\Config;
4
5
use Hal\Metric\Group\Group;
6
7
/**
8
 * @package Hal\Application\Config
9
 */
10
class Validator
11
{
12
    /**
13
     * @param Config $config
14
     * @throws ConfigException
15
     */
16
    public function validate(Config $config)
17
    {
18
        // required
19
        if (!$config->has('files')) {
20
            throw new ConfigException('Directory to parse is missing or incorrect');
21
        }
22
        foreach ($config->get('files') as $dir) {
0 ignored issues
show
Bug introduced by
The expression $config->get('files') of type null is not traversable.
Loading history...
23
            if (!file_exists($dir)) {
24
                throw new ConfigException(sprintf('Directory %s does not exist', $dir));
25
            }
26
        }
27
28
        // extensions
29
        if (!$config->has('extensions')) {
30
            $config->set('extensions', 'php,inc');
31
        }
32
        $config->set('extensions', explode(',', $config->get('extensions')));
33
34
        // excluded directories
35
        if (!$config->has('exclude')) {
36
            $config->set('exclude', 'vendor,test,Test,tests,Tests,testing,Testing,bower_components,node_modules,cache,spec');
37
        }
38
39
        // retro-compatibility with excludes as string in config files
40
        if (is_array($config->get('exclude'))) {
41
            $config->set('exclude', implode(',', $config->get('exclude')));
42
        }
43
        $config->set('exclude', array_filter(explode(',', $config->get('exclude'))));
44
45
        // groups by regex
46
        if (!$config->has('groups')) {
47
            $config->set('groups', []);
48
        }
49
        $groupsRaw = $config->get('groups');
50
51
        $groups = array_map(static function (array $groupRaw): Group {
52
            return new Group($groupRaw['name'], $groupRaw['match']);
53
        }, $groupsRaw);
54
        $config->set('groups', $groups);
55
56
        // parameters with values
57
        $keys = ['report-html', 'report-csv', 'report-violation', 'report-json', 'extensions', 'config'];
58
        foreach ($keys as $key) {
59
            $value = $config->get($key);
60
            if ($config->has($key) && empty($value) || true === $value) {
61
                throw new ConfigException(sprintf('%s option requires a value', $key));
62
            }
63
        }
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function help()
70
    {
71
        return <<<EOT
72
Usage:
73
74
    phpmetrics [...options...] <directories>
75
76
Required:
77
78
    <directories>                     List of directories to parse, separated by a comma (,)
79
80
Optional:
81
82
    --config=<file>                   Use a file for configuration
83
    --exclude=<directory>             List of directories to exclude, separated by a comma (,)
84
    --extensions=<php,inc>            List of extensions to parse, separated by a comma (,)
85
    --report-html=<directory>         Folder where report HTML will be generated
86
    --report-csv=<file>               File where report CSV will be generated
87
    --report-json=<file>              File where report Json will be generated
88
    --report-violations=<file>        File where XML violations report will be generated
89
    --git[=</path/to/git_binary>]     Perform analyses based on Git History (default binary path: "git")
90
    --junit[=</path/to/junit.xml>]    Evaluates metrics according to JUnit logs
91
    --quiet                           Quiet mode
92
    --version                         Display current version
93
94
Examples:
95
96
    phpmetrics --report-html="./report" ./src
97
98
        Analyze the "./src" directory and generate a HTML report on the "./report" folder
99
100
101
    phpmetrics --report-violations="./build/violations.xml" ./src,./lib
102
103
        Analyze the "./src" and "./lib" directories, and generate the "./build/violations.xml" file. This file could
104
        be read by any Continuous Integration Platform, and follows the "PMD Violation" standards.
105
106
EOT;
107
    }
108
}
109