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

Validator::validate()   C

Complexity

Conditions 12
Paths 98

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 98
nop 1
dl 0
loc 46
rs 6.9666
c 0
b 0
f 0

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 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
        $config->set('exclude', array_filter(explode(',', $config->get('exclude'))));
39
40
        // groups by regex
41
        if (!$config->has('groups')) {
42
            $config->set('groups', []);
43
        }
44
        $groupsRaw = $config->get('groups');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $groupsRaw is correct as $config->get('groups') (which targets Hal\Application\Config\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
46
        $groups = [];
47
        $config->set('groups', []);
48
        foreach ($groupsRaw as $groupRaw) {
0 ignored issues
show
Bug introduced by
The expression $groupsRaw of type null is not traversable.
Loading history...
49
            $groups[] = new Group($groupRaw['name'], $groupRaw['match']);
50
        }
51
        $config->set('groups', $groups);
52
53
        // parameters with values
54
        $keys = ['report-html', 'report-csv', 'report-violation', 'report-json', 'extensions', 'config'];
55
        foreach ($keys as $key) {
56
            $value = $config->get($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $config->get($key) (which targets Hal\Application\Config\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

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