Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

FinalPrinter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 120
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 98.61%

Importance

Changes 14
Bugs 2 Features 1
Metric Value
wmc 19
c 14
b 2
f 1
lcom 1
cbo 4
dl 20
loc 120
ccs 71
cts 72
cp 0.9861
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onEngineEnd() 0 29 5
A printAllFailuresOutput() 10 10 3
A printFailuresOutput() 0 20 4
A printAllFilesRecap() 10 10 3
A printFileRecap() 0 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\EngineEvent;
6
use Paraunit\Parser\JSONLogParser;
7
use Paraunit\Parser\OutputContainerBearerInterface;
8
use Paraunit\Process\ParaunitProcessAbstract;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class FinalPrinter.
13
 */
14
class FinalPrinter
15
{
16
    /** @var  JSONLogParser */
17
    private $logParser;
18
19
    /** @var  OutputInterface */
20
    private $output;
21
22
    /**
23
     * FinalPrinter constructor.
24
     * @param JSONLogParser $logParser
25
     */
26 9
    public function __construct(JSONLogParser $logParser)
27
    {
28 9
        $this->logParser = $logParser;
29 9
    }
30
31
    /**
32
     * @param EngineEvent $engineEvent
33
     */
34 9
    public function onEngineEnd(EngineEvent $engineEvent)
35
    {
36 9
        if ( ! $engineEvent->has('start') || ! $engineEvent->has('end') || ! $engineEvent->has('process_completed')) {
37
            throw new \BadMethodCallException('missing argument/s');
38
        }
39
40 9
        $this->output = $engineEvent->getOutputInterface();
41
        /** @var \DateInterval $elapsedTime */
42 9
        $elapsedTime = $engineEvent->get('start')->diff($engineEvent->get('end'));
43 9
        $completedProcesses = $engineEvent->get('process_completed');
44
45 9
        $this->output->writeln('');
46 9
        $this->output->writeln('');
47 9
        $this->output->writeln($elapsedTime->format('Execution time -- %H:%I:%S '));
48
49 9
        $testsCount = 0;
50
        /** @var ParaunitProcessAbstract $process */
51 9
        foreach ($completedProcesses as $process) {
52 9
            $testsCount += count($process->getTestResults());
53 9
        }
54
55 9
        $this->output->writeln('');
56 9
        $this->output->writeln(sprintf('Executed: %d test classes, %d tests', count($completedProcesses), $testsCount));
57
58 9
        $this->printAllFailuresOutput();
59 9
        $this->printAllFilesRecap();
60
61 9
        $this->output->writeln('');
62 9
    }
63
64 9 View Code Duplication
    private function printAllFailuresOutput()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66 9
        $this->printFailuresOutput($this->logParser->getAbnormalTerminatedOutputContainer());
67
68 9
        foreach ($this->logParser->getParsersForPrinting() as $parser) {
69 9
            if ($parser instanceof OutputContainerBearerInterface) {
70 9
                $this->printFailuresOutput($parser->getOutputContainer());
71 9
            }
72 9
        }
73 9
    }
74
75
    /**
76
     * @param OutputContainerInterface $outputContainer
77
     */
78 9
    private function printFailuresOutput(OutputContainerInterface $outputContainer)
79
    {
80 9
        $buffer = $outputContainer->getOutputBuffer();
81 9
        if (count($buffer)) {
82 8
            $tag = $outputContainer->getTag();
83 8
            $this->output->writeln('');
84 8
            $this->output->writeln(sprintf('<%s>%s output:</%s>', $tag, ucwords($outputContainer->getTitle()), $tag));
85
86 8
            $i = 1;
87
88 8
            foreach ($buffer as $filename => $messages) {
89 8
                foreach ($messages as $message) {
0 ignored issues
show
Bug introduced by
The expression $messages of type string is not traversable.
Loading history...
90 8
                    $this->output->writeln('');
91 8
                    $this->output->writeln(
92 8
                        sprintf('<%s>%d)</%s> %s', $tag, $i++, $tag, $message)
93 8
                    );
94 8
                }
95 8
            }
96 8
        }
97 9
    }
98
99 9 View Code Duplication
    private function printAllFilesRecap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101 9
        $this->printFileRecap($this->logParser->getAbnormalTerminatedOutputContainer());
102
103 9
        foreach ($this->logParser->getParsersForPrinting() as $parser) {
104 9
            if ($parser instanceof OutputContainerBearerInterface) {
105 9
                $this->printFileRecap($parser->getOutputContainer());
106 9
            }
107 9
        }
108 9
    }
109
110
    /**
111
     * @param OutputContainerInterface $outputContainer
112
     */
113 9
    private function printFileRecap(OutputContainerInterface $outputContainer)
114
    {
115 9
        if ($outputContainer->countFiles()) {
116 8
            $tag = $outputContainer->getTag();
117 8
            $this->output->writeln('');
118 8
            $this->output->writeln(
119 8
                sprintf(
120 8
                    '<%s>%d files with %s:</%s>',
121 8
                    $tag,
122 8
                    $outputContainer->countFiles(),
123 8
                    strtoupper($outputContainer->getTitle()),
124
                    $tag
125 8
                )
126 8
            );
127
128 8
            foreach ($outputContainer->getFileNames() as $fileName) {
129 8
                $this->output->writeln(sprintf(' <%s>%s</%s>', $tag, $fileName, $tag));
130 8
            }
131 8
        }
132 9
    }
133
}
134