Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextData - Minor Changes, Test Coverage #2151

Merged
merged 7 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 0 additions & 95 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1320,11 +1320,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:TRIMSPACES\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:CONCATENATE\\(\\) has parameter \\$args with no typehint specified\\.$#"
count: 1
Expand All @@ -1340,96 +1335,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\CharacterConvert\\:\\:character\\(\\) should return string but returns string\\|false\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\CharacterConvert\\:\\:unicodeToOrd\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\CharacterConvert\\:\\:unicodeToOrd\\(\\) has parameter \\$character with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Cannot access offset 1 on array\\|false\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Parameter \\#2 \\$data of function unpack expects string, string\\|false given\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\CharacterConvert\\:\\:convertBooleanValue\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\CharacterConvert\\:\\:convertBooleanValue\\(\\) has parameter \\$value with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Concatenate\\:\\:CONCATENATE\\(\\) has parameter \\$args with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Concatenate\\:\\:convertBooleanValue\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Concatenate\\:\\:convertBooleanValue\\(\\) has parameter \\$value with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: "#^Parameter \\#3 \\$length of function mb_substr expects int\\|null, float\\|int\\<0, max\\>\\|string given\\.$#"
count: 3
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php

-
message: "#^Parameter \\#2 \\$start of function mb_substr expects int, float\\|int given\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php

-
message: "#^Cannot cast array\\|float\\|int\\|string to float\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: "#^Parameter \\#3 \\$offset of function mb_strpos expects int, float\\|int given\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Search.php

-
message: "#^Parameter \\#3 \\$offset of function mb_stripos expects int, float\\|int given\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Search.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Trim\\:\\:\\$invalidChars has no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Trim.php

-
message: "#^Parameter \\#1 \\$str of function trim expects string, float\\|int\\|string given\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/TextData/Trim.php

-
message: "#^Parameter \\#1 \\$str of function trim expects string, string\\|null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Trim.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Token\\\\Stack\\:\\:getStackItem\\(\\) has no return typehint specified\\.$#"
count: 1
Expand Down
16 changes: 3 additions & 13 deletions src/PhpSpreadsheet/Calculation/TextData/CaseConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;

Expand All @@ -18,10 +17,7 @@ class CaseConvert
public static function lower($mixedCaseValue): string
{
$mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue);

if (is_bool($mixedCaseValue)) {
$mixedCaseValue = ($mixedCaseValue === true) ? Calculation::getTRUE() : Calculation::getFALSE();
}
$mixedCaseValue = Helpers::extractString($mixedCaseValue);

return StringHelper::strToLower($mixedCaseValue);
}
Expand All @@ -36,10 +32,7 @@ public static function lower($mixedCaseValue): string
public static function upper($mixedCaseValue): string
{
$mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue);

if (is_bool($mixedCaseValue)) {
$mixedCaseValue = ($mixedCaseValue === true) ? Calculation::getTRUE() : Calculation::getFALSE();
}
$mixedCaseValue = Helpers::extractString($mixedCaseValue);

return StringHelper::strToUpper($mixedCaseValue);
}
Expand All @@ -54,10 +47,7 @@ public static function upper($mixedCaseValue): string
public static function proper($mixedCaseValue): string
{
$mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue);

if (is_bool($mixedCaseValue)) {
$mixedCaseValue = ($mixedCaseValue === true) ? Calculation::getTRUE() : Calculation::getFALSE();
}
$mixedCaseValue = Helpers::extractString($mixedCaseValue);

return StringHelper::strToTitle($mixedCaseValue);
}
Expand Down
44 changes: 18 additions & 26 deletions src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,40 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class CharacterConvert
{
/**
* CHARACTER.
* CHAR.
*
* @param mixed $character Integer Value to convert to its character representation
*/
public static function character($character): string
{
$character = Functions::flattenSingleValue($character);

if (!is_numeric($character)) {
return Functions::VALUE();
}

$character = (int) $character;
if ($character < 1 || $character > 255) {
$character = Helpers::validateInt($character);
$min = Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE ? 0 : 1;
if ($character < $min || $character > 255) {
return Functions::VALUE();
}
$result = iconv('UCS-4LE', 'UTF-8', pack('V', $character));

return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
return ($result === false) ? '' : $result;
}

/**
* ASCIICODE.
* CODE.
*
* @param mixed $characters String character to convert to its ASCII value
*
* @return int|string A string if arguments are invalid
*/
public static function code($characters)
{
if (($characters === null) || ($characters === '')) {
$characters = Helpers::extractString($characters);
if ($characters === '') {
return Functions::VALUE();
}
$characters = Functions::flattenSingleValue($characters);
if (is_bool($characters)) {
$characters = self::convertBooleanValue($characters);
}

$character = $characters;
if (mb_strlen($characters, 'UTF-8') > 1) {
Expand All @@ -53,17 +45,17 @@ public static function code($characters)
return self::unicodeToOrd($character);
}

private static function unicodeToOrd($character)
{
return unpack('V', iconv('UTF-8', 'UCS-4LE', $character))[1];
}

private static function convertBooleanValue($value)
private static function unicodeToOrd(string $character): int
{
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
return (int) $value;
$retVal = 0;
$iconv = iconv('UTF-8', 'UCS-4LE', $character);
if ($iconv !== false) {
$result = unpack('V', $iconv);
if (is_array($result) && isset($result[1])) {
$retVal = $result[1];
}
}

return ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
return $retVal;
}
}
27 changes: 8 additions & 19 deletions src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class Concatenate
{
/**
* CONCATENATE.
*
* @param array $args
*/
public static function CONCATENATE(...$args): string
{
$returnValue = '';

// Loop through arguments
$aArgs = Functions::flattenArray($args);

foreach ($aArgs as $arg) {
if (is_bool($arg)) {
$arg = self::convertBooleanValue($arg);
}
$returnValue .= $arg;
$returnValue .= Helpers::extractString($arg);
}

return $returnValue;
Expand All @@ -35,13 +34,15 @@ public static function CONCATENATE(...$args): string
*/
public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args): string
{
$delimiter = Functions::flattenSingleValue($delimiter);
$ignoreEmpty = Functions::flattenSingleValue($ignoreEmpty);
// Loop through arguments
$aArgs = Functions::flattenArray($args);
foreach ($aArgs as $key => &$arg) {
if ($ignoreEmpty === true && is_string($arg) && trim($arg) === '') {
unset($aArgs[$key]);
} elseif (is_bool($arg)) {
$arg = self::convertBooleanValue($arg);
$arg = Helpers::convertBooleanValue($arg);
}
}

Expand All @@ -59,24 +60,12 @@ public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args): string
public static function builtinREPT($stringValue, $repeatCount): string
{
$repeatCount = Functions::flattenSingleValue($repeatCount);
$stringValue = Helpers::extractString($stringValue);

if (!is_numeric($repeatCount) || $repeatCount < 0) {
return Functions::VALUE();
}

if (is_bool($stringValue)) {
$stringValue = self::convertBooleanValue($stringValue);
}

return str_repeat($stringValue, (int) $repeatCount);
}

private static function convertBooleanValue($value)
{
if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) {
return (int) $value;
}

return ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
}
}
53 changes: 20 additions & 33 deletions src/PhpSpreadsheet/Calculation/TextData/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;

class Extract
{
Expand All @@ -13,17 +12,13 @@ class Extract
* @param mixed $value String value from which to extract characters
* @param mixed $chars The number of characters to extract (as an integer)
*/
public static function left($value = '', $chars = 1): string
public static function left($value, $chars = 1): string
{
$value = Functions::flattenSingleValue($value);
$chars = Functions::flattenSingleValue($chars);

if (!is_numeric($chars) || $chars < 0) {
return Functions::VALUE();
}

if (is_bool($value)) {
$value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}

return mb_substr($value ?? '', 0, $chars, 'UTF-8');
Expand All @@ -36,18 +31,14 @@ public static function left($value = '', $chars = 1): string
* @param mixed $start Integer offset of the first character that we want to extract
* @param mixed $chars The number of characters to extract (as an integer)
*/
public static function mid($value = '', $start = 1, $chars = null): string
public static function mid($value, $start, $chars): string
{
$value = Functions::flattenSingleValue($value);
$start = Functions::flattenSingleValue($start);
$chars = Functions::flattenSingleValue($chars);

if (!is_numeric($start) || $start < 1 || !is_numeric($chars) || $chars < 0) {
return Functions::VALUE();
}

if (is_bool($value)) {
$value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
try {
$value = Helpers::extractString($value);
$start = Helpers::extractInt($start, 1);
$chars = Helpers::extractInt($chars, 0);
} catch (CalcExp $e) {
return $e->getMessage();
}

return mb_substr($value ?? '', --$start, $chars, 'UTF-8');
Expand All @@ -59,17 +50,13 @@ public static function mid($value = '', $start = 1, $chars = null): string
* @param mixed $value String value from which to extract characters
* @param mixed $chars The number of characters to extract (as an integer)
*/
public static function right($value = '', $chars = 1): string
public static function right($value, $chars = 1): string
{
$value = Functions::flattenSingleValue($value);
$chars = Functions::flattenSingleValue($chars);

if (!is_numeric($chars) || $chars < 0) {
return Functions::VALUE();
}

if (is_bool($value)) {
$value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}

return mb_substr($value ?? '', mb_strlen($value ?? '', 'UTF-8') - $chars, $chars, 'UTF-8');
Expand Down
Loading