Completed
Push — develop ( e6c95b...1c5db4 )
by Adrien
32:25
created

Border::getBorderStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\IComparable;
7
8
/**
9
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
10
 *
11
 * This library is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * This library is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with this library; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 *
25
 * @category   PhpSpreadsheet
26
 *
27
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
28
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
29
 */
30
class Border extends Supervisor implements IComparable
31
{
32
    /* Border style */
33
    const BORDER_NONE = 'none';
34
    const BORDER_DASHDOT = 'dashDot';
35
    const BORDER_DASHDOTDOT = 'dashDotDot';
36
    const BORDER_DASHED = 'dashed';
37
    const BORDER_DOTTED = 'dotted';
38
    const BORDER_DOUBLE = 'double';
39
    const BORDER_HAIR = 'hair';
40
    const BORDER_MEDIUM = 'medium';
41
    const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
42
    const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
43
    const BORDER_MEDIUMDASHED = 'mediumDashed';
44
    const BORDER_SLANTDASHDOT = 'slantDashDot';
45
    const BORDER_THICK = 'thick';
46
    const BORDER_THIN = 'thin';
47
48
    /**
49
     * Border style.
50
     *
51
     * @var string
52
     */
53
    protected $borderStyle = self::BORDER_NONE;
54
55
    /**
56
     * Border color.
57
     *
58
     * @var Color
59
     */
60
    protected $color;
61
62
    /**
63
     * Parent property name.
64
     *
65
     * @var string
66
     */
67
    protected $parentPropertyName;
68
69
    /**
70
     * Create a new Border.
71
     *
72
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
73
     *                                    Leave this value at default unless you understand exactly what
74
     *                                        its ramifications are
75
     * @param bool $isConditional Flag indicating if this is a conditional style or not
76
     *                                    Leave this value at default unless you understand exactly what
77
     *                                        its ramifications are
78
     */
79 80
    public function __construct($isSupervisor = false, $isConditional = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isConditional is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        // Supervisor?
82 80
        parent::__construct($isSupervisor);
83
84
        // Initialise values
85 80
        $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
86
87
        // bind parent if we are a supervisor
88 80
        if ($isSupervisor) {
89 80
            $this->color->bindParent($this, 'color');
90
        }
91 80
    }
92
93
    /**
94
     * Bind parent. Only used for supervisor.
95
     *
96
     * @param Borders $parent
97
     * @param string $parentPropertyName
98
     *
99
     * @return Border
100
     */
101 80
    public function bindParent($parent, $parentPropertyName = null)
102
    {
103 80
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent of type object<PhpOffice\PhpSpreadsheet\Style\Borders> is incompatible with the declared type object<PhpOffice\PhpSpreadsheet\Style> of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104 80
        $this->parentPropertyName = $parentPropertyName;
105
106 80
        return $this;
107
    }
108
109
    /**
110
     * Get the shared style component for the currently active cell in currently active sheet.
111
     * Only used for style supervisor.
112
     *
113
     * @throws PhpSpreadsheetException
114
     *
115
     * @return Border
116
     */
117 1
    public function getSharedComponent()
118
    {
119 1
        switch ($this->parentPropertyName) {
120 1
            case 'allBorders':
121 1
            case 'horizontal':
122 1
            case 'inside':
123 1
            case 'outline':
124 1
            case 'vertical':
125
                throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
126
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
127 1
            case 'bottom':
128 1
                return $this->parent->getSharedComponent()->getBottom();
0 ignored issues
show
Bug introduced by
The method getBottom() does not seem to exist on object<PhpOffice\PhpSpreadsheet\Style>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
            case 'diagonal':
130
                return $this->parent->getSharedComponent()->getDiagonal();
0 ignored issues
show
Bug introduced by
The method getDiagonal() does not seem to exist on object<PhpOffice\PhpSpreadsheet\Style>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            case 'left':
132
                return $this->parent->getSharedComponent()->getLeft();
0 ignored issues
show
Bug introduced by
The method getLeft() does not seem to exist on object<PhpOffice\PhpSpreadsheet\Style>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            case 'right':
134
                return $this->parent->getSharedComponent()->getRight();
0 ignored issues
show
Bug introduced by
The method getRight() does not seem to exist on object<PhpOffice\PhpSpreadsheet\Style>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
            case 'top':
136
                return $this->parent->getSharedComponent()->getTop();
0 ignored issues
show
Bug introduced by
The method getTop() does not seem to exist on object<PhpOffice\PhpSpreadsheet\Style>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        }
138
    }
139
140
    /**
141
     * Build style array from subcomponents.
142
     *
143
     * @param array $array
144
     *
145
     * @return array
146
     */
147 1
    public function getStyleArray($array)
148
    {
149 1
        return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
150
    }
151
152
    /**
153
     * Apply styles from array.
154
     *
155
     * <code>
156
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
157
     *        array(
158
     *            'borderStyle' => Border::BORDER_DASHDOT,
159
     *            'color' => array(
160
     *                'rgb' => '808080'
161
     *            )
162
     *        )
163
     * );
164
     * </code>
165
     *
166
     * @param array $pStyles Array containing style information
167
     *
168
     * @throws PhpSpreadsheetException
169
     *
170
     * @return Border
171
     */
172 18
    public function applyFromArray(array $pStyles)
173
    {
174 18
        if ($this->isSupervisor) {
175
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
176
        } else {
177 18
            if (isset($pStyles['borderStyle'])) {
178 18
                $this->setBorderStyle($pStyles['borderStyle']);
179
            }
180 18
            if (isset($pStyles['color'])) {
181 14
                $this->getColor()->applyFromArray($pStyles['color']);
182
            }
183
        }
184
185 18
        return $this;
186
    }
187
188
    /**
189
     * Get Border style.
190
     *
191
     * @return string
192
     */
193 63
    public function getBorderStyle()
194
    {
195 63
        if ($this->isSupervisor) {
196 1
            return $this->getSharedComponent()->getBorderStyle();
197
        }
198
199 63
        return $this->borderStyle;
200
    }
201
202
    /**
203
     * Set Border style.
204
     *
205
     * @param string|bool $pValue
206
     *                            When passing a boolean, FALSE equates Border::BORDER_NONE
207
     *                                and TRUE to Border::BORDER_MEDIUM
208
     *
209
     * @return Border
210
     */
211 21 View Code Duplication
    public function setBorderStyle($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...
212
    {
213 21
        if (empty($pValue)) {
214
            $pValue = self::BORDER_NONE;
215 21
        } elseif (is_bool($pValue) && $pValue) {
216
            $pValue = self::BORDER_MEDIUM;
217
        }
218 21
        if ($this->isSupervisor) {
219 1
            $styleArray = $this->getStyleArray(['borderStyle' => $pValue]);
220 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
221
        } else {
222 21
            $this->borderStyle = $pValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pValue can also be of type boolean. However, the property $borderStyle is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
223
        }
224
225 21
        return $this;
226
    }
227
228
    /**
229
     * Get Border Color.
230
     *
231
     * @return Color
232
     */
233 47
    public function getColor()
234
    {
235 47
        return $this->color;
236
    }
237
238
    /**
239
     * Set Border Color.
240
     *
241
     * @param Color $pValue
242
     *
243
     * @throws PhpSpreadsheetException
244
     *
245
     * @return Border
246
     */
247 View Code Duplication
    public function setColor(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
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
251
252
        if ($this->isSupervisor) {
253
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
254
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
255
        } else {
256
            $this->color = $color;
257
        }
258
259
        return $this;
260
    }
261
262
    /**
263
     * Get hash code.
264
     *
265
     * @return string Hash code
266
     */
267 70
    public function getHashCode()
268
    {
269 70
        if ($this->isSupervisor) {
270
            return $this->getSharedComponent()->getHashCode();
271
        }
272
273 70
        return md5(
274 70
            $this->borderStyle .
275 70
            $this->color->getHashCode() .
276 70
            __CLASS__
277
        );
278
    }
279
}
280