1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TemplesOfCode\CodeSanity\Output; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use TemplesOfCode\CodeSanity\DiffItem; |
7
|
|
|
use TemplesOfCode\CodeSanity\Output; |
8
|
|
|
|
9
|
|
|
class PrettyOutput extends Output |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected static $maskTemplate = '| %%-%d.%ds | %%-%d.%ds | %%-%d.%ds | %%-%d.%ds |'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $fileNameSpaceLength = 30; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $hashSpaceLength = 40; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $border; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
2 |
|
protected function init() |
35
|
|
|
{ |
36
|
2 |
|
$this->setBorder(); |
37
|
2 |
|
$this->setMask(); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
2 |
|
protected function setBorder() |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* Initial constant lengths |
47
|
|
|
* @var int $borderLength |
48
|
|
|
*/ |
49
|
2 |
|
$borderLength = 13; |
50
|
|
|
$borderLength = $borderLength |
51
|
2 |
|
+ ($this->fileNameSpaceLength * 2) |
52
|
2 |
|
+ ($this->hashSpaceLength * 2) |
53
|
2 |
|
; |
54
|
|
|
|
55
|
2 |
|
$this->border = str_repeat('-', $borderLength); |
56
|
|
|
|
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
2 |
|
protected function setMask() |
63
|
|
|
{ |
64
|
2 |
|
$this->mask = sprintf( |
65
|
2 |
|
static::$maskTemplate, |
66
|
2 |
|
$this->getFileNameSpaceLength(), |
67
|
2 |
|
$this->getFileNameSpaceLength(), |
68
|
2 |
|
$this->getHashSpaceLength(), |
69
|
2 |
|
$this->getHashSpaceLength(), |
70
|
2 |
|
$this->getFileNameSpaceLength(), |
71
|
2 |
|
$this->getFileNameSpaceLength(), |
72
|
2 |
|
$this->getHashSpaceLength(), |
73
|
2 |
|
$this->getHashSpaceLength() |
74
|
2 |
|
); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
2 |
|
public function getFileNameSpaceLength() |
81
|
|
|
{ |
82
|
2 |
|
return $this->fileNameSpaceLength; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $fileNameSpaceLength |
87
|
|
|
* @return PrettyOutput |
88
|
|
|
*/ |
89
|
1 |
|
public function setFileNameSpaceLength($fileNameSpaceLength) |
90
|
|
|
{ |
91
|
1 |
|
if ($fileNameSpaceLength == $this->fileNameSpaceLength) { |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$this->fileNameSpaceLength = $fileNameSpaceLength; |
96
|
|
|
|
97
|
1 |
|
$this->setBorder(); |
98
|
1 |
|
$this->setMask(); |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
2 |
|
public function getHashSpaceLength() |
107
|
|
|
{ |
108
|
2 |
|
return $this->hashSpaceLength; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $hashSpaceLength |
113
|
|
|
* @return PrettyOutput |
114
|
|
|
*/ |
115
|
1 |
|
public function setHashSpaceLength($hashSpaceLength) |
116
|
|
|
{ |
117
|
1 |
|
if ($hashSpaceLength == $this->hashSpaceLength) { |
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$this->hashSpaceLength = $hashSpaceLength; |
122
|
|
|
|
123
|
1 |
|
$this->setBorder(); |
124
|
1 |
|
$this->setMask(); |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
1 |
|
public function write() |
133
|
|
|
{ |
134
|
1 |
|
if ($this->headerEnabled) { |
135
|
1 |
|
$this->writeHeader(); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
1 |
|
$this->output->writeln($this->border); |
139
|
|
|
|
140
|
1 |
|
foreach ($this->differences as $differenceSet) { |
141
|
|
|
/** |
142
|
|
|
* @var ArrayCollection $differenceSet |
143
|
|
|
*/ |
144
|
1 |
|
$this->writeDifferenceSet($differenceSet); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
1 |
|
$this->output->writeln($this->border); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param ArrayCollection $differenceSet |
152
|
|
|
*/ |
153
|
1 |
|
private function writeDifferenceSet(ArrayCollection $differenceSet) |
154
|
|
|
{ |
155
|
1 |
|
foreach ($differenceSet as $diffItem) { |
156
|
|
|
/** |
157
|
|
|
* @var DiffItem $diffItem |
158
|
|
|
*/ |
159
|
1 |
|
$this->writeDiffItem($diffItem); |
160
|
1 |
|
} |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
*/ |
166
|
1 |
|
private function writeHeader() |
167
|
|
|
{ |
168
|
1 |
|
$this->output->writeln($this->border); |
169
|
1 |
|
$this->output->writeln(sprintf( |
170
|
1 |
|
$this->getMask(), |
171
|
1 |
|
static::$header[0], |
172
|
1 |
|
static::$header[1], |
173
|
1 |
|
static::$header[2], |
174
|
1 |
|
static::$header[3] |
175
|
1 |
|
)); |
176
|
1 |
|
} |
177
|
|
|
} |
178
|
|
|
|