TextReporter::onInitialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\reporter;
13
14
use cloak\AnalyzedCoverageResult;
15
use cloak\event\InitializeEvent;
16
use cloak\event\AnalyzeStopEvent;
17
use cloak\result\FileResult;
18
use cloak\writer\ResultConsoleWriter;
19
use PHPExtra\EventManager\EventManager;
20
21
22
/**
23
 * Class TextReporter
24
 * @package cloak\reporter
25
 */
26
class TextReporter
27
    implements Reporter, InitializeEventListener, AnalyzeStopEventListener
28
{
29
30
31
    /**
32
     * @var \cloak\writer\ResultConsoleWriter
33
     */
34
    private $console;
35
36
37
    /**
38
     * @param \cloak\event\InitializeEvent $event
39
     */
40
    public function onInitialize(InitializeEvent $event)
41
    {
42
        $coverageBounds = $event->getCoverageBounds();
43
        $this->console = new ResultConsoleWriter($coverageBounds);
44
    }
45
46
    /**
47
     * @param \cloak\event\AnalyzeStopEvent $event
48
     */
49
    public function onAnalyzeStop(AnalyzeStopEvent $event)
50
    {
51
        $this->reportResult($event->getResult());
52
    }
53
54
    /**
55
     * @param AnalyzedCoverageResult $result
56
     */
57
    public function reportResult(AnalyzedCoverageResult $result)
58
    {
59
        $files = $result->getFiles()->getIterator();
60
61
        foreach ($files as $file) {
62
            $this->reportFile($file);
63
        }
64
65
        $this->writeTotalCoverage($result);
66
    }
67
68
    /**
69
     * @param \cloak\result\FileResult $file
70
     */
71
    protected function reportFile(FileResult $file)
72
    {
73
        $currentDirectory = getcwd();
74
75
        $filePathReport = $file->getRelativePath($currentDirectory);
76
77
        $this->console->writeResult($file);
78
        $this->console->writeText(' ');
79
        $this->console->writeText(sprintf("(%2d/%2d)",
80
            $file->getExecutedLineCount(),
81
            $file->getExecutableLineCount()
82
        ));
83
        $this->console->writeText(' ');
84
        $this->console->writeText($filePathReport);
85
86
        $this->console->writeText(PHP_EOL);
87
    }
88
89
    /**
90
     * @param AnalyzedCoverageResult $result
91
     */
92 View Code Duplication
    protected function writeTotalCoverage(AnalyzedCoverageResult $result)
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...
93
    {
94
        $this->console->writeText(PHP_EOL);
95
        $this->console->writeText('Code Coverage: ');
96
        $this->console->writeResult($result);
97
        $this->console->writeText(PHP_EOL);
98
    }
99
100
    /**
101
     * @param EventManager $eventManager
102
     */
103
    public function registerTo(EventManager $eventManager)
104
    {
105
        $eventManager->add($this);
106
    }
107
108
}
109