Completed
Push — develop ( 47d726...7b90bb )
by Adrien
17:12
created

TextData::DOLLAR()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 8
nop 2
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
crap 6.0131
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
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
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
     *
41
     * @return string
42
     */
43 11
    public static function CHARACTER($character)
44
    {
45 11
        $character = Functions::flattenSingleValue($character);
46
47 11
        if ((!is_numeric($character)) || ($character < 0)) {
48 2
            return Functions::VALUE();
49
        }
50
51 9
        if (function_exists('iconv')) {
52 9
            return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
53
        }
54
55
        return mb_convert_encoding('&#' . (int) $character . ';', 'UTF-8', 'HTML-ENTITIES');
56
    }
57
58
    /**
59
     * TRIMNONPRINTABLE.
60
     *
61
     * @param mixed $stringValue Value to check
62
     *
63
     * @return string
64
     */
65 5
    public static function TRIMNONPRINTABLE($stringValue = '')
66
    {
67 5
        $stringValue = Functions::flattenSingleValue($stringValue);
68
69 5
        if (is_bool($stringValue)) {
70 1
            return ($stringValue) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
71
        }
72
73 4
        if (self::$invalidChars == null) {
74 1
            self::$invalidChars = range(chr(0), chr(31));
75
        }
76
77 4
        if (is_string($stringValue) || is_numeric($stringValue)) {
78 3
            return str_replace(self::$invalidChars, '', trim($stringValue, "\x00..\x1F"));
79
        }
80
81 1
        return null;
82
    }
83
84
    /**
85
     * TRIMSPACES.
86
     *
87
     * @param mixed $stringValue Value to check
88
     *
89
     * @return string
90
     */
91 7
    public static function TRIMSPACES($stringValue = '')
92
    {
93 7
        $stringValue = Functions::flattenSingleValue($stringValue);
94 7
        if (is_bool($stringValue)) {
95 1
            return ($stringValue) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
96
        }
97
98 6
        if (is_string($stringValue) || is_numeric($stringValue)) {
99 5
            return trim(preg_replace('/ +/', ' ', trim($stringValue, ' ')), ' ');
100
        }
101
102 1
        return null;
103
    }
104
105
    /**
106
     * ASCIICODE.
107
     *
108
     * @param string $characters Value
109
     *
110
     * @return int
111
     */
112 17
    public static function ASCIICODE($characters)
113
    {
114 17
        if (($characters === null) || ($characters === '')) {
115 2
            return Functions::VALUE();
116
        }
117 15
        $characters = Functions::flattenSingleValue($characters);
118 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...
119 1
            if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
120
                $characters = (int) $characters;
121
            } else {
122 1
                $characters = ($characters) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
123
            }
124
        }
125
126 15
        $character = $characters;
127 15
        if (mb_strlen($characters, 'UTF-8') > 1) {
128 9
            $character = mb_substr($characters, 0, 1, 'UTF-8');
129
        }
130
131 15
        return self::unicodeToOrd($character);
132
    }
133
134
    /**
135
     * CONCATENATE.
136
     *
137
     * @return string
138
     */
139 3
    public static function CONCATENATE(...$args)
140
    {
141 3
        $returnValue = '';
142
143
        // Loop through arguments
144 3
        $aArgs = Functions::flattenArray($args);
145 3
        foreach ($aArgs as $arg) {
146 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...
147 1
                if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
148
                    $arg = (int) $arg;
149
                } else {
150 1
                    $arg = ($arg) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
151
                }
152
            }
153 3
            $returnValue .= $arg;
154
        }
155
156 3
        return $returnValue;
157
    }
158
159
    /**
160
     * DOLLAR.
161
     *
162
     * This function converts a number to text using currency format, with the decimals rounded to the specified place.
163
     * The format used is $#,##0.00_);($#,##0.00)..
164
     *
165
     * @param float $value The value to format
166
     * @param int $decimals The number of digits to display to the right of the decimal point.
167
     *                                    If decimals is negative, number is rounded to the left of the decimal point.
168
     *                                    If you omit decimals, it is assumed to be 2
169
     *
170
     * @return string
171
     */
172 6
    public static function DOLLAR($value = 0, $decimals = 2)
173
    {
174 6
        $value = Functions::flattenSingleValue($value);
175 6
        $decimals = is_null($decimals) ? 0 : Functions::flattenSingleValue($decimals);
176
177
        // Validate parameters
178 6
        if (!is_numeric($value) || !is_numeric($decimals)) {
179 2
            return Functions::NAN();
180
        }
181 4
        $decimals = floor($decimals);
182
183 4
        $mask = '$#,##0';
184 4
        if ($decimals > 0) {
185 2
            $mask .= '.' . str_repeat('0', $decimals);
186
        } else {
187 2
            $round = pow(10, abs($decimals));
188 2
            if ($value < 0) {
189
                $round = 0 - $round;
190
            }
191 2
            $value = MathTrig::MROUND($value, $round);
192
        }
193
194 4
        return \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($value, $mask);
195
    }
196
197
    /**
198
     * SEARCHSENSITIVE.
199
     *
200
     * @param string $needle The string to look for
201
     * @param string $haystack The string in which to look
202
     * @param int $offset Offset within $haystack
203
     *
204
     * @return string
205
     */
206 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...
207
    {
208 13
        $needle = Functions::flattenSingleValue($needle);
209 13
        $haystack = Functions::flattenSingleValue($haystack);
210 13
        $offset = Functions::flattenSingleValue($offset);
211
212 13
        if (!is_bool($needle)) {
213 13
            if (is_bool($haystack)) {
214 2
                $haystack = ($haystack) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
215
            }
216
217 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 209 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...
218 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 208 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...
219 2
                    return $offset;
220
                }
221
222 11
                $pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
223 11
                if ($pos !== false) {
224 9
                    return ++$pos;
225
                }
226
            }
227
        }
228
229 2
        return Functions::VALUE();
230
    }
231
232
    /**
233
     * SEARCHINSENSITIVE.
234
     *
235
     * @param string $needle The string to look for
236
     * @param string $haystack The string in which to look
237
     * @param int $offset Offset within $haystack
238
     *
239
     * @return string
240
     */
241 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...
242
    {
243 11
        $needle = Functions::flattenSingleValue($needle);
244 11
        $haystack = Functions::flattenSingleValue($haystack);
245 11
        $offset = Functions::flattenSingleValue($offset);
246
247 11
        if (!is_bool($needle)) {
248 11
            if (is_bool($haystack)) {
249 2
                $haystack = ($haystack) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
250
            }
251
252 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 244 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...
253 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 243 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...
254
                    return $offset;
255
                }
256
257 11
                $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
258 11
                if ($pos !== false) {
259 9
                    return ++$pos;
260
                }
261
            }
262
        }
263
264 2
        return Functions::VALUE();
265
    }
266
267
    /**
268
     * FIXEDFORMAT.
269
     *
270
     * @param mixed $value Value to check
271
     * @param int $decimals
272
     * @param bool $no_commas
273
     *
274
     * @return string
275
     */
276 5
    public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
277
    {
278 5
        $value = Functions::flattenSingleValue($value);
279 5
        $decimals = Functions::flattenSingleValue($decimals);
280 5
        $no_commas = Functions::flattenSingleValue($no_commas);
281
282
        // Validate parameters
283 5
        if (!is_numeric($value) || !is_numeric($decimals)) {
284 2
            return Functions::NAN();
285
        }
286 3
        $decimals = floor($decimals);
287
288 3
        $valueResult = round($value, $decimals);
289 3
        if ($decimals < 0) {
290
            $decimals = 0;
291
        }
292 3
        if (!$no_commas) {
293 1
            $valueResult = number_format($valueResult, $decimals);
294
        }
295
296 3
        return (string) $valueResult;
297
    }
298
299
    /**
300
     * LEFT.
301
     *
302
     * @param string $value Value
303
     * @param int $chars Number of characters
304
     *
305
     * @return string
306
     */
307 11 View Code Duplication
    public static function LEFT($value = '', $chars = 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...
308
    {
309 11
        $value = Functions::flattenSingleValue($value);
310 11
        $chars = Functions::flattenSingleValue($chars);
311
312 11
        if ($chars < 0) {
313 1
            return Functions::VALUE();
314
        }
315
316 10
        if (is_bool($value)) {
317 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
318
        }
319
320 10
        return mb_substr($value, 0, $chars, 'UTF-8');
321
    }
322
323
    /**
324
     * MID.
325
     *
326
     * @param string $value Value
327
     * @param int $start Start character
328
     * @param int $chars Number of characters
329
     *
330
     * @return string
331
     */
332 9
    public static function MID($value = '', $start = 1, $chars = null)
333
    {
334 9
        $value = Functions::flattenSingleValue($value);
335 9
        $start = Functions::flattenSingleValue($start);
336 9
        $chars = Functions::flattenSingleValue($chars);
337
338 9
        if (($start < 1) || ($chars < 0)) {
339 2
            return Functions::VALUE();
340
        }
341
342 7
        if (is_bool($value)) {
343 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
344
        }
345
346 7
        if (empty($chars)) {
347 1
            return '';
348
        }
349
350 6
        return mb_substr($value, --$start, $chars, 'UTF-8');
351
    }
352
353
    /**
354
     * RIGHT.
355
     *
356
     * @param string $value Value
357
     * @param int $chars Number of characters
358
     *
359
     * @return string
360
     */
361 11 View Code Duplication
    public static function RIGHT($value = '', $chars = 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...
362
    {
363 11
        $value = Functions::flattenSingleValue($value);
364 11
        $chars = Functions::flattenSingleValue($chars);
365
366 11
        if ($chars < 0) {
367 1
            return Functions::VALUE();
368
        }
369
370 10
        if (is_bool($value)) {
371 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
372
        }
373
374 10
        return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
375
    }
376
377
    /**
378
     * STRINGLENGTH.
379
     *
380
     * @param string $value Value
381
     *
382
     * @return int
383
     */
384 11 View Code Duplication
    public static function STRINGLENGTH($value = '')
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...
385
    {
386 11
        $value = Functions::flattenSingleValue($value);
387
388 11
        if (is_bool($value)) {
389 2
            $value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
390
        }
391
392 11
        return mb_strlen($value, 'UTF-8');
393
    }
394
395
    /**
396
     * LOWERCASE.
397
     *
398
     * Converts a string value to upper case.
399
     *
400
     * @param string $mixedCaseString
401
     *
402
     * @return string
403
     */
404 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...
405
    {
406 4
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
407
408 4
        if (is_bool($mixedCaseString)) {
409 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
410
        }
411
412 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 406 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...
413
    }
414
415
    /**
416
     * UPPERCASE.
417
     *
418
     * Converts a string value to upper case.
419
     *
420
     * @param string $mixedCaseString
421
     *
422
     * @return string
423
     */
424 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...
425
    {
426 4
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
427
428 4
        if (is_bool($mixedCaseString)) {
429 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
430
        }
431
432 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 426 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...
433
    }
434
435
    /**
436
     * PROPERCASE.
437
     *
438
     * Converts a string value to upper case.
439
     *
440
     * @param string $mixedCaseString
441
     *
442
     * @return string
443
     */
444 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...
445
    {
446 3
        $mixedCaseString = Functions::flattenSingleValue($mixedCaseString);
447
448 3
        if (is_bool($mixedCaseString)) {
449 2
            $mixedCaseString = ($mixedCaseString) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
450
        }
451
452 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 446 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...
453
    }
454
455
    /**
456
     * REPLACE.
457
     *
458
     * @param string $oldText String to modify
459
     * @param int $start Start character
460
     * @param int $chars Number of characters
461
     * @param string $newText String to replace in defined position
462
     *
463
     * @return string
464
     */
465 5
    public static function REPLACE($oldText, $start, $chars, $newText)
466
    {
467 5
        $oldText = Functions::flattenSingleValue($oldText);
468 5
        $start = Functions::flattenSingleValue($start);
469 5
        $chars = Functions::flattenSingleValue($chars);
470 5
        $newText = Functions::flattenSingleValue($newText);
471
472 5
        $left = self::LEFT($oldText, $start - 1);
473 5
        $right = self::RIGHT($oldText, self::STRINGLENGTH($oldText) - ($start + $chars) + 1);
474
475 5
        return $left . $newText . $right;
476
    }
477
478
    /**
479
     * SUBSTITUTE.
480
     *
481
     * @param string $text Value
482
     * @param string $fromText From Value
483
     * @param string $toText To Value
484
     * @param int $instance Instance Number
485
     *
486
     * @return string
487
     */
488 4
    public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0)
489
    {
490 4
        $text = Functions::flattenSingleValue($text);
491 4
        $fromText = Functions::flattenSingleValue($fromText);
492 4
        $toText = Functions::flattenSingleValue($toText);
493 4
        $instance = floor(Functions::flattenSingleValue($instance));
494
495 4
        if ($instance == 0) {
496 2
            return \PhpOffice\PhpSpreadsheet\Shared\StringHelper::mbStrReplace($fromText, $toText, $text);
497
        }
498
499 2
        $pos = -1;
500 2
        while ($instance > 0) {
501 2
            $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
502 2
            if ($pos === false) {
503 1
                break;
504
            }
505 1
            --$instance;
506
        }
507
508 2
        if ($pos !== false) {
509 1
            return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
510
        }
511
512 1
        return $text;
513
    }
514
515
    /**
516
     * RETURNSTRING.
517
     *
518
     * @param mixed $testValue Value to check
519
     *
520
     * @return string|null
521
     */
522 5
    public static function RETURNSTRING($testValue = '')
523
    {
524 5
        $testValue = Functions::flattenSingleValue($testValue);
525
526 5
        if (is_string($testValue)) {
527 2
            return $testValue;
528
        }
529
530 3
        return null;
531
    }
532
533
    /**
534
     * TEXTFORMAT.
535
     *
536
     * @param mixed $value Value to check
537
     * @param string $format Format mask to use
538
     *
539
     * @return string
540
     */
541 13
    public static function TEXTFORMAT($value, $format)
542
    {
543 13
        $value = Functions::flattenSingleValue($value);
544 13
        $format = Functions::flattenSingleValue($format);
545
546 13
        if ((is_string($value)) && (!is_numeric($value)) && \PhpOffice\PhpSpreadsheet\Shared\Date::isDateTimeFormatCode($format)) {
547 2
            $value = DateTime::DATEVALUE($value);
548
        }
549
550 13
        return (string) \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($value, $format);
551
    }
552
553
    /**
554
     * VALUE.
555
     *
556
     * @param mixed $value Value to check
557
     *
558
     * @return bool
559
     */
560 10
    public static function VALUE($value = '')
561
    {
562 10
        $value = Functions::flattenSingleValue($value);
563
564 10
        if (!is_numeric($value)) {
565 8
            $numberValue = str_replace(
566 8
                \PhpOffice\PhpSpreadsheet\Shared\StringHelper::getThousandsSeparator(),
567 8
                '',
568 8
                trim($value, " \t\n\r\0\x0B" . \PhpOffice\PhpSpreadsheet\Shared\StringHelper::getCurrencyCode())
569
            );
570 8
            if (is_numeric($numberValue)) {
571 3
                return (float) $numberValue;
572
            }
573
574 5
            $dateSetting = Functions::getReturnDateType();
575 5
            Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
576
577 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...
578 2
                $timeValue = DateTime::TIMEVALUE($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by \PhpOffice\PhpSpreadshee...ttenSingleValue($value) on line 562 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...
579 2
                if ($timeValue !== Functions::VALUE()) {
580 2
                    Functions::setReturnDateType($dateSetting);
581
582 2
                    return $timeValue;
583
                }
584
            }
585 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...
586 3
            if ($dateValue !== Functions::VALUE()) {
587 1
                Functions::setReturnDateType($dateSetting);
588
589 1
                return $dateValue;
590
            }
591 2
            Functions::setReturnDateType($dateSetting);
592
593 2
            return Functions::VALUE();
594
        }
595
596 2
        return (float) $value;
597
    }
598
}
599