Completed
Pull Request — master (#428)
by personal
01:35
created

Validator::validate()   C

Complexity

Conditions 13
Paths 194

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 194
nop 1
dl 0
loc 51
rs 5.8333
c 0
b 0
f 0

How to fix   Long Method    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 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 = [];
52
        $config->set('groups', []);
53
        foreach ($groupsRaw as $groupRaw) {
0 ignored issues
show
Bug introduced by
The expression $groupsRaw of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
54
            $groups[] = new Group($groupRaw['name'], $groupRaw['match']);
55
        }
56
        $config->set('groups', $groups);
57
58
        // parameters with values
59
        $keys = ['report-html', 'report-csv', 'report-violation', 'report-json', 'extensions', 'config'];
60
        foreach ($keys as $key) {
61
            $value = $config->get($key);
62
            if ($config->has($key) && empty($value) || true === $value) {
63
                throw new ConfigException(sprintf('%s option requires a value', $key));
64
            }
65
        }
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function help()
72
    {
73
        return <<<EOT
74
Usage:
75
76
    phpmetrics [...options...] <directories>
77
78
Required:
79
80
    <directories>                     List of directories to parse, separated by a comma (,)
81
82
Optional:
83
84
    --config=<file>                   Use a file for configuration
85
    --exclude=<directory>             List of directories to exclude, separated by a comma (,)
86
    --extensions=<php,inc>            List of extensions to parse, separated by a comma (,)
87
    --report-html=<directory>         Folder where report HTML will be generated
88
    --report-csv=<file>               File where report CSV will be generated
89
    --report-json=<file>              File where report Json will be generated
90
    --report-violations=<file>        File where XML violations report will be generated
91
    --git[=</path/to/git_binary>]     Perform analyses based on Git History (default binary path: "git")
92
    --junit[=</path/to/junit.xml>]    Evaluates metrics according to JUnit logs
93
    --quiet                           Quiet mode
94
    --version                         Display current version
95
96
Examples:
97
98
    phpmetrics --report-html="./report" ./src
99
100
        Analyze the "./src" directory and generate a HTML report on the "./report" folder
101
102
103
    phpmetrics --report-violations="./build/violations.xml" ./src,./lib
104
105
        Analyze the "./src" and "./lib" directories, and generate the "./build/violations.xml" file. This file could
106
        be read by any Continuous Integration Platform, and follows the "PMD Violation" standards.
107
108
EOT;
109
    }
110
}
111