Completed
Push — develop ( 8cf911...93ccf7 )
by Adrien
33:03
created

TextData::PROPERCASE()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 10
loc 10
rs 9.4285
c 1
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation;
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 TextData
28
{
29
    private static $invalidChars;
30
31 15
    private static function unicodeToOrd($character)
32
    {
33 15
        return unpack('V', iconv('UTF-8', 'UCS-4LE', $character))[1];
34
    }
35
36
    /**
37
     * CHARACTER
38
     *
39
     * @param    string    $character    Value
40
     * @return    string
41
     */
42 11
    public static function CHARACTER($character)
43
    {
44 11
        $character = Functions::flattenSingleValue($character);
45
46 11
        if ((!is_numeric($character)) || ($character < 0)) {
47 2
            return Functions::VALUE();
48
        }
49
50 9
        if (function_exists('iconv')) {
51 9
            return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
52
        }
53
54
        return mb_convert_encoding('&#' . intval($character) . ';', 'UTF-8', 'HTML-ENTITIES');
55
    }
56
57
    /**
58
     * TRIMNONPRINTABLE
59
     *
60
     * @param    mixed    $stringValue    Value to check
61
     * @return    string
62
     */
63 5
    public static function TRIMNONPRINTABLE($stringValue = '')
64
    {
65 5
        $stringValue = Functions::flattenSingleValue($stringValue);
66
67 5
        if (is_bool($stringValue)) {
68 1
            return ($stringValue) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
69
        }
70
71 4
        if (self::$invalidChars == null) {
72 1
            self::$invalidChars = range(chr(0), chr(31));
73
        }
74
75 4
        if (is_string($stringValue) || is_numeric($stringValue)) {
76 3
            return str_replace(self::$invalidChars, '', trim($stringValue, "\x00..\x1F"));
77
        }
78
79 1
        return null;
80
    }
81
82
    /**
83
     * TRIMSPACES
84
     *
85
     * @param    mixed    $stringValue    Value to check
86
     * @return    string
87
     */
88 7
    public static function TRIMSPACES($stringValue = '')
89
    {
90 7
        $stringValue = Functions::flattenSingleValue($stringValue);
91 7
        if (is_bool($stringValue)) {
92 1
            return ($stringValue) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
93
        }
94
95 6
        if (is_string($stringValue) || is_numeric($stringValue)) {
96 5
            return trim(preg_replace('/ +/', ' ', trim($stringValue, ' ')), ' ');
97
        }
98
99 1
        return null;
100
    }
101
102
    /**
103
     * ASCIICODE
104
     *
105
     * @param    string    $characters        Value
106
     * @return    int
107
     */
108 17
    public static function ASCIICODE($characters)
109
    {
110 17
        if (($characters === null) || ($characters === '')) {
111 2
            return Functions::VALUE();
112
        }
113 15
        $characters = Functions::flattenSingleValue($characters);
114 15 View Code Duplication
        if (is_bool($characters)) {
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...
115 1
            if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
116
                $characters = (int) $characters;
117
            } else {
118 1
                $characters = ($characters) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
119
            }
120
        }
121
122 15
        $character = $characters;
123 15
        if ((function_exists('mb_strlen')) && (function_exists('mb_substr'))) {
124 15
            if (mb_strlen($characters, 'UTF-8') > 1) {
125 9
                $character = mb_substr($characters, 0, 1, 'UTF-8');
126
            }
127
128 15
            return self::unicodeToOrd($character);
129
        } else {
130
            if (strlen($characters) > 0) {
131
                $character = substr($characters, 0, 1);
132
            }
133
134
            return ord($character);
135
        }
136
    }
137
138
    /**
139
     * CONCATENATE
140
     *
141
     * @return    string
142
     */
143 3
    public static function CONCATENATE()
144
    {
145 3
        $returnValue = '';
146
147
        // Loop through arguments
148 3
        $aArgs = Functions::flattenArray(func_get_args());
149 3
        foreach ($aArgs as $arg) {
150 3 View Code Duplication
            if (is_bool($arg)) {
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...
151 1
                if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
152
                    $arg = (int) $arg;
153
                } else {
154 1
                    $arg = ($arg) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
155
                }
156
            }
157 3
            $returnValue .= $arg;
158
        }
159
160 3
        return $returnValue;
161
    }
162
163
    /**
164
     * DOLLAR
165
     *
166
     * This function converts a number to text using currency format, with the decimals rounded to the specified place.
167
     * The format used is $#,##0.00_);($#,##0.00)..
168
     *
169
     * @param    float    $value            The value to format
170
     * @param    int        $decimals        The number of digits to display to the right of the decimal point.
171
     *                                    If decimals is negative, number is rounded to the left of the decimal point.
172
     *                                    If you omit decimals, it is assumed to be 2
173
     * @return    string
174
     */
175 6
    public static function DOLLAR($value = 0, $decimals = 2)
176
    {
177 6
        $value = Functions::flattenSingleValue($value);
178 6
        $decimals = is_null($decimals) ? 0 : Functions::flattenSingleValue($decimals);
179
180
        // Validate parameters
181 6
        if (!is_numeric($value) || !is_numeric($decimals)) {
182 2
            return Functions::NAN();
183
        }
184 4
        $decimals = floor($decimals);
185
186 4
        $mask = '$#,##0';
187 4
        if ($decimals > 0) {
188 2
            $mask .= '.' . str_repeat('0', $decimals);
189
        } else {
190 2
            $round = pow(10, abs($decimals));
191 2
            if ($value < 0) {
192
                $round = 0 - $round;
193
            }
194 2
            $value = MathTrig::MROUND($value, $round);
195
        }
196
197 4
        return \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($value, $mask);
198
    }
199
200
    /**
201
     * SEARCHSENSITIVE
202
     *
203
     * @param    string    $needle        The string to look for
204
     * @param    string    $haystack    The string in which to look
205
     * @param    int        $offset        Offset within $haystack
206
     * @return    string
207
     */
208 13 View Code Duplication
    public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210 13
        $needle = Functions::flattenSingleValue($needle);
211 13
        $haystack = Functions::flattenSingleValue($haystack);
212 13
        $offset = Functions::flattenSingleValue($offset);
213
214 13
        if (!is_bool($needle)) {
215 13
            if (is_bool($haystack)) {
216 2
                $haystack = ($haystack) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
217
            }
218
219 13
            if (($offset > 0) && (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($haystack) > $offset)) {
0 ignored issues
show
Bug introduced by
It seems like $haystack defined by \PhpOffice\PhpSpreadshee...nSingleValue($haystack) on line 211 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...lper::countCharacters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
220 13
                if (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($needle) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $needle defined by \PhpOffice\PhpSpreadshee...tenSingleValue($needle) on line 210 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...lper::countCharacters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
221 2
                    return $offset;
222
                }
223 11
                if (function_exists('mb_strpos')) {
224 11
                    $pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
225
                } else {
226
                    $pos = strpos($haystack, $needle, --$offset);
227
                }
228 11
                if ($pos !== false) {
229 9
                    return ++$pos;
230
                }
231
            }
232
        }
233
234 2
        return Functions::VALUE();
235
    }
236
237
    /**
238
     * SEARCHINSENSITIVE
239
     *
240
     * @param    string    $needle        The string to look for
241
     * @param    string    $haystack    The string in which to look
242
     * @param    int        $offset        Offset within $haystack
243
     * @return    string
244
     */
245 11 View Code Duplication
    public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
    {
247 11
        $needle = Functions::flattenSingleValue($needle);
248 11
        $haystack = Functions::flattenSingleValue($haystack);
249 11
        $offset = Functions::flattenSingleValue($offset);
250
251 11
        if (!is_bool($needle)) {
252 11
            if (is_bool($haystack)) {
253 2
                $haystack = ($haystack) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
254
            }
255
256 11
            if (($offset > 0) && (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($haystack) > $offset)) {
0 ignored issues
show
Bug introduced by
It seems like $haystack defined by \PhpOffice\PhpSpreadshee...nSingleValue($haystack) on line 248 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...lper::countCharacters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
257 11
                if (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($needle) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $needle defined by \PhpOffice\PhpSpreadshee...tenSingleValue($needle) on line 247 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...lper::countCharacters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
258
                    return $offset;
259
                }
260 11
                if (function_exists('mb_stripos')) {
261 11
                    $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
262
                } else {
263
                    $pos = stripos($haystack, $needle, --$offset);
264
                }
265 11
                if ($pos !== false) {
266 9
                    return ++$pos;
267
                }
268
            }
269
        }
270
271 2
        return Functions::VALUE();
272
    }
273
274
    /**
275
     * FIXEDFORMAT
276
     *
277
     * @param    mixed        $value    Value to check
278
     * @param    int        $decimals
279
     * @param    bool        $no_commas
280
     * @return    string
281
     */
282 5
    public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
283
    {
284 5
        $value = Functions::flattenSingleValue($value);
285 5
        $decimals = Functions::flattenSingleValue($decimals);
286 5
        $no_commas = Functions::flattenSingleValue($no_commas);
287
288
        // Validate parameters
289 5
        if (!is_numeric($value) || !is_numeric($decimals)) {
290 2
            return Functions::NAN();
291
        }
292 3
        $decimals = floor($decimals);
293
294 3
        $valueResult = round($value, $decimals);
295 3
        if ($decimals < 0) {
296
            $decimals = 0;
297
        }
298 3
        if (!$no_commas) {
299 1
            $valueResult = number_format($valueResult, $decimals);
300
        }
301
302 3
        return (string) $valueResult;
303
    }
304
305
    /**
306
     * LEFT
307
     *
308
     * @param    string    $value    Value
309
     * @param    int        $chars    Number of characters
310
     * @return    string
311
     */
312 11
    public static function LEFT($value = '', $chars = 1)
313
    {
314 11
        $value = Functions::flattenSingleValue($value);
315 11
        $chars = Functions::flattenSingleValue($chars);
316
317 11
        if ($chars < 0) {
318 1
            return Functions::VALUE();
319
        }
320
321 10
        if (is_bool($value)) {
322 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
323
        }
324
325 10
        if (function_exists('mb_substr')) {
326 10
            return mb_substr($value, 0, $chars, 'UTF-8');
327
        } else {
328
            return substr($value, 0, $chars);
329
        }
330
    }
331
332
    /**
333
     * MID
334
     *
335
     * @param    string    $value    Value
336
     * @param    int        $start    Start character
337
     * @param    int        $chars    Number of characters
338
     * @return    string
339
     */
340 9
    public static function MID($value = '', $start = 1, $chars = null)
341
    {
342 9
        $value = Functions::flattenSingleValue($value);
343 9
        $start = Functions::flattenSingleValue($start);
344 9
        $chars = Functions::flattenSingleValue($chars);
345
346 9
        if (($start < 1) || ($chars < 0)) {
347 2
            return Functions::VALUE();
348
        }
349
350 7
        if (is_bool($value)) {
351 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
352
        }
353
354 7
        if (empty($chars)) {
355 1
            return '';
356
        }
357 6
        if (function_exists('mb_substr')) {
358 6
            return mb_substr($value, --$start, $chars, 'UTF-8');
359
        } else {
360
            return substr($value, --$start, $chars);
361
        }
362
    }
363
364
    /**
365
     * RIGHT
366
     *
367
     * @param    string    $value    Value
368
     * @param    int        $chars    Number of characters
369
     * @return    string
370
     */
371 11
    public static function RIGHT($value = '', $chars = 1)
372
    {
373 11
        $value = Functions::flattenSingleValue($value);
374 11
        $chars = Functions::flattenSingleValue($chars);
375
376 11
        if ($chars < 0) {
377 1
            return Functions::VALUE();
378
        }
379
380 10
        if (is_bool($value)) {
381 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
382
        }
383
384 10
        if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
385 10
            return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
386
        } else {
387
            return substr($value, strlen($value) - $chars);
388
        }
389
    }
390
391
    /**
392
     * STRINGLENGTH
393
     *
394
     * @param    string    $value    Value
395
     * @return    int
396
     */
397 11
    public static function STRINGLENGTH($value = '')
398
    {
399 11
        $value = Functions::flattenSingleValue($value);
400
401 11
        if (is_bool($value)) {
402 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
403
        }
404
405 11
        if (function_exists('mb_strlen')) {
406 11
            return mb_strlen($value, 'UTF-8');
407
        } else {
408
            return strlen($value);
409
        }
410
    }
411
412
    /**
413
     * LOWERCASE
414
     *
415
     * Converts a string value to upper case.
416
     *
417
     * @param    string        $mixedCaseString
418
     * @return    string
419
     */
420 4 View Code Duplication
    public static function LOWERCASE($mixedCaseString)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
421
    {
422 4
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
423
424 4
        if (is_bool($mixedCaseString)) {
425 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
426
        }
427
428 4
        return \PhpOffice\PhpSpreadsheet\Shared\StringHelper::strToLower($mixedCaseString);
0 ignored issues
show
Bug introduced by
It seems like $mixedCaseString defined by \PhpOffice\PhpSpreadshee...Value($mixedCaseString) on line 422 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...ingHelper::strToLower() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
429
    }
430
431
    /**
432
     * UPPERCASE
433
     *
434
     * Converts a string value to upper case.
435
     *
436
     * @param    string        $mixedCaseString
437
     * @return    string
438
     */
439 4 View Code Duplication
    public static function UPPERCASE($mixedCaseString)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
440
    {
441 4
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
442
443 4
        if (is_bool($mixedCaseString)) {
444 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
445
        }
446
447 4
        return \PhpOffice\PhpSpreadsheet\Shared\StringHelper::strToUpper($mixedCaseString);
0 ignored issues
show
Bug introduced by
It seems like $mixedCaseString defined by \PhpOffice\PhpSpreadshee...Value($mixedCaseString) on line 441 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...ingHelper::strToUpper() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
448
    }
449
450
    /**
451
     * PROPERCASE
452
     *
453
     * Converts a string value to upper case.
454
     *
455
     * @param    string        $mixedCaseString
456
     * @return    string
457
     */
458 3 View Code Duplication
    public static function PROPERCASE($mixedCaseString)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
459
    {
460 3
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
461
462 3
        if (is_bool($mixedCaseString)) {
463 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
464
        }
465
466 3
        return \PhpOffice\PhpSpreadsheet\Shared\StringHelper::strToTitle($mixedCaseString);
0 ignored issues
show
Bug introduced by
It seems like $mixedCaseString defined by \PhpOffice\PhpSpreadshee...Value($mixedCaseString) on line 460 can also be of type double or integer or null or object; however, PhpOffice\PhpSpreadsheet...ingHelper::strToTitle() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
467
    }
468
469
    /**
470
     * REPLACE
471
     *
472
     * @param    string    $oldText    String to modify
473
     * @param    int        $start        Start character
474
     * @param    int        $chars        Number of characters
475
     * @param    string    $newText    String to replace in defined position
476
     * @return    string
477
     */
478 5
    public static function REPLACE($oldText, $start, $chars, $newText)
479
    {
480 5
        $oldText = Functions::flattenSingleValue($oldText);
481 5
        $start = Functions::flattenSingleValue($start);
482 5
        $chars = Functions::flattenSingleValue($chars);
483 5
        $newText = Functions::flattenSingleValue($newText);
484
485 5
        $left = self::LEFT($oldText, $start - 1);
486 5
        $right = self::RIGHT($oldText, self::STRINGLENGTH($oldText) - ($start + $chars) + 1);
487
488 5
        return $left . $newText . $right;
489
    }
490
491
    /**
492
     * SUBSTITUTE
493
     *
494
     * @param    string    $text        Value
495
     * @param    string    $fromText    From Value
496
     * @param    string    $toText        To Value
497
     * @param    int    $instance    Instance Number
498
     * @return    string
499
     */
500 4
    public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0)
501
    {
502 4
        $text = Functions::flattenSingleValue($text);
503 4
        $fromText = Functions::flattenSingleValue($fromText);
504 4
        $toText = Functions::flattenSingleValue($toText);
505 4
        $instance = floor(Functions::flattenSingleValue($instance));
506
507 4
        if ($instance == 0) {
508 2
            if (function_exists('mb_str_replace')) {
509
                return mb_str_replace($fromText, $toText, $text);
510
            } else {
511 2
                return str_replace($fromText, $toText, $text);
512
            }
513
        } else {
514 2
            $pos = -1;
515 2
            while ($instance > 0) {
516 2
                if (function_exists('mb_strpos')) {
517 2
                    $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
518
                } else {
519
                    $pos = strpos($text, $fromText, $pos + 1);
520
                }
521 2
                if ($pos === false) {
522 1
                    break;
523
                }
524 1
                --$instance;
525
            }
526 2
            if ($pos !== false) {
527 1
                if (function_exists('mb_strlen')) {
528 1
                    return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
529
                } else {
530
                    return self::REPLACE($text, ++$pos, strlen($fromText), $toText);
531
                }
532
            }
533
        }
534
535 1
        return $text;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $text; (object|integer|double|string|null|boolean) is incompatible with the return type documented by PhpOffice\PhpSpreadsheet...on\TextData::SUBSTITUTE of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
536
    }
537
538
    /**
539
     * RETURNSTRING
540
     *
541
     * @param    mixed    $testValue    Value to check
542
     * @return    string|null
543
     */
544 5
    public static function RETURNSTRING($testValue = '')
545
    {
546 5
        $testValue = Functions::flattenSingleValue($testValue);
547
548 5
        if (is_string($testValue)) {
549 2
            return $testValue;
550
        }
551
552 3
        return null;
553
    }
554
555
    /**
556
     * TEXTFORMAT
557
     *
558
     * @param    mixed    $value    Value to check
559
     * @param    string    $format    Format mask to use
560
     * @return    string
561
     */
562 13
    public static function TEXTFORMAT($value, $format)
563
    {
564 13
        $value = Functions::flattenSingleValue($value);
565 13
        $format = Functions::flattenSingleValue($format);
566
567 13
        if ((is_string($value)) && (!is_numeric($value)) && \PhpOffice\PhpSpreadsheet\Shared\Date::isDateTimeFormatCode($format)) {
568 2
            $value = DateTime::DATEVALUE($value);
569
        }
570
571 13
        return (string) \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($value, $format);
572
    }
573
574
    /**
575
     * VALUE
576
     *
577
     * @param    mixed    $value    Value to check
578
     * @return    bool
579
     */
580 10
    public static function VALUE($value = '')
581
    {
582 10
        $value = Functions::flattenSingleValue($value);
583
584 10
        if (!is_numeric($value)) {
585 8
            $numberValue = str_replace(
586 8
                \PhpOffice\PhpSpreadsheet\Shared\StringHelper::getThousandsSeparator(),
587 8
                '',
588 8
                trim($value, " \t\n\r\0\x0B" . \PhpOffice\PhpSpreadsheet\Shared\StringHelper::getCurrencyCode())
589
            );
590 8
            if (is_numeric($numberValue)) {
591 3
                return (float) $numberValue;
592
            }
593
594 5
            $dateSetting = Functions::getReturnDateType();
595 5
            Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
596
597 5 View Code Duplication
            if (strpos($value, ':') !== false) {
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...
598 2
                $timeValue = DateTime::TIMEVALUE($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by \PhpOffice\PhpSpreadshee...ttenSingleValue($value) on line 582 can also be of type boolean or null or object; however, PhpOffice\PhpSpreadsheet...n\DateTime::TIMEVALUE() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
599 2
                if ($timeValue !== Functions::VALUE()) {
600 2
                    Functions::setReturnDateType($dateSetting);
601
602 2
                    return $timeValue;
603
                }
604
            }
605 3
            $dateValue = DateTime::DATEVALUE($value);
0 ignored issues
show
Documentation introduced by
$value is of type object|string|null|boolean, but the function expects a integer.

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...
606 3
            if ($dateValue !== Functions::VALUE()) {
607 1
                Functions::setReturnDateType($dateSetting);
608
609 1
                return $dateValue;
610
            }
611 2
            Functions::setReturnDateType($dateSetting);
612
613 2
            return Functions::VALUE();
614
        }
615
616 2
        return (float) $value;
617
    }
618
}
619