Completed
Pull Request — master (#301)
by personal
02:13
created

Reporter::generate()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 39
Code Lines 22

Duplication

Lines 3
Ratio 7.69 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 22
c 1
b 0
f 1
nc 8
nop 1
dl 3
loc 39
rs 4.8196

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\Report\Csv;
3
4
use Hal\Application\Config\Config;
5
use Hal\Component\Output\Output;
6
use Hal\Metric\ClassMetric;
7
use Hal\Metric\Metrics;
8
use Hal\Metric\Registry;
9
10
class Reporter
11
{
12
13
    /**
14
     * @var Config
15
     */
16
    private $config;
17
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23
    /**
24
     * Reporter constructor.
25
     * @param Config $config
26
     * @param OutputInterface $output
27
     */
28
    public function __construct(Config $config, Output $output)
29
    {
30
        $this->config = $config;
31
        $this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
It seems like $output of type object<Hal\Component\Output\Output> is incompatible with the declared type object<Hal\Report\Csv\OutputInterface> of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
35
    public function generate(Metrics $metrics)
36
    {
37
        if ($this->config->has('quiet')) {
38
            return;
39
        }
40
41
42
        $logFile = $this->config->get('report-csv');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $logFile is correct as $this->config->get('report-csv') (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...
43
        if (!$logFile) {
44
            return;
45
        }
46 View Code Duplication
        if (!file_exists(dirname($logFile)) || !is_writable(dirname($logFile))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            throw new \RuntimeException('You don\'t have permissions to write CSV report in ' . $logFile);
48
        }
49
50
        $availables = (new Registry())->allForStructures();
51
        $hwnd = fopen($logFile, 'w');
52
        fputcsv($hwnd, $availables);
53
54
        foreach ($metrics->all() as $metric) {
55
56
            if (!$metric instanceof ClassMetric) {
57
                continue;
58
            }
59
            $row = [];
60
            foreach ($availables as $key) {
61
                $data = $metric->get($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $metric->get($key) (which targets Hal\Metric\BagTrait::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...
62
                if (is_array($data) || !is_scalar($data)) {
63
                    $data = 'N/A';
64
                }
65
66
                array_push($row, $data);
67
            }
68
            fputcsv($hwnd, $row);
69
        }
70
71
        fclose($hwnd);
72
73
    }
74
}
75