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

Fixes for PHP types #5495

Merged
merged 3 commits into from
Aug 31, 2023
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
2 changes: 1 addition & 1 deletion src/Toolkit/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public static function nest(array $array, array $ignore = []): array

foreach ($array as $fullKey => $value) {
// extract the first part of a multi-level key, keep the others
$subKeys = explode('.', $fullKey);
$subKeys = is_int($fullKey) ? [$fullKey] : explode('.', $fullKey);
$key = array_shift($subKeys);

// skip the magic for ignored keys
Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public static function youtube(
return false;
}

return preg_match('!^[a-zA-Z0-9_-]+$!', $id);
return preg_match('!^[a-zA-Z0-9_-]+$!', $id) === 1;
};

switch ($path->toString()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,6 @@ public static function translateCount(
$count = static::formatNumber($count, $locale);
}

return str_replace('{{ count }}', $count, $message);
return str_replace('{{ count }}', (string)$count, $message);
}
}
19 changes: 16 additions & 3 deletions src/Toolkit/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,21 @@ public static function float(
// make sure $value is not null
$value ??= '';

// turn the value into a string
$value = (string)$value;

// Convert exponential to decimal, 1e-8 as 0.00000001
if (strpos(strtolower($value), 'e') !== false) {
$value = rtrim(sprintf('%.16f', (float)$value), '0');
}

$value = str_replace(',', '.', $value);
$decimal = strlen(substr(strrchr($value, '.'), 1));
$decimal = strrchr($value, '.');
$decimal = match ($decimal) {
false => 0,
default => strlen($decimal) - 1
};

return number_format((float)$value, $decimal, '.', '');
}

Expand Down Expand Up @@ -972,7 +980,7 @@ public static function safeTemplate(
public static function short(
string $string = null,
int $length = 0,
string $appendix = '…'
string|false $appendix = '…'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was false as the argument already supported or used before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@lukasbestle lukasbestle Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@distantnative Should we really officially support this though by adding the type hint for false? If false is passed to an argument that is hinted for string, the false value is converted to an empty string. This can be made explicit in the calling code (here: Str::slug()) by passing an empty string instead of false in the first place.

I feel like supporting false is unnecessary because it doesn't add any benefit over an empty string, but makes the method signature and code more complex.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk, I'm indifferent, don't have any issue with supporting false either

): string {
if ($string === null) {
return '';
Expand All @@ -986,7 +994,12 @@ public static function short(
return $string;
}

return static::substr($string, 0, $length) . $appendix;
$string = static::substr($string, 0, $length);

return match ($appendix) {
false => $string,
default => $string . $appendix
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/V.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public static function __callStatic(string $method, array $arguments): bool
* Checks if the value matches the given regular expression
*/
'match' => function ($value, string $pattern): bool {
return preg_match($pattern, $value) !== 0;
return preg_match($pattern, (string)$value) === 1;
},

/**
Expand Down