Completed
Push — develop ( 268fc1...a06731 )
by Adrien
23:59
created

Chart::chartDataSeriesValuesMultiLevel()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 35
Code Lines 26

Duplication

Lines 20
Ratio 57.14 %

Code Coverage

Tests 16
CRAP Score 9.8033

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 8
nop 2
dl 20
loc 35
ccs 16
cts 23
cp 0.6957
crap 9.8033
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
7
/**
8
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
 *
24
 * @category    PhpSpreadsheet
25
 * @copyright   Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
26
 * @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
27
 */
28
class Chart
29
{
30
    /**
31
     * @param \SimpleXMLElement $component
32
     * @param string $name
33
     * @param string $format
34
     */
35 1
    private static function getAttribute(\SimpleXMLElement $component, $name, $format)
36
    {
37 1
        $attributes = $component->attributes();
38 1
        if (isset($attributes[$name])) {
39 1
            if ($format == 'string') {
40 1
                return (string) $attributes[$name];
41 1
            } elseif ($format == 'integer') {
42 1
                return (integer) $attributes[$name];
43 1
            } elseif ($format == 'boolean') {
44 1
                return (boolean) ($attributes[$name] === '0' || $attributes[$name] !== 'true') ? false : true;
45
            } else {
46
                return (float) $attributes[$name];
47
            }
48
        }
49
50 1
        return null;
51
    }
52
53
    private static function readColor($color, $background = false)
54
    {
55
        if (isset($color['rgb'])) {
56
            return (string) $color['rgb'];
57
        } elseif (isset($color['indexed'])) {
58
            return \PhpOffice\PhpSpreadsheet\Style\Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
59
        }
60
    }
61
62
    /**
63
     * @param \SimpleXMLElement $chartElements
64
     * @param string $chartName
65
     */
66 1
    public static function readChart(\SimpleXMLElement $chartElements, $chartName)
67
    {
68 1
        $namespacesChartMeta = $chartElements->getNamespaces(true);
69 1
        $chartElementsC = $chartElements->children($namespacesChartMeta['c']);
70
71 1
        $XaxisLabel = $YaxisLabel = $legend = $title = null;
72 1
        $dispBlanksAs = $plotVisOnly = null;
73
74 1
        foreach ($chartElementsC as $chartElementKey => $chartElement) {
75
            switch ($chartElementKey) {
76 1
                case 'chart':
77 1
                    foreach ($chartElement as $chartDetailsKey => $chartDetails) {
78 1
                        $chartDetailsC = $chartDetails->children($namespacesChartMeta['c']);
0 ignored issues
show
Unused Code introduced by
$chartDetailsC is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
                        switch ($chartDetailsKey) {
80 1
                            case 'plotArea':
81 1
                                $plotAreaLayout = $XaxisLable = $YaxisLable = null;
0 ignored issues
show
Unused Code introduced by
$YaxisLable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$XaxisLable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82 1
                                $plotSeries = $plotAttributes = [];
83 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
84
                                    switch ($chartDetailKey) {
85 1
                                        case 'layout':
86 1
                                            $plotAreaLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta, 'plotArea');
0 ignored issues
show
Unused Code introduced by
The call to Chart::chartLayoutDetails() has too many arguments starting with 'plotArea'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
87 1
                                            break;
88 1 View Code Duplication
                                        case 'catAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89 1
                                            if (isset($chartDetail->title)) {
90 1
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
91
                                            }
92 1
                                            break;
93 1 View Code Duplication
                                        case 'dateAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
                                            if (isset($chartDetail->title)) {
95
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
96
                                            }
97
                                            break;
98 1 View Code Duplication
                                        case 'valAx':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99 1
                                            if (isset($chartDetail->title)) {
100 1
                                                $YaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
101
                                            }
102 1
                                            break;
103 1
                                        case 'barChart':
104 1
                                        case 'bar3DChart':
105 1
                                            $barDirection = self::getAttribute($chartDetail->barDir, 'val', 'string');
106 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
107 1
                                            $plotSer->setPlotDirection($barDirection);
108 1
                                            $plotSeries[] = $plotSer;
109 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
110 1
                                            break;
111 1
                                        case 'lineChart':
112 1 View Code Duplication
                                        case 'line3DChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
114 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
115 1
                                            break;
116 1
                                        case 'areaChart':
117 1 View Code Duplication
                                        case 'area3DChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
119 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
120 1
                                            break;
121 1
                                        case 'doughnutChart':
122 1
                                        case 'pieChart':
123 1
                                        case 'pie3DChart':
124 1
                                            $explosion = isset($chartDetail->ser->explosion);
125 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
126 1
                                            $plotSer->setPlotStyle($explosion);
0 ignored issues
show
Documentation introduced by
$explosion is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127 1
                                            $plotSeries[] = $plotSer;
128 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
129 1
                                            break;
130 1 View Code Duplication
                                        case 'scatterChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131 1
                                            $scatterStyle = self::getAttribute($chartDetail->scatterStyle, 'val', 'string');
132 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
133 1
                                            $plotSer->setPlotStyle($scatterStyle);
134 1
                                            $plotSeries[] = $plotSer;
135 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
136 1
                                            break;
137 1
                                        case 'bubbleChart':
138 1
                                            $bubbleScale = self::getAttribute($chartDetail->bubbleScale, 'val', 'integer');
139 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
140 1
                                            $plotSer->setPlotStyle($bubbleScale);
141 1
                                            $plotSeries[] = $plotSer;
142 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
143 1
                                            break;
144 1 View Code Duplication
                                        case 'radarChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
145 1
                                            $radarStyle = self::getAttribute($chartDetail->radarStyle, 'val', 'string');
146 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
147 1
                                            $plotSer->setPlotStyle($radarStyle);
148 1
                                            $plotSeries[] = $plotSer;
149 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
150 1
                                            break;
151 1
                                        case 'surfaceChart':
152 1
                                        case 'surface3DChart':
153 1
                                            $wireFrame = self::getAttribute($chartDetail->wireframe, 'val', 'boolean');
154 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
155 1
                                            $plotSer->setPlotStyle($wireFrame);
156 1
                                            $plotSeries[] = $plotSer;
157 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
158 1
                                            break;
159 1 View Code Duplication
                                        case 'stockChart':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
160 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
161 1
                                            $plotAttributes = self::readChartAttributes($plotAreaLayout);
162 1
                                            break;
163
                                    }
164
                                }
165 1
                                if ($plotAreaLayout == null) {
166 1
                                    $plotAreaLayout = new \PhpOffice\PhpSpreadsheet\Chart\Layout();
167
                                }
168 1
                                $plotArea = new \PhpOffice\PhpSpreadsheet\Chart\PlotArea($plotAreaLayout, $plotSeries);
169 1
                                self::setChartAttributes($plotAreaLayout, $plotAttributes);
170 1
                                break;
171 1
                            case 'plotVisOnly':
172 1
                                $plotVisOnly = self::getAttribute($chartDetails, 'val', 'string');
173 1
                                break;
174 1
                            case 'dispBlanksAs':
175 1
                                $dispBlanksAs = self::getAttribute($chartDetails, 'val', 'string');
176 1
                                break;
177 1
                            case 'title':
178 1
                                $title = self::chartTitle($chartDetails, $namespacesChartMeta, 'title');
179 1
                                break;
180 1
                            case 'legend':
181 1
                                $legendPos = 'r';
182 1
                                $legendLayout = null;
183 1
                                $legendOverlay = false;
184 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
185
                                    switch ($chartDetailKey) {
186 1
                                        case 'legendPos':
187 1
                                            $legendPos = self::getAttribute($chartDetail, 'val', 'string');
188 1
                                            break;
189 1
                                        case 'overlay':
190 1
                                            $legendOverlay = self::getAttribute($chartDetail, 'val', 'boolean');
191 1
                                            break;
192 1
                                        case 'layout':
193 1
                                            $legendLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta, 'legend');
0 ignored issues
show
Unused Code introduced by
The call to Chart::chartLayoutDetails() has too many arguments starting with 'legend'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
194 1
                                            break;
195
                                    }
196
                                }
197 1
                                $legend = new \PhpOffice\PhpSpreadsheet\Chart\Legend($legendPos, $legendLayout, $legendOverlay);
198 1
                                break;
199
                        }
200
                    }
201
            }
202
        }
203 1
        $chart = new \PhpOffice\PhpSpreadsheet\Chart($chartName, $title, $legend, $plotArea, $plotVisOnly, $dispBlanksAs, $XaxisLabel, $YaxisLabel);
0 ignored issues
show
Bug introduced by
The variable $plotArea does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
204
205 1
        return $chart;
206
    }
207
208 1
    private static function chartTitle($titleDetails, $namespacesChartMeta, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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...
209
    {
210 1
        $caption = [];
211 1
        $titleLayout = null;
212 1
        foreach ($titleDetails as $titleDetailKey => $chartDetail) {
213
            switch ($titleDetailKey) {
214 1
                case 'tx':
215 1
                    $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
216 1
                    foreach ($titleDetails as $titleKey => $titleDetail) {
217
                        switch ($titleKey) {
218 1
                            case 'p':
219 1
                                $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
220 1
                                $caption[] = self::parseRichText($titleDetailPart);
221
                        }
222
                    }
223 1
                    break;
224 1
                case 'layout':
225 1
                    $titleLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
226 1
                    break;
227
            }
228
        }
229
230 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Title($caption, $titleLayout);
231
    }
232
233 1
    private static function chartLayoutDetails($chartDetail, $namespacesChartMeta)
234
    {
235 1
        if (!isset($chartDetail->manualLayout)) {
236 1
            return null;
237
        }
238 1
        $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
239 1
        if (is_null($details)) {
240
            return null;
241
        }
242 1
        $layout = [];
243 1
        foreach ($details as $detailKey => $detail) {
244 1
            $layout[$detailKey] = self::getAttribute($detail, 'val', 'string');
245
        }
246
247 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Layout($layout);
248
    }
249
250 1
    private static function chartDataSeries($chartDetail, $namespacesChartMeta, $plotType)
251
    {
252 1
        $multiSeriesType = null;
253 1
        $smoothLine = false;
254 1
        $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = [];
255
256 1
        $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
257 1
        foreach ($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
258
            switch ($seriesDetailKey) {
259 1
                case 'grouping':
260 1
                    $multiSeriesType = self::getAttribute($chartDetail->grouping, 'val', 'string');
261 1
                    break;
262 1
                case 'ser':
263 1
                    $marker = null;
264 1
                    foreach ($seriesDetails as $seriesKey => $seriesDetail) {
265
                        switch ($seriesKey) {
266 1
                            case 'idx':
267 1
                                $seriesIndex = self::getAttribute($seriesDetail, 'val', 'integer');
268 1
                                break;
269 1
                            case 'order':
270 1
                                $seriesOrder = self::getAttribute($seriesDetail, 'val', 'integer');
271 1
                                $plotOrder[$seriesIndex] = $seriesOrder;
0 ignored issues
show
Bug introduced by
The variable $seriesIndex does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
272 1
                                break;
273 1
                            case 'tx':
274 1
                                $seriesLabel[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
275 1
                                break;
276 1
                            case 'marker':
277 1
                                $marker = self::getAttribute($seriesDetail->symbol, 'val', 'string');
278 1
                                break;
279 1
                            case 'smooth':
280 1
                                $smoothLine = self::getAttribute($seriesDetail, 'val', 'boolean');
281 1
                                break;
282 1
                            case 'cat':
283 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
284 1
                                break;
285 1
                            case 'val':
286 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
287 1
                                break;
288 1
                            case 'xVal':
289 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
290 1
                                break;
291 1
                            case 'yVal':
292 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
293 1
                                break;
294
                        }
295
                    }
296
            }
297
        }
298
299 1
        return new \PhpOffice\PhpSpreadsheet\Chart\DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $smoothLine);
300
    }
301
302 1
    private static function chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null, $smoothLine = false)
303
    {
304 1
        if (isset($seriesDetail->strRef)) {
305 1
            $seriesSource = (string) $seriesDetail->strRef->f;
306 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
307
308 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
309
        } elseif (isset($seriesDetail->numRef)) {
310 1
            $seriesSource = (string) $seriesDetail->numRef->f;
311 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
312
313 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
314
        } elseif (isset($seriesDetail->multiLvlStrRef)) {
315 1
            $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
316 1
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
317 1
            $seriesData['pointCount'] = count($seriesData['dataValues']);
318
319 1
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
320
        } elseif (isset($seriesDetail->multiLvlNumRef)) {
321
            $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
322
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
323
            $seriesData['pointCount'] = count($seriesData['dataValues']);
324
325
            return new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker, $smoothLine);
0 ignored issues
show
Unused Code introduced by
The call to DataSeriesValues::__construct() has too many arguments starting with $smoothLine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
326
        }
327
328
        return null;
329
    }
330
331 1
    private static function chartDataSeriesValues($seriesValueSet, $dataType = 'n')
332
    {
333 1
        $seriesVal = [];
334 1
        $formatCode = '';
335 1
        $pointCount = 0;
336
337 1 View Code Duplication
        foreach ($seriesValueSet as $seriesValueIdx => $seriesValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
338
            switch ($seriesValueIdx) {
339 1
                case 'ptCount':
340 1
                    $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
341 1
                    break;
342 1
                case 'formatCode':
343 1
                    $formatCode = (string) $seriesValue;
344 1
                    break;
345 1
                case 'pt':
346 1
                    $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
347 1
                    if ($dataType == 's') {
348 1
                        $seriesVal[$pointVal] = (string) $seriesValue->v;
349 1
                    } elseif ($seriesValue->v === Functions::NA()) {
350
                        $seriesVal[$pointVal] = null;
351
                    } else {
352 1
                        $seriesVal[$pointVal] = (float) $seriesValue->v;
353
                    }
354 1
                    break;
355
            }
356
        }
357
358
        return [
359 1
            'formatCode' => $formatCode,
360 1
            'pointCount' => $pointCount,
361 1
            'dataValues' => $seriesVal,
362
        ];
363
    }
364
365 1
    private static function chartDataSeriesValuesMultiLevel($seriesValueSet, $dataType = 'n')
366
    {
367 1
        $seriesVal = [];
368 1
        $formatCode = '';
369 1
        $pointCount = 0;
370
371 1
        foreach ($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
372 1 View Code Duplication
            foreach ($seriesLevel as $seriesValueIdx => $seriesValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
373
                switch ($seriesValueIdx) {
374 1
                    case 'ptCount':
375
                        $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
376
                        break;
377 1
                    case 'formatCode':
378
                        $formatCode = (string) $seriesValue;
379
                        break;
380 1
                    case 'pt':
381 1
                        $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
382 1
                        if ($dataType == 's') {
383 1
                            $seriesVal[$pointVal][] = (string) $seriesValue->v;
384
                        } elseif ($seriesValue->v === Functions::NA()) {
385
                            $seriesVal[$pointVal] = null;
386
                        } else {
387
                            $seriesVal[$pointVal][] = (float) $seriesValue->v;
388
                        }
389 1
                        break;
390
                }
391
            }
392
        }
393
394
        return [
395 1
            'formatCode' => $formatCode,
396 1
            'pointCount' => $pointCount,
397 1
            'dataValues' => $seriesVal,
398
        ];
399
    }
400
401 1
    private static function parseRichText($titleDetailPart = null)
402
    {
403 1
        $value = new \PhpOffice\PhpSpreadsheet\RichText();
404
405 1
        foreach ($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
406 1
            if (isset($titleDetailElement->t)) {
407 1
                $objText = $value->createTextRun((string) $titleDetailElement->t);
408
            }
409 1
            if (isset($titleDetailElement->rPr)) {
410 1 View Code Duplication
                if (isset($titleDetailElement->rPr->rFont['val'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
411
                    $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont['val']);
0 ignored issues
show
Bug introduced by
The variable $objText does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
412
                }
413
414 1
                $fontSize = (self::getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
415 1
                if (!is_null($fontSize)) {
416 1
                    $objText->getFont()->setSize(floor($fontSize / 100));
417
                }
418
419 1
                $fontColor = (self::getAttribute($titleDetailElement->rPr, 'color', 'string'));
420 1
                if (!is_null($fontColor)) {
421
                    $objText->getFont()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color(self::readColor($fontColor)));
422
                }
423
424 1
                $bold = self::getAttribute($titleDetailElement->rPr, 'b', 'boolean');
425 1
                if (!is_null($bold)) {
426 1
                    $objText->getFont()->setBold($bold);
427
                }
428
429 1
                $italic = self::getAttribute($titleDetailElement->rPr, 'i', 'boolean');
430 1
                if (!is_null($italic)) {
431 1
                    $objText->getFont()->setItalic($italic);
432
                }
433
434 1
                $baseline = self::getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
435 1
                if (!is_null($baseline)) {
436 1
                    if ($baseline > 0) {
437 1
                        $objText->getFont()->setSuperScript(true);
438 1
                    } elseif ($baseline < 0) {
439 1
                        $objText->getFont()->setSubScript(true);
440
                    }
441
                }
442
443 1
                $underscore = (self::getAttribute($titleDetailElement->rPr, 'u', 'string'));
444 1
                if (!is_null($underscore)) {
445 1
                    if ($underscore == 'sng') {
446 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
447 1
                    } elseif ($underscore == 'dbl') {
448 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE);
449
                    } else {
450 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE);
451
                    }
452
                }
453
454 1
                $strikethrough = (self::getAttribute($titleDetailElement->rPr, 's', 'string'));
455 1
                if (!is_null($strikethrough)) {
456
                    if ($strikethrough == 'noStrike') {
457
                        $objText->getFont()->setStrikethrough(false);
458
                    } else {
459 1
                        $objText->getFont()->setStrikethrough(true);
460
                    }
461
                }
462
            }
463
        }
464
465 1
        return $value;
466
    }
467
468 1
    private static function readChartAttributes($chartDetail)
469
    {
470 1
        $plotAttributes = [];
471 1
        if (isset($chartDetail->dLbls)) {
472 1
            if (isset($chartDetail->dLbls->howLegendKey)) {
473
                $plotAttributes['showLegendKey'] = self::getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
474
            }
475 1 View Code Duplication
            if (isset($chartDetail->dLbls->showVal)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
476 1
                $plotAttributes['showVal'] = self::getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
477
            }
478 1 View Code Duplication
            if (isset($chartDetail->dLbls->showCatName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
479 1
                $plotAttributes['showCatName'] = self::getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
480
            }
481 1 View Code Duplication
            if (isset($chartDetail->dLbls->showSerName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
482 1
                $plotAttributes['showSerName'] = self::getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
483
            }
484 1 View Code Duplication
            if (isset($chartDetail->dLbls->showPercent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
485 1
                $plotAttributes['showPercent'] = self::getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
486
            }
487 1 View Code Duplication
            if (isset($chartDetail->dLbls->showBubbleSize)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
488 1
                $plotAttributes['showBubbleSize'] = self::getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
489
            }
490 1 View Code Duplication
            if (isset($chartDetail->dLbls->showLeaderLines)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
491 1
                $plotAttributes['showLeaderLines'] = self::getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
492
            }
493
        }
494
495 1
        return $plotAttributes;
496
    }
497
498
    /**
499
     * @param \PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea
500
     */
501 1
    private static function setChartAttributes(\PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea, $plotAttributes)
502
    {
503 1
        foreach ($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
504
            switch ($plotAttributeKey) {
505 1
                case 'showLegendKey':
506
                    $plotArea->setShowLegendKey($plotAttributeValue);
507
                    break;
508 1
                case 'showVal':
509 1
                    $plotArea->setShowVal($plotAttributeValue);
510 1
                    break;
511 1
                case 'showCatName':
512 1
                    $plotArea->setShowCatName($plotAttributeValue);
513 1
                    break;
514 1
                case 'showSerName':
515 1
                    $plotArea->setShowSerName($plotAttributeValue);
516 1
                    break;
517 1
                case 'showPercent':
518 1
                    $plotArea->setShowPercent($plotAttributeValue);
519 1
                    break;
520 1
                case 'showBubbleSize':
521 1
                    $plotArea->setShowBubbleSize($plotAttributeValue);
522 1
                    break;
523 1
                case 'showLeaderLines':
524 1
                    $plotArea->setShowLeaderLines($plotAttributeValue);
525 1
                    break;
526
            }
527
        }
528 1
    }
529
}
530