Completed
Push — develop ( 2b41bd...39b55d )
by Adrien
22:48
created

Chart::readChart()   D

Complexity

Conditions 37
Paths 60

Size

Total Lines 141
Code Lines 124

Duplication

Lines 41
Ratio 29.08 %

Code Coverage

Tests 117
CRAP Score 37.0212

Importance

Changes 0
Metric Value
cc 37
eloc 124
nc 60
nop 2
dl 41
loc 141
ccs 117
cts 120
cp 0.975
crap 37.0212
rs 4.2495
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 * @category    PhpSpreadsheet
23
 * @copyright   Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 * @version     ##VERSION##, ##DATE##
26
 */
27
class Chart
28
{
29
    /**
30
     * @param \SimpleXMLElement $component
31
     * @param string $name
32
     * @param string $format
33
     */
34 1
    private static function getAttribute(\SimpleXMLElement $component, $name, $format)
35
    {
36 1
        $attributes = $component->attributes();
37 1
        if (isset($attributes[$name])) {
38 1
            if ($format == 'string') {
39 1
                return (string) $attributes[$name];
40 1
            } elseif ($format == 'integer') {
41 1
                return (integer) $attributes[$name];
42 1
            } elseif ($format == 'boolean') {
43 1
                return (boolean) ($attributes[$name] === '0' || $attributes[$name] !== 'true') ? false : true;
44
            } else {
45
                return (float) $attributes[$name];
46
            }
47
        }
48
49 1
        return null;
50
    }
51
52
    private static function readColor($color, $background = false)
53
    {
54
        if (isset($color['rgb'])) {
55
            return (string) $color['rgb'];
56
        } elseif (isset($color['indexed'])) {
57
            return \PhpOffice\PhpSpreadsheet\Style\Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
58
        }
59
    }
60
61
    /**
62
     * @param \SimpleXMLElement $chartElements
63
     * @param string $chartName
64
     */
65 1
    public static function readChart(\SimpleXMLElement $chartElements, $chartName)
66
    {
67 1
        $namespacesChartMeta = $chartElements->getNamespaces(true);
68 1
        $chartElementsC = $chartElements->children($namespacesChartMeta['c']);
69
70 1
        $XaxisLabel = $YaxisLabel = $legend = $title = null;
71 1
        $dispBlanksAs = $plotVisOnly = null;
72
73 1
        foreach ($chartElementsC as $chartElementKey => $chartElement) {
74
            switch ($chartElementKey) {
75 1
                case 'chart':
76 1
                    foreach ($chartElement as $chartDetailsKey => $chartDetails) {
77 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...
78
                        switch ($chartDetailsKey) {
79 1
                            case 'plotArea':
80 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...
81 1
                                $plotSeries = $plotAttributes = [];
82 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
83
                                    switch ($chartDetailKey) {
84 1
                                        case 'layout':
85 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...
86 1
                                            break;
87 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...
88 1
                                            if (isset($chartDetail->title)) {
89 1
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
90
                                            }
91 1
                                            break;
92 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...
93
                                            if (isset($chartDetail->title)) {
94
                                                $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
95
                                            }
96
                                            break;
97 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...
98 1
                                            if (isset($chartDetail->title)) {
99 1
                                                $YaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta, 'cat');
100
                                            }
101 1
                                            break;
102 1
                                        case 'barChart':
103 1
                                        case 'bar3DChart':
104 1
                                            $barDirection = self::getAttribute($chartDetail->barDir, 'val', 'string');
105 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
106 1
                                            $plotSer->setPlotDirection($barDirection);
107 1
                                            $plotSeries[] = $plotSer;
108 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
109 1
                                            break;
110 1
                                        case 'lineChart':
111 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...
112 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
113 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
114 1
                                            break;
115 1
                                        case 'areaChart':
116 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...
117 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
118 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
119 1
                                            break;
120 1
                                        case 'doughnutChart':
121 1
                                        case 'pieChart':
122 1
                                        case 'pie3DChart':
123 1
                                            $explosion = isset($chartDetail->ser->explosion);
124 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
125 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...
126 1
                                            $plotSeries[] = $plotSer;
127 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
128 1
                                            break;
129 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...
130 1
                                            $scatterStyle = self::getAttribute($chartDetail->scatterStyle, 'val', 'string');
131 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
132 1
                                            $plotSer->setPlotStyle($scatterStyle);
133 1
                                            $plotSeries[] = $plotSer;
134 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
135 1
                                            break;
136 1
                                        case 'bubbleChart':
137 1
                                            $bubbleScale = self::getAttribute($chartDetail->bubbleScale, 'val', 'integer');
138 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
139 1
                                            $plotSer->setPlotStyle($bubbleScale);
140 1
                                            $plotSeries[] = $plotSer;
141 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
142 1
                                            break;
143 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...
144 1
                                            $radarStyle = self::getAttribute($chartDetail->radarStyle, 'val', 'string');
145 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
146 1
                                            $plotSer->setPlotStyle($radarStyle);
147 1
                                            $plotSeries[] = $plotSer;
148 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
149 1
                                            break;
150 1
                                        case 'surfaceChart':
151 1
                                        case 'surface3DChart':
152 1
                                            $wireFrame = self::getAttribute($chartDetail->wireframe, 'val', 'boolean');
153 1
                                            $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
154 1
                                            $plotSer->setPlotStyle($wireFrame);
155 1
                                            $plotSeries[] = $plotSer;
156 1
                                            $plotAttributes = self::readChartAttributes($chartDetail);
157 1
                                            break;
158 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...
159 1
                                            $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
160 1
                                            $plotAttributes = self::readChartAttributes($plotAreaLayout);
161 1
                                            break;
162
                                    }
163
                                }
164 1
                                if ($plotAreaLayout == null) {
165 1
                                    $plotAreaLayout = new \PhpOffice\PhpSpreadsheet\Chart\Layout();
166
                                }
167 1
                                $plotArea = new \PhpOffice\PhpSpreadsheet\Chart\PlotArea($plotAreaLayout, $plotSeries);
168 1
                                self::setChartAttributes($plotAreaLayout, $plotAttributes);
169 1
                                break;
170 1
                            case 'plotVisOnly':
171 1
                                $plotVisOnly = self::getAttribute($chartDetails, 'val', 'string');
172 1
                                break;
173 1
                            case 'dispBlanksAs':
174 1
                                $dispBlanksAs = self::getAttribute($chartDetails, 'val', 'string');
175 1
                                break;
176 1
                            case 'title':
177 1
                                $title = self::chartTitle($chartDetails, $namespacesChartMeta, 'title');
178 1
                                break;
179 1
                            case 'legend':
180 1
                                $legendPos = 'r';
181 1
                                $legendLayout = null;
182 1
                                $legendOverlay = false;
183 1
                                foreach ($chartDetails as $chartDetailKey => $chartDetail) {
184
                                    switch ($chartDetailKey) {
185 1
                                        case 'legendPos':
186 1
                                            $legendPos = self::getAttribute($chartDetail, 'val', 'string');
187 1
                                            break;
188 1
                                        case 'overlay':
189 1
                                            $legendOverlay = self::getAttribute($chartDetail, 'val', 'boolean');
190 1
                                            break;
191 1
                                        case 'layout':
192 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...
193 1
                                            break;
194
                                    }
195
                                }
196 1
                                $legend = new \PhpOffice\PhpSpreadsheet\Chart\Legend($legendPos, $legendLayout, $legendOverlay);
197 1
                                break;
198
                        }
199
                    }
200
            }
201
        }
202 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...
203
204 1
        return $chart;
205
    }
206
207 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...
208
    {
209 1
        $caption = [];
210 1
        $titleLayout = null;
211 1
        foreach ($titleDetails as $titleDetailKey => $chartDetail) {
212
            switch ($titleDetailKey) {
213 1
                case 'tx':
214 1
                    $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
215 1
                    foreach ($titleDetails as $titleKey => $titleDetail) {
216
                        switch ($titleKey) {
217 1
                            case 'p':
218 1
                                $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
219 1
                                $caption[] = self::parseRichText($titleDetailPart);
220
                        }
221
                    }
222 1
                    break;
223 1
                case 'layout':
224 1
                    $titleLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
225 1
                    break;
226
            }
227
        }
228
229 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Title($caption, $titleLayout);
230
    }
231
232 1
    private static function chartLayoutDetails($chartDetail, $namespacesChartMeta)
233
    {
234 1
        if (!isset($chartDetail->manualLayout)) {
235 1
            return null;
236
        }
237 1
        $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
238 1
        if (is_null($details)) {
239
            return null;
240
        }
241 1
        $layout = [];
242 1
        foreach ($details as $detailKey => $detail) {
243 1
            $layout[$detailKey] = self::getAttribute($detail, 'val', 'string');
244
        }
245
246 1
        return new \PhpOffice\PhpSpreadsheet\Chart\Layout($layout);
247
    }
248
249 1
    private static function chartDataSeries($chartDetail, $namespacesChartMeta, $plotType)
250
    {
251 1
        $multiSeriesType = null;
252 1
        $smoothLine = false;
253 1
        $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = [];
254
255 1
        $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
256 1
        foreach ($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
257
            switch ($seriesDetailKey) {
258 1
                case 'grouping':
259 1
                    $multiSeriesType = self::getAttribute($chartDetail->grouping, 'val', 'string');
260 1
                    break;
261 1
                case 'ser':
262 1
                    $marker = null;
263 1
                    foreach ($seriesDetails as $seriesKey => $seriesDetail) {
264
                        switch ($seriesKey) {
265 1
                            case 'idx':
266 1
                                $seriesIndex = self::getAttribute($seriesDetail, 'val', 'integer');
267 1
                                break;
268 1
                            case 'order':
269 1
                                $seriesOrder = self::getAttribute($seriesDetail, 'val', 'integer');
270 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...
271 1
                                break;
272 1
                            case 'tx':
273 1
                                $seriesLabel[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
274 1
                                break;
275 1
                            case 'marker':
276 1
                                $marker = self::getAttribute($seriesDetail->symbol, 'val', 'string');
277 1
                                break;
278 1
                            case 'smooth':
279 1
                                $smoothLine = self::getAttribute($seriesDetail, 'val', 'boolean');
280 1
                                break;
281 1
                            case 'cat':
282 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
283 1
                                break;
284 1
                            case 'val':
285 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
286 1
                                break;
287 1
                            case 'xVal':
288 1
                                $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
289 1
                                break;
290 1
                            case 'yVal':
291 1
                                $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
292 1
                                break;
293
                        }
294
                    }
295
            }
296
        }
297
298 1
        return new \PhpOffice\PhpSpreadsheet\Chart\DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $smoothLine);
299
    }
300
301 1
    private static function chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null, $smoothLine = false)
302
    {
303 1
        if (isset($seriesDetail->strRef)) {
304 1
            $seriesSource = (string) $seriesDetail->strRef->f;
305 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
306
307 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...
308
        } elseif (isset($seriesDetail->numRef)) {
309 1
            $seriesSource = (string) $seriesDetail->numRef->f;
310 1
            $seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
311
312 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...
313
        } elseif (isset($seriesDetail->multiLvlStrRef)) {
314 1
            $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
315 1
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
316 1
            $seriesData['pointCount'] = count($seriesData['dataValues']);
317
318 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...
319
        } elseif (isset($seriesDetail->multiLvlNumRef)) {
320
            $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
321
            $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
322
            $seriesData['pointCount'] = count($seriesData['dataValues']);
323
324
            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...
325
        }
326
327
        return null;
328
    }
329
330 1
    private static function chartDataSeriesValues($seriesValueSet, $dataType = 'n')
331
    {
332 1
        $seriesVal = [];
333 1
        $formatCode = '';
334 1
        $pointCount = 0;
335
336 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...
337
            switch ($seriesValueIdx) {
338 1
                case 'ptCount':
339 1
                    $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
340 1
                    break;
341 1
                case 'formatCode':
342 1
                    $formatCode = (string) $seriesValue;
343 1
                    break;
344 1
                case 'pt':
345 1
                    $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
346 1
                    if ($dataType == 's') {
347 1
                        $seriesVal[$pointVal] = (string) $seriesValue->v;
348
                    } else {
349 1
                        $seriesVal[$pointVal] = (float) $seriesValue->v;
350
                    }
351 1
                    break;
352
            }
353
        }
354
355
        return [
356 1
            'formatCode' => $formatCode,
357 1
            'pointCount' => $pointCount,
358 1
            'dataValues' => $seriesVal,
359
        ];
360
    }
361
362 1
    private static function chartDataSeriesValuesMultiLevel($seriesValueSet, $dataType = 'n')
363
    {
364 1
        $seriesVal = [];
365 1
        $formatCode = '';
366 1
        $pointCount = 0;
367
368 1
        foreach ($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
369 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...
370
                switch ($seriesValueIdx) {
371 1
                    case 'ptCount':
372
                        $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
373
                        break;
374 1
                    case 'formatCode':
375
                        $formatCode = (string) $seriesValue;
376
                        break;
377 1
                    case 'pt':
378 1
                        $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
379 1
                        if ($dataType == 's') {
380 1
                            $seriesVal[$pointVal][] = (string) $seriesValue->v;
381
                        } else {
382
                            $seriesVal[$pointVal][] = (float) $seriesValue->v;
383
                        }
384 1
                        break;
385
                }
386
            }
387
        }
388
389
        return [
390 1
            'formatCode' => $formatCode,
391 1
            'pointCount' => $pointCount,
392 1
            'dataValues' => $seriesVal,
393
        ];
394
    }
395
396 1
    private static function parseRichText($titleDetailPart = null)
397
    {
398 1
        $value = new \PhpOffice\PhpSpreadsheet\RichText();
399
400 1
        foreach ($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
401 1
            if (isset($titleDetailElement->t)) {
402 1
                $objText = $value->createTextRun((string) $titleDetailElement->t);
403
            }
404 1
            if (isset($titleDetailElement->rPr)) {
405 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...
406
                    $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...
407
                }
408
409 1
                $fontSize = (self::getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
410 1
                if (!is_null($fontSize)) {
411 1
                    $objText->getFont()->setSize(floor($fontSize / 100));
412
                }
413
414 1
                $fontColor = (self::getAttribute($titleDetailElement->rPr, 'color', 'string'));
415 1
                if (!is_null($fontColor)) {
416
                    $objText->getFont()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color(self::readColor($fontColor)));
417
                }
418
419 1
                $bold = self::getAttribute($titleDetailElement->rPr, 'b', 'boolean');
420 1
                if (!is_null($bold)) {
421 1
                    $objText->getFont()->setBold($bold);
422
                }
423
424 1
                $italic = self::getAttribute($titleDetailElement->rPr, 'i', 'boolean');
425 1
                if (!is_null($italic)) {
426 1
                    $objText->getFont()->setItalic($italic);
427
                }
428
429 1
                $baseline = self::getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
430 1
                if (!is_null($baseline)) {
431 1
                    if ($baseline > 0) {
432 1
                        $objText->getFont()->setSuperScript(true);
433 1
                    } elseif ($baseline < 0) {
434 1
                        $objText->getFont()->setSubScript(true);
435
                    }
436
                }
437
438 1
                $underscore = (self::getAttribute($titleDetailElement->rPr, 'u', 'string'));
439 1
                if (!is_null($underscore)) {
440 1
                    if ($underscore == 'sng') {
441 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
442 1
                    } elseif ($underscore == 'dbl') {
443 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE);
444
                    } else {
445 1
                        $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE);
446
                    }
447
                }
448
449 1
                $strikethrough = (self::getAttribute($titleDetailElement->rPr, 's', 'string'));
450 1
                if (!is_null($strikethrough)) {
451
                    if ($strikethrough == 'noStrike') {
452
                        $objText->getFont()->setStrikethrough(false);
453
                    } else {
454 1
                        $objText->getFont()->setStrikethrough(true);
455
                    }
456
                }
457
            }
458
        }
459
460 1
        return $value;
461
    }
462
463 1
    private static function readChartAttributes($chartDetail)
464
    {
465 1
        $plotAttributes = [];
466 1
        if (isset($chartDetail->dLbls)) {
467 1
            if (isset($chartDetail->dLbls->howLegendKey)) {
468
                $plotAttributes['showLegendKey'] = self::getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
469
            }
470 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...
471 1
                $plotAttributes['showVal'] = self::getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
472
            }
473 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...
474 1
                $plotAttributes['showCatName'] = self::getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
475
            }
476 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...
477 1
                $plotAttributes['showSerName'] = self::getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
478
            }
479 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...
480 1
                $plotAttributes['showPercent'] = self::getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
481
            }
482 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...
483 1
                $plotAttributes['showBubbleSize'] = self::getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
484
            }
485 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...
486 1
                $plotAttributes['showLeaderLines'] = self::getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
487
            }
488
        }
489
490 1
        return $plotAttributes;
491
    }
492
493
    /**
494
     * @param \PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea
495
     */
496 1
    private static function setChartAttributes(\PhpOffice\PhpSpreadsheet\Chart\Layout $plotArea, $plotAttributes)
497
    {
498 1
        foreach ($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
499
            switch ($plotAttributeKey) {
500 1
                case 'showLegendKey':
501
                    $plotArea->setShowLegendKey($plotAttributeValue);
502
                    break;
503 1
                case 'showVal':
504 1
                    $plotArea->setShowVal($plotAttributeValue);
505 1
                    break;
506 1
                case 'showCatName':
507 1
                    $plotArea->setShowCatName($plotAttributeValue);
508 1
                    break;
509 1
                case 'showSerName':
510 1
                    $plotArea->setShowSerName($plotAttributeValue);
511 1
                    break;
512 1
                case 'showPercent':
513 1
                    $plotArea->setShowPercent($plotAttributeValue);
514 1
                    break;
515 1
                case 'showBubbleSize':
516 1
                    $plotArea->setShowBubbleSize($plotAttributeValue);
517 1
                    break;
518 1
                case 'showLeaderLines':
519 1
                    $plotArea->setShowLeaderLines($plotAttributeValue);
520 1
                    break;
521
            }
522
        }
523 1
    }
524
}
525