Passed
Push — develop ( eb3189...b05d07 )
by Adrien
24:31 queued 01:04
created

Border::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
7
class Border extends Supervisor
8
{
9
    // Border style
10
    const BORDER_NONE = 'none';
11
    const BORDER_DASHDOT = 'dashDot';
12
    const BORDER_DASHDOTDOT = 'dashDotDot';
13
    const BORDER_DASHED = 'dashed';
14
    const BORDER_DOTTED = 'dotted';
15
    const BORDER_DOUBLE = 'double';
16
    const BORDER_HAIR = 'hair';
17
    const BORDER_MEDIUM = 'medium';
18
    const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
19
    const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
20
    const BORDER_MEDIUMDASHED = 'mediumDashed';
21
    const BORDER_SLANTDASHDOT = 'slantDashDot';
22
    const BORDER_THICK = 'thick';
23
    const BORDER_THIN = 'thin';
24
25
    /**
26
     * Border style.
27
     *
28
     * @var string
29
     */
30
    protected $borderStyle = self::BORDER_NONE;
31
32
    /**
33
     * Border color.
34
     *
35
     * @var Color
36
     */
37
    protected $color;
38
39
    /**
40
     * @var int
41
     */
42
    public $colorIndex;
43
44
    /**
45
     * Create a new Border.
46
     *
47
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
48
     *                                    Leave this value at default unless you understand exactly what
49
     *                                        its ramifications are
50
     * @param bool $isConditional Flag indicating if this is a conditional style or not
51
     *                                    Leave this value at default unless you understand exactly what
52
     *                                        its ramifications are
53
     */
54 185
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function __construct($isSupervisor = false, /** @scrutinizer ignore-unused */ $isConditional = false)

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

Loading history...
55
    {
56
        // Supervisor?
57 185
        parent::__construct($isSupervisor);
58
59
        // Initialise values
60 185
        $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
61
62
        // bind parent if we are a supervisor
63 185
        if ($isSupervisor) {
64 185
            $this->color->bindParent($this, 'color');
0 ignored issues
show
Bug introduced by
$this of type PhpOffice\PhpSpreadsheet\Style\Border is incompatible with the type PhpOffice\PhpSpreadsheet...Spreadsheet\Spreadsheet expected by parameter $parent of PhpOffice\PhpSpreadsheet...upervisor::bindParent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $this->color->bindParent(/** @scrutinizer ignore-type */ $this, 'color');
Loading history...
65
        }
66 185
    }
67
68
    /**
69
     * Get the shared style component for the currently active cell in currently active sheet.
70
     * Only used for style supervisor.
71
     *
72
     * @throws PhpSpreadsheetException
73
     *
74
     * @return Border
75
     */
76 2
    public function getSharedComponent()
77
    {
78 2
        switch ($this->parentPropertyName) {
79 2
            case 'allBorders':
80 2
            case 'horizontal':
81 2
            case 'inside':
82 2
            case 'outline':
83 2
            case 'vertical':
84
                throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
85
86
                break;
87 2
            case 'bottom':
88 2
                return $this->parent->getSharedComponent()->getBottom();
1 ignored issue
show
Bug introduced by
The method getSharedComponent() does not exist on PhpOffice\PhpSpreadsheet\Spreadsheet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                return $this->parent->/** @scrutinizer ignore-call */ getSharedComponent()->getBottom();

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...
Bug introduced by
The method getBottom() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getBottom();

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...
89 1
            case 'diagonal':
90
                return $this->parent->getSharedComponent()->getDiagonal();
0 ignored issues
show
Bug introduced by
The method getDiagonal() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getDiagonal();

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...
91 1
            case 'left':
92 1
                return $this->parent->getSharedComponent()->getLeft();
0 ignored issues
show
Bug introduced by
The method getLeft() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getLeft();

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...
93 1
            case 'right':
94 1
                return $this->parent->getSharedComponent()->getRight();
0 ignored issues
show
Bug introduced by
The method getRight() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getRight();

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...
95 1
            case 'top':
96 1
                return $this->parent->getSharedComponent()->getTop();
0 ignored issues
show
Bug introduced by
The method getTop() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getTop();

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...
97
        }
98
    }
99
100
    /**
101
     * Build style array from subcomponents.
102
     *
103
     * @param array $array
104
     *
105
     * @return array
106
     */
107 3
    public function getStyleArray($array)
108
    {
109 3
        return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
1 ignored issue
show
Bug introduced by
The method getStyleArray() does not exist on PhpOffice\PhpSpreadsheet\Spreadsheet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return $this->parent->/** @scrutinizer ignore-call */ getStyleArray([$this->parentPropertyName => $array]);

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...
110
    }
111
112
    /**
113
     * Apply styles from array.
114
     *
115
     * <code>
116
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
117
     *        [
118
     *            'borderStyle' => Border::BORDER_DASHDOT,
119
     *            'color' => [
120
     *                'rgb' => '808080'
121
     *            ]
122
     *        ]
123
     * );
124
     * </code>
125
     *
126
     * @param array $pStyles Array containing style information
127
     *
128
     * @throws PhpSpreadsheetException
129
     *
130
     * @return Border
131
     */
132 21
    public function applyFromArray(array $pStyles)
133
    {
134 21
        if ($this->isSupervisor) {
135
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
136
        } else {
137 21
            if (isset($pStyles['borderStyle'])) {
138 21
                $this->setBorderStyle($pStyles['borderStyle']);
139
            }
140 21
            if (isset($pStyles['color'])) {
141 16
                $this->getColor()->applyFromArray($pStyles['color']);
142
            }
143
        }
144
145 21
        return $this;
146
    }
147
148
    /**
149
     * Get Border style.
150
     *
151
     * @return string
152
     */
153 99
    public function getBorderStyle()
154
    {
155 99
        if ($this->isSupervisor) {
156 2
            return $this->getSharedComponent()->getBorderStyle();
157
        }
158
159 99
        return $this->borderStyle;
160
    }
161
162
    /**
163
     * Set Border style.
164
     *
165
     * @param bool|string $pValue
166
     *                            When passing a boolean, FALSE equates Border::BORDER_NONE
167
     *                                and TRUE to Border::BORDER_MEDIUM
168
     *
169
     * @return Border
170
     */
171 42
    public function setBorderStyle($pValue)
172
    {
173 42
        if (empty($pValue)) {
174
            $pValue = self::BORDER_NONE;
175 42
        } elseif (is_bool($pValue) && $pValue) {
176
            $pValue = self::BORDER_MEDIUM;
177
        }
178 42
        if ($this->isSupervisor) {
179 3
            $styleArray = $this->getStyleArray(['borderStyle' => $pValue]);
180 3
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
181
        } else {
182 42
            $this->borderStyle = $pValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pValue can also be of type false. 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...
183
        }
184
185 42
        return $this;
186
    }
187
188
    /**
189
     * Get Border Color.
190
     *
191
     * @return Color
192
     */
193 75
    public function getColor()
194
    {
195 75
        return $this->color;
196
    }
197
198
    /**
199
     * Set Border Color.
200
     *
201
     * @param Color $pValue
202
     *
203
     * @throws PhpSpreadsheetException
204
     *
205
     * @return Border
206
     */
207 1
    public function setColor(Color $pValue)
208
    {
209
        // make sure parameter is a real color and not a supervisor
210 1
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
211
212 1
        if ($this->isSupervisor) {
213 1
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
214 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
215
        } else {
216
            $this->color = $color;
217
        }
218
219 1
        return $this;
220
    }
221
222
    /**
223
     * Get hash code.
224
     *
225
     * @return string Hash code
226
     */
227 106
    public function getHashCode()
228
    {
229 106
        if ($this->isSupervisor) {
230
            return $this->getSharedComponent()->getHashCode();
231
        }
232
233 106
        return md5(
234 106
            $this->borderStyle .
235 106
            $this->color->getHashCode() .
236 106
            __CLASS__
237
        );
238
    }
239
}
240