Skip to content

Commit fbd632d

Browse files
authored
Merge pull request #6058 from paulbalandan/slash-item
Fix `slash_item()` erroring when property fetched does not exist on `Config\App`
2 parents 1027f8b + 42563c2 commit fbd632d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

system/Common.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,10 +1013,26 @@ function single_service(string $name, ...$params)
10131013
*/
10141014
function slash_item(string $item): ?string
10151015
{
1016-
$config = config(App::class);
1016+
$config = config(App::class);
1017+
1018+
if (! property_exists($config, $item)) {
1019+
return null;
1020+
}
1021+
10171022
$configItem = $config->{$item};
10181023

1019-
if (! isset($configItem) || empty(trim($configItem))) {
1024+
if (! is_scalar($configItem)) {
1025+
throw new RuntimeException(sprintf(
1026+
'Cannot convert "%s::$%s" of type "%s" to type "string".',
1027+
App::class,
1028+
$item,
1029+
gettype($configItem)
1030+
));
1031+
}
1032+
1033+
$configItem = trim((string) $configItem);
1034+
1035+
if ($configItem === '') {
10201036
return $configItem;
10211037
}
10221038

tests/system/CommonFunctionsTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Config\Modules;
3131
use Config\Services;
3232
use Kint;
33+
use RuntimeException;
3334
use stdClass;
3435
use Tests\Support\Models\JobModel;
3536

@@ -392,9 +393,28 @@ public function testReallyWritable()
392393

393394
public function testSlashItem()
394395
{
395-
$this->assertSame('/', slash_item('cookiePath')); // slash already there
396-
$this->assertSame('', slash_item('cookieDomain')); // empty, so untouched
397-
$this->assertSame('en/', slash_item('defaultLocale')); // slash appended
396+
$this->assertSame('/', slash_item('cookiePath')); // /
397+
$this->assertSame('', slash_item('cookieDomain')); // ''
398+
$this->assertSame('en/', slash_item('defaultLocale')); // en
399+
$this->assertSame('7200/', slash_item('sessionExpiration')); // int 7200
400+
$this->assertSame('', slash_item('negotiateLocale')); // false
401+
$this->assertSame('1/', slash_item('cookieHTTPOnly')); // true
402+
}
403+
404+
public function testSlashItemOnInexistentItem()
405+
{
406+
$this->assertNull(slash_item('foo'));
407+
$this->assertNull(slash_item('bar'));
408+
$this->assertNull(slash_item('cookieDomains'));
409+
$this->assertNull(slash_item('indices'));
410+
}
411+
412+
public function testSlashItemThrowsErrorOnNonStringableItem()
413+
{
414+
$this->expectException(RuntimeException::class);
415+
$this->expectExceptionMessage('Cannot convert "Config\\App::$supportedLocales" of type "array" to type "string".');
416+
417+
slash_item('supportedLocales');
398418
}
399419

400420
protected function injectSessionMock()

0 commit comments

Comments
 (0)