Completed
Push — master ( 1e2def...61d955 )
by Omar
02:27
created

PrettyOutput::writeDiffItem()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 46
Code Lines 22

Duplication

Lines 20
Ratio 43.48 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 20
loc 46
rs 8.9411
cc 3
eloc 22
nc 4
nop 1
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
use TemplesOfCode\CodeSanity\RosterItem;
9
10
class PrettyOutput extends Output
11
{
12
    /**
13
     * @var string
14
     */
15
    protected static $maskTemplate = '| %%-%d.%ds | %%-%d.%ds | %%-%d.%ds | %%-%d.%ds |';
16
    
17
    /**
18
     * @var int
19
     */
20
    protected  $fileNameSpaceLength = 30;
21
22
    /**
23
     * @var int
24
     */
25
    protected  $hashSpaceLength = 40;
26
    
27
    /**
28
     * @var string
29
     */
30
    protected $border;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function init()
36
    {
37
        $this->setBorder();
38
        $this->setMask();
39
    }
40
41
    /**
42
     *
43
     */
44
    protected function setBorder()
45
    {
46
        /**
47
         * Initial constant lengths
48
         * @var int $borderLength
49
         */
50
        $borderLength = 13;
51
        $borderLength = $borderLength
52
            + ($this->fileNameSpaceLength * 2)
53
            + ($this->hashSpaceLength * 2)
54
        ;
55
56
        $this->border = str_repeat('-', $borderLength);
57
58
    }
59
    
60
    /**
61
     *
62
     */
63
    protected function setMask()
64
    {
65
           $this->mask = sprintf(
66
               static::$maskTemplate,
67
               $this->getFileNameSpaceLength(),
68
               $this->getFileNameSpaceLength(),
69
               $this->getHashSpaceLength(),
70
               $this->getHashSpaceLength(),
71
               $this->getFileNameSpaceLength(),
72
               $this->getFileNameSpaceLength(),
73
               $this->getHashSpaceLength(),
74
               $this->getHashSpaceLength()
75
           );
76
    }
77
78
    /**
79
     * @return int
80
     */
81
    public function getFileNameSpaceLength()
82
    {
83
        return $this->fileNameSpaceLength;
84
    }
85
86
    /**
87
     * @param int $fileNameSpaceLength
88
     * @return PrettyOutput
89
     */
90
    public function setFileNameSpaceLength($fileNameSpaceLength)
91
    {
92
        if ($fileNameSpaceLength == $this->fileNameSpaceLength) {
93
            return $this;
94
        }
95
96
        $this->fileNameSpaceLength = $fileNameSpaceLength;
97
98
        $this->setBorder();
99
        $this->setMask();
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getHashSpaceLength()
108
    {
109
        return $this->hashSpaceLength;
110
    }
111
112
    /**
113
     * @param int $hashSpaceLength
114
     * @return PrettyOutput
115
     */
116
    public function setHashSpaceLength($hashSpaceLength)
117
    {
118
        if ($hashSpaceLength == $this->hashSpaceLength) {
119
            return $this;
120
        }
121
122
        $this->hashSpaceLength = $hashSpaceLength;
123
124
        $this->setBorder();
125
        $this->setMask();
126
127
        return $this;
128
    }
129
130
    /**
131
     *
132
     */
133
    public function write()
134
    {
135
        if ($this->headerEnabled) {
136
            $this->writeHeader();
137
        }
138
139
        $this->output->writeln($this->border);
140
141
        foreach ($this->differences as $differenceSet) {
142
            /**
143
             * @var ArrayCollection<DiffItem> $differenceSet
0 ignored issues
show
Documentation introduced by
The doc-type ArrayCollection<DiffItem> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
144
             */
145
            $this->writeDifferenceSet($differenceSet);
146
        }
147
148
        $this->output->writeln($this->border);
149
    }
150
151
    /**
152
     * @param ArrayCollection $differenceSet
153
     */
154
    private function writeDifferenceSet(ArrayCollection $differenceSet)
155
    {
156
        foreach ($differenceSet as $diffItem) {
157
            /**
158
             * @var DiffItem $diffItem
159
             */
160
            $this->writeDiffItem($diffItem);
161
        }
162
    }
163
164
    /**
165
     *
166
     */
167
    private function writeHeader()
168
    {
169
        $this->output->writeln($this->border);
170
        $this->output->writeln(sprintf(
171
            $this->getMask(),
172
            static::$header[0],
173
            static::$header[1],
174
            static::$header[2],
175
            static::$header[3]
176
        ));
177
    }
178
}
179