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\CoverageResultNode; |
18
|
|
|
use cloak\result\CoverageResultVisitor; |
19
|
|
|
use cloak\writer\ResultConsoleWriter; |
20
|
|
|
use Zend\Console\ColorInterface as Color; |
21
|
|
|
use PHPExtra\EventManager\EventManager; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class TreeReporter |
26
|
|
|
* @package cloak\reporter |
27
|
|
|
*/ |
28
|
|
|
class TreeReporter |
29
|
|
|
implements Reporter, InitializeEventListener, AnalyzeStopEventListener, CoverageResultVisitor |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
const IDENT_SIZE = 2; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \cloak\writer\ResultConsoleWriter |
36
|
|
|
*/ |
37
|
|
|
private $console; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $indent; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
$this->indent = 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \cloak\event\InitializeEvent $event |
53
|
|
|
*/ |
54
|
|
|
public function onInitialize(InitializeEvent $event) |
55
|
|
|
{ |
56
|
|
|
$coverageBounds = $event->getCoverageBounds(); |
57
|
|
|
$this->console = new ResultConsoleWriter($coverageBounds); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \cloak\event\AnalyzeStopEvent $event |
62
|
|
|
*/ |
63
|
|
|
public function onAnalyzeStop(AnalyzeStopEvent $event) |
64
|
|
|
{ |
65
|
|
|
$result = $event->getResult(); |
66
|
|
|
|
67
|
|
|
$this->writeHeader($result); |
68
|
|
|
$this->writeChildResults($result); |
69
|
|
|
$this->writeTotalCoverage($event->getResult()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param CoverageResultNode $result |
74
|
|
|
*/ |
75
|
|
|
public function visit(CoverageResultNode $result) |
76
|
|
|
{ |
77
|
|
|
$this->writeResult($result); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param CoverageResultNode $result |
82
|
|
|
*/ |
83
|
|
|
protected function writeHeader(CoverageResultNode $result) |
84
|
|
|
{ |
85
|
|
|
$header = sprintf('%s code coverage', $result->getName()); |
86
|
|
|
$this->console->writeLine($header, Color::CYAN); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param CoverageResultNode $result |
91
|
|
|
*/ |
92
|
|
|
protected function writeResult(CoverageResultNode $result) |
93
|
|
|
{ |
94
|
|
|
$this->writeCoverageResult($result); |
95
|
|
|
$this->indent++; |
96
|
|
|
$this->writeChildResults($result); |
97
|
|
|
$this->indent--; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param CoverageResultNode $result |
102
|
|
|
*/ |
103
|
|
|
protected function writeChildResults(CoverageResultNode $result) |
104
|
|
|
{ |
105
|
|
|
$childResults = $result->getChildResults(); |
106
|
|
|
|
107
|
|
|
foreach ($childResults as $childResult) { |
108
|
|
|
$this->visit($childResult); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function writeCoverageResult(CoverageResultNode $result) |
113
|
|
|
{ |
114
|
|
|
$size = $this->indent * $this->indent; |
115
|
|
|
$indent = str_pad('', $size, ' '); |
116
|
|
|
$this->console->writeText($indent); |
117
|
|
|
$this->console->writeResult($result); |
118
|
|
|
$this->console->writeText(' '); |
119
|
|
|
$this->console->writeText($result->getName()); |
120
|
|
|
$this->console->writeEOL(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param AnalyzedCoverageResult $result |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
protected function writeTotalCoverage(AnalyzedCoverageResult $result) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$this->console->writeText(PHP_EOL); |
129
|
|
|
$this->console->writeText('Code Coverage: '); |
130
|
|
|
$this->console->writeResult($result); |
131
|
|
|
$this->console->writeText(PHP_EOL); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param EventManager $eventManager |
136
|
|
|
*/ |
137
|
|
|
public function registerTo(EventManager $eventManager) |
138
|
|
|
{ |
139
|
|
|
$eventManager->add($this); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
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.