Completed
Push — develop ( 29208e...50a0ec )
by Adrien
05:34
created

Fill   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 308
Duplicated Lines 16.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.58%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 50
loc 308
rs 10
c 1
b 0
f 0
ccs 62
cts 76
cp 0.8158
wmc 30
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getSharedComponent() 0 4 1
A getStyleArray() 0 4 1
C applyFromArray() 0 25 7
A getFillType() 0 8 2
A setFillType() 11 11 2
A getRotation() 0 8 2
A setRotation() 11 11 2
A getStartColor() 0 4 1
A setStartColor() 14 14 3
A getEndColor() 0 4 1
A setEndColor() 14 14 3
A getHashCode() 0 14 2
A __construct() 0 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\IComparable;
7
8
class Fill extends Supervisor implements IComparable
9
{
10
    /* Fill types */
11
    const FILL_NONE = 'none';
12
    const FILL_SOLID = 'solid';
13
    const FILL_GRADIENT_LINEAR = 'linear';
14
    const FILL_GRADIENT_PATH = 'path';
15
    const FILL_PATTERN_DARKDOWN = 'darkDown';
16
    const FILL_PATTERN_DARKGRAY = 'darkGray';
17
    const FILL_PATTERN_DARKGRID = 'darkGrid';
18
    const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
19
    const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
20
    const FILL_PATTERN_DARKUP = 'darkUp';
21
    const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
22
    const FILL_PATTERN_GRAY0625 = 'gray0625';
23
    const FILL_PATTERN_GRAY125 = 'gray125';
24
    const FILL_PATTERN_LIGHTDOWN = 'lightDown';
25
    const FILL_PATTERN_LIGHTGRAY = 'lightGray';
26
    const FILL_PATTERN_LIGHTGRID = 'lightGrid';
27
    const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
28
    const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
29
    const FILL_PATTERN_LIGHTUP = 'lightUp';
30
    const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
31
    const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
32
33
    /**
34
     * Fill type.
35
     *
36
     * @var string
37
     */
38
    protected $fillType = self::FILL_NONE;
39
40
    /**
41
     * Rotation.
42
     *
43
     * @var float
44
     */
45
    protected $rotation = 0;
46
47
    /**
48
     * Start color.
49
     *
50
     * @var Color
51
     */
52
    protected $startColor;
53
54
    /**
55
     * End color.
56
     *
57
     * @var Color
58
     */
59
    protected $endColor;
60
61
    /**
62
     * Create a new Fill.
63
     *
64
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
65
     *                                    Leave this value at default unless you understand exactly what
66
     *                                        its ramifications are
67
     * @param bool $isConditional Flag indicating if this is a conditional style or not
68
     *                                    Leave this value at default unless you understand exactly what
69
     *                                        its ramifications are
70
     */
71 28
    public function __construct($isSupervisor = false, $isConditional = false)
72
    {
73
        // Supervisor?
74 28
        parent::__construct($isSupervisor);
75
76
        // Initialise values
77 28
        if ($isConditional) {
78
            $this->fillType = null;
79
        }
80 28
        $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
81 28
        $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
82
83
        // bind parent if we are a supervisor
84 28
        if ($isSupervisor) {
85 28
            $this->startColor->bindParent($this, 'startColor');
86 28
            $this->endColor->bindParent($this, 'endColor');
87
        }
88 28
    }
89
90
    /**
91
     * Get the shared style component for the currently active cell in currently active sheet.
92
     * Only used for style supervisor.
93
     *
94
     * @return Fill
95
     */
96 1
    public function getSharedComponent()
97
    {
98 1
        return $this->parent->getSharedComponent()->getFill();
99
    }
100
101
    /**
102
     * Build style array from subcomponents.
103
     *
104
     * @param array $array
105
     *
106
     * @return array
107
     */
108 2
    public function getStyleArray($array)
109
    {
110 2
        return ['fill' => $array];
111
    }
112
113
    /**
114
     * Apply styles from array.
115
     * <code>
116
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
117
     *        array(
118
     *            'fillType'       => Fill::FILL_GRADIENT_LINEAR,
119
     *            'rotation'   => 0,
120
     *            'startColor' => array(
121
     *                'rgb' => '000000'
122
     *            ),
123
     *            'endColor'   => array(
124
     *                'argb' => 'FFFFFFFF'
125
     *            )
126
     *        )
127
     * );
128
     * </code>.
129
     *
130
     * @param array $pStyles Array containing style information
131
     *
132
     * @throws PhpSpreadsheetException
133
     *
134
     * @return Fill
135
     */
136 3
    public function applyFromArray(array $pStyles)
137
    {
138 3
        if ($this->isSupervisor) {
139
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
140
        } else {
141 3
            if (isset($pStyles['fillType'])) {
142 3
                $this->setFillType($pStyles['fillType']);
143
            }
144 3
            if (isset($pStyles['rotation'])) {
145 1
                $this->setRotation($pStyles['rotation']);
146
            }
147 3
            if (isset($pStyles['startColor'])) {
148 2
                $this->getStartColor()->applyFromArray($pStyles['startColor']);
149
            }
150 3
            if (isset($pStyles['endColor'])) {
151 1
                $this->getEndColor()->applyFromArray($pStyles['endColor']);
152
            }
153 3
            if (isset($pStyles['color'])) {
154 1
                $this->getStartColor()->applyFromArray($pStyles['color']);
155 1
                $this->getEndColor()->applyFromArray($pStyles['color']);
156
            }
157
        }
158
159 3
        return $this;
160
    }
161
162
    /**
163
     * Get Fill Type.
164
     *
165
     * @return string
166
     */
167 11
    public function getFillType()
168
    {
169 11
        if ($this->isSupervisor) {
170 1
            return $this->getSharedComponent()->getFillType();
171
        }
172
173 11
        return $this->fillType;
174
    }
175
176
    /**
177
     * Set Fill Type.
178
     *
179
     * @param string $pValue Fill type, see self::FILL_*
180
     *
181
     * @return Fill
182
     */
183 4 View Code Duplication
    public function setFillType($pValue)
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...
184
    {
185 4
        if ($this->isSupervisor) {
186 2
            $styleArray = $this->getStyleArray(['fillType' => $pValue]);
187 2
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
188
        } else {
189 4
            $this->fillType = $pValue;
190
        }
191
192 4
        return $this;
193
    }
194
195
    /**
196
     * Get Rotation.
197
     *
198
     * @return float
199
     */
200 10
    public function getRotation()
201
    {
202 10
        if ($this->isSupervisor) {
203
            return $this->getSharedComponent()->getRotation();
204
        }
205
206 10
        return $this->rotation;
207
    }
208
209
    /**
210
     * Set Rotation.
211
     *
212
     * @param float $pValue
213
     *
214
     * @return Fill
215
     */
216 1 View Code Duplication
    public function setRotation($pValue)
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...
217
    {
218 1
        if ($this->isSupervisor) {
219
            $styleArray = $this->getStyleArray(['rotation' => $pValue]);
220
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
221
        } else {
222 1
            $this->rotation = $pValue;
223
        }
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * Get Start Color.
230
     *
231
     * @return Color
232
     */
233 11
    public function getStartColor()
234
    {
235 11
        return $this->startColor;
236
    }
237
238
    /**
239
     * Set Start Color.
240
     *
241
     * @param Color $pValue
242
     *
243
     * @throws PhpSpreadsheetException
244
     *
245
     * @return Fill
246
     */
247 1 View Code Duplication
    public function setStartColor(Color $pValue)
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...
248
    {
249
        // make sure parameter is a real color and not a supervisor
250 1
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
251
252 1
        if ($this->isSupervisor) {
253 1
            $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
254 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
255
        } else {
256
            $this->startColor = $color;
257
        }
258
259 1
        return $this;
260
    }
261
262
    /**
263
     * Get End Color.
264
     *
265
     * @return Color
266
     */
267 11
    public function getEndColor()
268
    {
269 11
        return $this->endColor;
270
    }
271
272
    /**
273
     * Set End Color.
274
     *
275
     * @param Color $pValue
276
     *
277
     * @throws PhpSpreadsheetException
278
     *
279
     * @return Fill
280
     */
281 View Code Duplication
    public function setEndColor(Color $pValue)
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...
282
    {
283
        // make sure parameter is a real color and not a supervisor
284
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
285
286
        if ($this->isSupervisor) {
287
            $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
288
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
289
        } else {
290
            $this->endColor = $color;
291
        }
292
293
        return $this;
294
    }
295
296
    /**
297
     * Get hash code.
298
     *
299
     * @return string Hash code
300
     */
301 10
    public function getHashCode()
302
    {
303 10
        if ($this->isSupervisor) {
304
            return $this->getSharedComponent()->getHashCode();
305
        }
306
307 10
        return md5(
308 10
            $this->getFillType() .
309 10
            $this->getRotation() .
310 10
            $this->getStartColor()->getHashCode() .
311 10
            $this->getEndColor()->getHashCode() .
312 10
            __CLASS__
313
        );
314
    }
315
}
316