Completed
Push — master ( bdaaad...4f4372 )
by personal
06:57 queued 04:36
created

Application::run()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 57
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 7
nop 1
dl 0
loc 57
rs 8.7433
c 1
b 0
f 0

How to fix   Long Method   

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;
3
4
use Hal\Application\Config\ConfigException;
5
use Hal\Application\Config\Parser;
6
use Hal\Application\Config\Validator;
7
use Hal\Component\File\Finder;
8
use Hal\Component\Issue\Issuer;
9
use Hal\Report;
10
use Hal\Violation\ViolationParser;
11
use Symfony\Component\Console\Formatter\OutputFormatter;
12
use Symfony\Component\Console\Output\ConsoleOutput;
13
14
15
class Application
16
{
17
18
    /**
19
     * @param $argv
20
     */
21
    public function run($argv)
22
    {
23
        // formatter
24
        $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, new OutputFormatter());
25
26
        // issues and debug
27
        $issuer = (new Issuer($output));//->enable();
28
29
        // config
30
        $config = (new Parser())->parse($argv);
31
        try {
32
            (new Validator())->validate($config);
33
        } catch (ConfigException $e) {
34
35
            if ($config->has('help')) {
36
                $output->writeln((new Validator())->help());
37
                exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
38
            }
39
40
            if ($config->has('version')) {
41
                $output->writeln(sprintf("PhpMetrics %s <http://phpmetrics.org>\nby Jean-François Lépine <https://twitter.com/Halleck45>", getVersion()));
42
                exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
43
            }
44
45
            $output->writeln(sprintf("\n<error>%s</error>\n", $e->getMessage()));
46
            $output->writeln((new Validator())->help());
47
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
48
        }
49
50
        if ($config->has('quiet')) {
51
            $output->setVerbosity(ConsoleOutput::VERBOSITY_QUIET);
52
        }
53
54
        // find files
55
        $finder = new Finder($config->get('extensions'), $config->get('exclude'));
0 ignored issues
show
Documentation introduced by
$config->get('extensions') is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$config->get('exclude') is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
        $files = $finder->fetch($config->get('files'));
0 ignored issues
show
Documentation introduced by
$config->get('files') is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
58
        // analyze
59
        try {
60
            $metrics = (new Analyze($config, $output, $issuer))->run($files);
61
        }catch(ConfigException $e) {
62
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
63
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
64
        }
65
66
        // violations
67
        (new ViolationParser($config, $output))->apply($metrics);
0 ignored issues
show
Unused Code introduced by
The call to ViolationParser::__construct() has too many arguments starting with $config.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
69
        // report
70
        (new Report\Cli\Reporter($config, $output))->generate($metrics);
71
        (new Report\Html\Reporter($config, $output))->generate($metrics);
72
        (new Report\Violations\Xml\Reporter($config, $output))->generate($metrics);
73
74
        // end
75
        $output->writeln('');
76
        $output->writeln('<info>Done</info>');
77
    }
78
}