StandardCliReporter::renderUrlOutput()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 30

Duplication

Lines 21
Ratio 70 %

Importance

Changes 0
Metric Value
dl 21
loc 30
rs 7.6666
c 0
b 0
f 0
cc 10
nc 64
nop 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 whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use PhmLabs\Components\Init\Init;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use whm\Smoke\Config\Configuration;
8
use whm\Smoke\Rules\CheckResult;
9
use whm\Smoke\Rules\Rule;
10
11
class StandardCliReporter extends CliReporter
12
{
13
    /**
14
     * @var CheckResult[]
15
     */
16
    private $results = array();
17
    private $orderBy;
18
19
    /**
20
     * @var Rule[]
21
     */
22
    private $rules = array();
23
    private $maxResults;
24
25
    public function init(OutputInterface $_output, Configuration $_configuration, $orderBy = 'url', $maxResults = 0)
26
    {
27
        $this->setOutputInterface($_output);
28
29
        $this->orderBy = $orderBy;
30
        $this->rules = $_configuration->getRules();
31
32
        if ($maxResults === 0) {
33
            $this->maxResults = 10000000;
34
        } else {
35
            $this->maxResults = $maxResults;
36
        }
37
    }
38
39
    /**
40
     * @param \whm\Smoke\Rules\CheckResult[] $results
41
     */
42
    public function processResults($results)
43
    {
44
        if (count($results) === 0) {
45
            return;
46
        }
47
48
        $failures = false;
49
50
        $processedResults = [];
51
52
        foreach ($results as $result) {
53
            if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
54
                $processedResults[] = $result;
55
                $failures = true;
56
            }
57
        }
58
        if ($failures) {
59
            $this->results[] = $processedResults;
60
        } else {
61
            $this->results[] = [array_pop($results)];
62
        }
63
    }
64
65
    public function finish()
66
    {
67
        if ($this->orderBy === 'url') {
68
            $this->renderUrlOutput();
69
        } elseif ($this->orderBy === 'rule') {
70
            $this->renderRuleOutput();
71
        }
72
        $this->output->writeln('');
73
    }
74
75
    private function getFailedUrls($ruleKey)
76
    {
77
        $failedUrls = array();
78
        $count = 0;
79
        foreach ($this->results as $results) {
80
            foreach ($results as $key => $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Rules\CheckResult> is not traversable.
Loading history...
81
                /** @var CheckResult $result */
82
                if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
83
                    if ($ruleKey === $key) {
84
                        $failedUrls[] = (string) $result->getResponse()->getUri() . ' - ' . $result->getMessage();
85
                        ++$count;
86
                    }
87
                    if ($count > $this->maxResults) {
88
                        $failedUrls[] = '... only the first ' . $this->maxResults . ' elements are shown.';
89
                        break;
90
                    }
91
                }
92
            }
93
        }
94
95
        return $failedUrls;
96
    }
97
98
    private function renderRuleOutput()
99
    {
100
        $this->output->writeln("\n\n <comment>Rules and Violations:</comment> \n");
101
102
        foreach ($this->rules as $ruleKey => $rule) {
103
            $info = Init::getInitInformationByClass($rule);
0 ignored issues
show
Bug introduced by
The call to getInitInformationByClass() misses a required argument $initMethod.

This check looks for function calls that miss required arguments.

Loading history...
104
            $failedUrls = $this->getFailedUrls($ruleKey);
105
106
            if (count($failedUrls) > 0) {
107
                $this->output->writeln('  <error> ' . get_class($rule) . ' </error>');
108
            } else {
109
                $this->output->writeln('  <info> ' . get_class($rule) . ' </info>');
110
            }
111
112
            $this->output->writeln('   ' . str_replace("\n", "\n   ", $info['documentation']) . "\n");
113
114
            foreach ($failedUrls as $failedUrl) {
115
                $this->output->writeln('   - ' . $failedUrl);
116
            }
117
118
            $this->output->writeln('');
119
        }
120
    }
121
122
    private function renderUrlOutput()
123
    {
124
        $this->output->writeln("\n\n <comment>Passed tests:</comment> \n");
125
126 View Code Duplication
        foreach ($this->results as $results) {
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...
127
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Rules\CheckResult> is not traversable.
Loading history...
128
                if ($result->getStatus() === CheckResult::STATUS_SUCCESS) {
129
                    $this->renderSuccess($result);
130
                }
131
            }
132
        }
133
134
        $this->output->writeln("\n <comment>Failed tests:</comment> \n");
135
136 View Code Duplication
        foreach ($this->results as $results) {
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...
137
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Rules\CheckResult> is not traversable.
Loading history...
138
                if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
139
                    $this->renderFailure($result);
140
                }
141
            }
142
        }
143
144 View Code Duplication
        foreach ($this->results as $results) {
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...
145
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Rules\CheckResult> is not traversable.
Loading history...
146
                if ($result->getStatus() === CheckResult::STATUS_SKIPPED) {
147
                    $this->renderSkipped($result);
148
                }
149
            }
150
        }
151
    }
152
}
153