Completed
Push — master ( bdaaad...4f4372 )
by personal
04:35 queued 45s
created

Validator::validate()   D

Complexity

Conditions 10
Paths 26

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 26
nop 1
dl 0
loc 34
rs 4.8196
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
namespace Hal\Application\Config;
3
4
/**
5
 * Class Validator
6
 * @package Hal\Application\Config
7
 */
8
class Validator
9
{
10
11
    /**
12
     * @param Config $config
13
     * @throws ConfigException
14
     */
15
    public function validate(Config $config)
16
    {
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
        // parameters with values
41
        $keys = ['report-html', 'report-violation', 'extensions'];
42
        foreach ($keys as $key) {
43
            $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...
44
            if ($config->has($key) && empty($value) || true === $value) {
45
                throw new ConfigException(sprintf('%s option requires a value', $key));
46
            }
47
        }
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function help()
54
    {
55
        return <<<EOT
56
Usage:
57
58
    phpmetrics [...options...] <directories>
59
60
Required:
61
62
    <directories>                       List of directories to parse, separated by a comma (,)
63
64
Optional:
65
66
    --exclude=<directory>               List of directories to exclude, separated by a comma (,)
67
    --extensions=<php,inc>              List of extensions to parse, separated by a comma (,)
68
    --report-html=<directory>           Folder where report HTML will be generated
69
    --report-violations=<file>          File where XML violations report will be generated
70
    --git[=</path/to/git_binary>]       Perform analyses based on Git History (default binary path: "git")
71
    --junit[=</path/to/junit.xml>]      Evaluates metrics according to JUnit logs
72
    --quiet                             Quiet mode
73
    --version                           Display current version
74
75
Examples:
76
77
    phpmetrics --report-html="./report" ./src
78
79
        Analyze the "./src" directory and generate a HTML report on the "./report" folder
80
81
82
    phpmetrics --report-violations="./build/violations.xml" ./src,./lib
83
84
        Analyze the "./src" and "./lib" directories, and generate the "./build/violations.xml" file. This file could 
85
        be read by any Continuous Integration Platform, and follows the "PMD Violation" standards.
86
87
EOT;
88
    }
89
}
90