|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TemplesOfCode\CodeSanity; |
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Output |
|
10
|
|
|
* @package TemplesOfCode\CodeSanity |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Output |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static $header = [ |
|
18
|
|
|
'Source of truth Location', |
|
19
|
|
|
'Source of truth File Hash', |
|
20
|
|
|
'Target Location', |
|
21
|
|
|
'Target File Hash' |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $mask = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $headerEnabled; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var OutputInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $output; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ArrayCollection |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $differences; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Output constructor. |
|
46
|
|
|
* @param ArrayCollection $differences |
|
47
|
|
|
* @param OutputInterface $output |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function __construct(ArrayCollection $differences, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$this->differences = $differences; |
|
52
|
5 |
|
$this->output = $output; |
|
53
|
|
|
|
|
54
|
5 |
|
$this->init(); |
|
55
|
5 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return boolean |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function isHeaderEnabled() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->headerEnabled; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param boolean $headerEnabled |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function setHeaderEnabled($headerEnabled) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$this->headerEnabled = $headerEnabled; |
|
72
|
3 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
2 |
|
protected function getMask() |
|
79
|
|
|
{ |
|
80
|
2 |
|
return $this->mask; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param DiffItem $diffItem |
|
85
|
|
|
*/ |
|
86
|
2 |
|
protected function writeDiffItem(DiffItem $diffItem) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$sot = $sotHash = 'Missing'; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var RosterItem $sotRosterItem |
|
92
|
|
|
*/ |
|
93
|
2 |
|
$sotRosterItem = $diffItem->getSotRosterItem(); |
|
94
|
2 |
View Code Duplication |
if (!empty($sotRosterItem)) { |
|
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var Location $location |
|
97
|
|
|
*/ |
|
98
|
2 |
|
$location = $sotRosterItem->getRoster()->getLocation(); |
|
99
|
|
|
/** |
|
100
|
|
|
* @var string $sot |
|
101
|
|
|
*/ |
|
102
|
2 |
|
$sot = $location->getFullPath($sotRosterItem); |
|
103
|
|
|
/** |
|
104
|
|
|
* @var string $sotHash |
|
105
|
|
|
*/ |
|
106
|
2 |
|
$sotHash = $sotRosterItem->getHash(); |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
$target = $targetHash = 'Missing'; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @var RosterItem $targetRosterItem |
|
113
|
|
|
*/ |
|
114
|
2 |
|
$targetRosterItem = $diffItem->getTargetRosterItem(); |
|
115
|
2 |
View Code Duplication |
if (!empty($targetRosterItem)) { |
|
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @var Location $location |
|
118
|
|
|
*/ |
|
119
|
2 |
|
$location = $targetRosterItem->getRoster()->getLocation(); |
|
120
|
|
|
/** |
|
121
|
|
|
* @var string $target |
|
122
|
|
|
*/ |
|
123
|
2 |
|
$target = $location->getFullPath($targetRosterItem); |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @var string $targetHash |
|
127
|
|
|
*/ |
|
128
|
2 |
|
$targetHash = $targetRosterItem->getHash(); |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
$line = sprintf( |
|
132
|
2 |
|
$this->getMask(), |
|
133
|
2 |
|
$sot, |
|
134
|
2 |
|
$sotHash, |
|
135
|
2 |
|
$target, |
|
136
|
|
|
$targetHash |
|
137
|
2 |
|
); |
|
138
|
|
|
|
|
139
|
2 |
|
$this->output->writeln($line); |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* |
|
144
|
|
|
*/ |
|
145
|
|
|
abstract public function write(); |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Perform any logic at construction time. |
|
149
|
|
|
*/ |
|
150
|
|
|
abstract protected function init(); |
|
151
|
|
|
} |
|
152
|
|
|
|
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.