Skip to content

Commit 189152e

Browse files
author
MarkBaker
committed
Apply some coercive type-hinting
1 parent 5cba6d8 commit 189152e

File tree

4 files changed

+37
-93
lines changed

4 files changed

+37
-93
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,26 +3115,6 @@ parameters:
31153115
count: 1
31163116
path: src/PhpSpreadsheet/Shared/StringHelper.php
31173117

3118-
-
3119-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\StringHelper\\:\\:mbIsUpper\\(\\) has no return type specified\\.$#"
3120-
count: 1
3121-
path: src/PhpSpreadsheet/Shared/StringHelper.php
3122-
3123-
-
3124-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\StringHelper\\:\\:mbIsUpper\\(\\) has parameter \\$character with no type specified\\.$#"
3125-
count: 1
3126-
path: src/PhpSpreadsheet/Shared/StringHelper.php
3127-
3128-
-
3129-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\StringHelper\\:\\:mbStrSplit\\(\\) has no return type specified\\.$#"
3130-
count: 1
3131-
path: src/PhpSpreadsheet/Shared/StringHelper.php
3132-
3133-
-
3134-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\StringHelper\\:\\:mbStrSplit\\(\\) has parameter \\$string with no type specified\\.$#"
3135-
count: 1
3136-
path: src/PhpSpreadsheet/Shared/StringHelper.php
3137-
31383118
-
31393119
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\StringHelper\\:\\:sanitizeUTF8\\(\\) should return string but returns string\\|false\\.$#"
31403120
count: 1
@@ -3160,11 +3140,6 @@ parameters:
31603140
count: 1
31613141
path: src/PhpSpreadsheet/Shared/StringHelper.php
31623142

3163-
-
3164-
message: "#^Variable \\$textValue on left side of \\?\\? always exists and is not nullable\\.$#"
3165-
count: 3
3166-
path: src/PhpSpreadsheet/Shared/StringHelper.php
3167-
31683143
-
31693144
message: "#^Static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\TimeZone\\:\\:validateTimeZone\\(\\) is unused\\.$#"
31703145
count: 1

src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static function hLookupSearch($lookupValue, array $lookupArray, $column,
7373
// break if we have passed possible keys
7474
$bothNumeric = is_numeric($lookupValue) && is_numeric($rowData);
7575
$bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData);
76-
$cellDataLower = StringHelper::strToLower($rowData);
76+
$cellDataLower = StringHelper::strToLower((string) $rowData);
7777

7878
if (
7979
$notExactMatch &&

src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static function vLookupSearch($lookupValue, array $lookupArray, $column,
9090
foreach ($lookupArray as $rowKey => $rowData) {
9191
$bothNumeric = is_numeric($lookupValue) && is_numeric($rowData[$column]);
9292
$bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData[$column]);
93-
$cellDataLower = StringHelper::strToLower($rowData[$column]);
93+
$cellDataLower = StringHelper::strToLower((string) $rowData[$column]);
9494

9595
// break if we have passed possible keys
9696
if (

src/PhpSpreadsheet/Shared/StringHelper.php

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,8 @@ public static function controlCharacterPHP2OOXML($textValue)
329329

330330
/**
331331
* Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
332-
*
333-
* @param string $textValue
334-
*
335-
* @return string
336332
*/
337-
public static function sanitizeUTF8($textValue)
333+
public static function sanitizeUTF8(string $textValue): string
338334
{
339335
if (self::getIsIconvEnabled()) {
340336
$textValue = @iconv('UTF-8', 'UTF-8', $textValue);
@@ -349,12 +345,8 @@ public static function sanitizeUTF8($textValue)
349345

350346
/**
351347
* Check if a string contains UTF8 data.
352-
*
353-
* @param string $textValue
354-
*
355-
* @return bool
356348
*/
357-
public static function isUTF8($textValue)
349+
public static function isUTF8(string $textValue): bool
358350
{
359351
return $textValue === '' || preg_match('/^./su', $textValue) === 1;
360352
}
@@ -364,10 +356,8 @@ public static function isUTF8($textValue)
364356
* point as decimal separator in case locale is other than English.
365357
*
366358
* @param mixed $numericValue
367-
*
368-
* @return string
369359
*/
370-
public static function formatNumber($numericValue)
360+
public static function formatNumber($numericValue): string
371361
{
372362
if (is_float($numericValue)) {
373363
return str_replace(',', '.', $numericValue);
@@ -385,10 +375,8 @@ public static function formatNumber($numericValue)
385375
*
386376
* @param string $textValue UTF-8 encoded string
387377
* @param mixed[] $arrcRuns Details of rich text runs in $value
388-
*
389-
* @return string
390378
*/
391-
public static function UTF8toBIFF8UnicodeShort($textValue, $arrcRuns = [])
379+
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
392380
{
393381
// character count
394382
$ln = self::countCharacters($textValue, 'UTF-8');
@@ -419,10 +407,8 @@ public static function UTF8toBIFF8UnicodeShort($textValue, $arrcRuns = [])
419407
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
420408
*
421409
* @param string $textValue UTF-8 encoded string
422-
*
423-
* @return string
424410
*/
425-
public static function UTF8toBIFF8UnicodeLong($textValue)
411+
public static function UTF8toBIFF8UnicodeLong(string $textValue): string
426412
{
427413
// character count
428414
$ln = self::countCharacters($textValue, 'UTF-8');
@@ -436,13 +422,10 @@ public static function UTF8toBIFF8UnicodeLong($textValue)
436422
/**
437423
* Convert string from one encoding to another.
438424
*
439-
* @param string $textValue
440425
* @param string $to Encoding to convert to, e.g. 'UTF-8'
441426
* @param string $from Encoding to convert from, e.g. 'UTF-16LE'
442-
*
443-
* @return string
444427
*/
445-
public static function convertEncoding($textValue, $to, $from)
428+
public static function convertEncoding(string $textValue, string $to, string $from): string
446429
{
447430
if (self::getIsIconvEnabled()) {
448431
$result = iconv($from, $to . self::$iconvOptions, $textValue);
@@ -457,88 +440,82 @@ public static function convertEncoding($textValue, $to, $from)
457440
/**
458441
* Get character count.
459442
*
460-
* @param string $textValue
461443
* @param string $encoding Encoding
462444
*
463445
* @return int Character count
464446
*/
465-
public static function countCharacters($textValue, $encoding = 'UTF-8')
447+
public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int
466448
{
467-
return mb_strlen($textValue ?? '', $encoding);
449+
return mb_strlen($textValue, $encoding);
468450
}
469451

470452
/**
471453
* Get a substring of a UTF-8 encoded string.
472454
*
473-
* @param null|string $textValue UTF-8 encoded string
455+
* @param string $textValue UTF-8 encoded string
474456
* @param int $offset Start offset
475457
* @param int $length Maximum number of characters in substring
476-
*
477-
* @return string
478458
*/
479-
public static function substring($textValue, $offset, $length = 0)
459+
public static function substring(string $textValue, int $offset, int $length = 0): string
480460
{
481-
return mb_substr($textValue ?? '', $offset, $length, 'UTF-8');
461+
return mb_substr($textValue, $offset, $length, 'UTF-8');
482462
}
483463

484464
/**
485465
* Convert a UTF-8 encoded string to upper case.
486466
*
487467
* @param string $textValue UTF-8 encoded string
488-
*
489-
* @return string
490468
*/
491-
public static function strToUpper($textValue)
469+
public static function strToUpper(string $textValue): string
492470
{
493-
return mb_convert_case($textValue ?? '', MB_CASE_UPPER, 'UTF-8');
471+
return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8');
494472
}
495473

496474
/**
497475
* Convert a UTF-8 encoded string to lower case.
498476
*
499477
* @param string $textValue UTF-8 encoded string
500-
*
501-
* @return string
502478
*/
503-
public static function strToLower($textValue)
479+
public static function strToLower(string $textValue): string
504480
{
505-
return mb_convert_case($textValue ?? '', MB_CASE_LOWER, 'UTF-8');
481+
return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8');
506482
}
507483

508484
/**
509485
* Convert a UTF-8 encoded string to title/proper case
510486
* (uppercase every first character in each word, lower case all other characters).
511487
*
512488
* @param string $textValue UTF-8 encoded string
513-
*
514-
* @return string
515489
*/
516-
public static function strToTitle($textValue)
490+
public static function strToTitle(string $textValue): string
517491
{
518492
return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8');
519493
}
520494

521-
public static function mbIsUpper($character)
495+
public static function mbIsUpper(string $character): bool
522496
{
523-
return mb_strtolower($character, 'UTF-8') != $character;
497+
return mb_strtolower($character, 'UTF-8') !== $character;
524498
}
525499

526-
public static function mbStrSplit($string)
500+
/**
501+
* Splits a UTF-8 string into an array of individual characters.
502+
*/
503+
public static function mbStrSplit(string $string): array
527504
{
528505
// Split at all position not after the start: ^
529506
// and not before the end: $
530-
return preg_split('/(?<!^)(?!$)/u', $string);
507+
$split = preg_split('/(?<!^)(?!$)/u', $string);
508+
509+
return ($split === false) ? [] : $split;
531510
}
532511

533512
/**
534513
* Reverse the case of a string, so that all uppercase characters become lowercase
535514
* and all lowercase characters become uppercase.
536515
*
537516
* @param string $textValue UTF-8 encoded string
538-
*
539-
* @return string
540517
*/
541-
public static function strCaseReverse($textValue)
518+
public static function strCaseReverse(string $textValue): string
542519
{
543520
$characters = self::mbStrSplit($textValue);
544521
foreach ($characters as &$character) {
@@ -557,10 +534,8 @@ public static function strCaseReverse($textValue)
557534
* and convert it to a numeric if it is.
558535
*
559536
* @param string $operand string value to test
560-
*
561-
* @return bool
562537
*/
563-
public static function convertToNumberIfFraction(&$operand)
538+
public static function convertToNumberIfFraction(string &$operand): bool
564539
{
565540
if (preg_match('/^' . self::STRING_REGEXP_FRACTION . '$/i', $operand, $match)) {
566541
$sign = ($match[1] == '-') ? '-' : '+';
@@ -578,10 +553,8 @@ public static function convertToNumberIfFraction(&$operand)
578553
/**
579554
* Get the decimal separator. If it has not yet been set explicitly, try to obtain number
580555
* formatting information from locale.
581-
*
582-
* @return string
583556
*/
584-
public static function getDecimalSeparator()
557+
public static function getDecimalSeparator(): string
585558
{
586559
if (!isset(self::$decimalSeparator)) {
587560
$localeconv = localeconv();
@@ -603,18 +576,16 @@ public static function getDecimalSeparator()
603576
*
604577
* @param string $separator Character for decimal separator
605578
*/
606-
public static function setDecimalSeparator($separator): void
579+
public static function setDecimalSeparator(string $separator): void
607580
{
608581
self::$decimalSeparator = $separator;
609582
}
610583

611584
/**
612585
* Get the thousands separator. If it has not yet been set explicitly, try to obtain number
613586
* formatting information from locale.
614-
*
615-
* @return string
616587
*/
617-
public static function getThousandsSeparator()
588+
public static function getThousandsSeparator(): string
618589
{
619590
if (!isset(self::$thousandsSeparator)) {
620591
$localeconv = localeconv();
@@ -636,18 +607,16 @@ public static function getThousandsSeparator()
636607
*
637608
* @param string $separator Character for thousands separator
638609
*/
639-
public static function setThousandsSeparator($separator): void
610+
public static function setThousandsSeparator(string $separator): void
640611
{
641612
self::$thousandsSeparator = $separator;
642613
}
643614

644615
/**
645616
* Get the currency code. If it has not yet been set explicitly, try to obtain the
646617
* symbol information from locale.
647-
*
648-
* @return string
649618
*/
650-
public static function getCurrencyCode()
619+
public static function getCurrencyCode(): string
651620
{
652621
if (!empty(self::$currencyCode)) {
653622
return self::$currencyCode;
@@ -674,19 +643,19 @@ public static function getCurrencyCode()
674643
*
675644
* @param string $currencyCode Character for currency code
676645
*/
677-
public static function setCurrencyCode($currencyCode): void
646+
public static function setCurrencyCode(string $currencyCode): void
678647
{
679648
self::$currencyCode = $currencyCode;
680649
}
681650

682651
/**
683652
* Convert SYLK encoded string to UTF-8.
684653
*
685-
* @param string $textValue
654+
* @param string $textValue SYLK encoded string
686655
*
687656
* @return string UTF-8 encoded string
688657
*/
689-
public static function SYLKtoUTF8($textValue)
658+
public static function SYLKtoUTF8(string $textValue): string
690659
{
691660
self::buildCharacterSets();
692661

0 commit comments

Comments
 (0)