Skip to content

Commit 3499670

Browse files
authored
Merge pull request #2 from hestiacp/int-float
support int|float
2 parents 7fd1a3a + 5397908 commit 3499670

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "library",
77
"license": "Unlicense",
88
"require": {
9-
"php": ">=7.2"
9+
"php": ">=8.0"
1010
},
1111
"autoload": {
1212
"files":["src/Hestiacp/quoteshellarg/quoteshellarg.php"]

src/Hestiacp/quoteshellarg/quoteshellarg.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@
66
* quotes shell arguments
77
* (doing a better job than escapeshellarg)
88
*
9-
* @param string $arg
9+
* @param string|int|float $arg
1010
* @throws UnexpectedValueException if $arg contains null bytes
1111
* @return string
1212
*/
1313

14-
function quoteshellarg(string $arg): string
14+
function quoteshellarg(string | int | float $arg): string
1515
{
16+
if (\is_float($arg)) {
17+
// 17: >The 53-bit significand precision gives from 15 to 17 significant decimal digits precision.
18+
return \escapeshellarg(\sprintf('%.17g', $arg));
19+
}
20+
if (\is_int($arg)) {
21+
return \escapeshellarg((string) $arg);
22+
}
1623
static $isUnix = null;
1724
if ($isUnix === null) {
18-
$isUnix = in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN';
25+
$isUnix = \in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN';
1926
}
2027
if ($isUnix) {
2128
// PHP's built-in escapeshellarg() for unix is kindof garbage: https://3v4l.org/Hkv7h
2229
// corrupting-or-stripping UTF-8 unicode characters like "æøå" and non-printable characters like "\x01",
2330
// both of which are fully legal in unix shell arguments.
2431
// In single-quoted-unix-shell-arguments there are only 2 bytes that needs special attention: \x00 and \x27
25-
if (false !== strpos($arg, "\x00")) {
26-
throw new UnexpectedValueException('unix shell arguments cannot contain null bytes!');
32+
if (false !== \strpos($arg, "\x00")) {
33+
throw new \UnexpectedValueException('unix shell arguments cannot contain null bytes!');
2734
}
28-
return "'" . strtr($arg, array("'" => "'\\''")) . "'";
35+
return "'" . \strtr($arg, array("'" => "'\\''")) . "'";
2936
}
3037
// todo: quoteshellarg for windows? it's a nightmare though: https://docs.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way
3138
// fallback to php's builtin escapeshellarg
32-
return escapeshellarg($arg);
39+
return \escapeshellarg($arg);
3340
}

0 commit comments

Comments
 (0)