Skip to content

Commit ac547b3

Browse files
committed
Enhance quote function to support int arguments
1 parent 395e40c commit ac547b3

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ Returns current timestamp in UTC timezone in ISO8601 format.
614614
## quote()
615615

616616
```php
617-
quote(string $arg): string
617+
quote(string|int $arg): string
618618
```
619619

620620
Quotes a string for safe use as a shell argument using ANSI-C $'...' syntax.

src/functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,9 @@ function timestamp(): string
992992
* run('echo ' . quote("it's a test")); // echo $'it\'s a test'
993993
* ```
994994
*/
995-
function quote(string $arg): string
995+
function quote(string|int $arg): string
996996
{
997+
$arg = (string) $arg;
997998
if ($arg === '') {
998999
return "\$''";
9991000
}

tests/src/QuoteTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,24 @@ public function testShellInjectionAttempts()
127127
self::assertEquals("\$'`rm -rf /`'", quote('`rm -rf /`'));
128128
self::assertEquals("\$'\$(cat /etc/passwd)'", quote('$(cat /etc/passwd)'));
129129
}
130+
131+
public function testIntPositive()
132+
{
133+
self::assertEquals('42', quote(42));
134+
}
135+
136+
public function testIntZero()
137+
{
138+
self::assertEquals('0', quote(0));
139+
}
140+
141+
public function testIntNegative()
142+
{
143+
self::assertEquals('-7', quote(-7));
144+
}
145+
146+
public function testIntMax()
147+
{
148+
self::assertEquals((string) PHP_INT_MAX, quote(PHP_INT_MAX));
149+
}
130150
}

0 commit comments

Comments
 (0)