Skip to content
Next Next commit
refactor: split off value casting out of config:system:set command
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot[bot] committed Aug 26, 2025
commit 642a81a8d47d40d9631647fa36cd9a7f8269f15a
76 changes: 76 additions & 0 deletions core/Command/Config/System/CastHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Config\System;

class CastHelper {
/**
* @return array{value: mixed, readable-value: string}
*/
public function castValue(?string $value, string $type): array {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];

case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];

case 'boolean':
case 'bool':
$value = strtolower($value);
return match ($value) {
'true' => [
'value' => true,
'readable-value' => 'boolean ' . $value,
],
'false' => [
'value' => false,
'readable-value' => 'boolean ' . $value,
],
default => throw new \InvalidArgumentException('Unable to parse value as boolean'),
};

case 'null':
return [
'value' => null,
'readable-value' => 'null',
];

case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];

case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];

default:
throw new \InvalidArgumentException('Invalid type');
}
}
}
77 changes: 2 additions & 75 deletions core/Command/Config/System/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class SetConfig extends Base {
public function __construct(
SystemConfig $systemConfig,
private CastHelper $castHelper,
) {
parent::__construct($systemConfig);
}
Expand Down Expand Up @@ -57,7 +58,7 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output): int {
$configNames = $input->getArgument('name');
$configName = $configNames[0];
$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
$configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type'));
$updateOnly = $input->getOption('update-only');

if (count($configNames) > 1) {
Expand All @@ -80,80 +81,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

/**
* @param string $value
* @param string $type
* @return mixed
* @throws \InvalidArgumentException
*/
protected function castValue($value, $type) {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];

case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];

case 'boolean':
case 'bool':
$value = strtolower($value);
switch ($value) {
case 'true':
return [
'value' => true,
'readable-value' => 'boolean ' . $value,
];

case 'false':
return [
'value' => false,
'readable-value' => 'boolean ' . $value,
];

default:
throw new \InvalidArgumentException('Unable to parse value as boolean');
}

// no break
case 'null':
return [
'value' => null,
'readable-value' => 'null',
];

case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];

case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];

default:
throw new \InvalidArgumentException('Invalid type');
}
}

/**
* @param array $configNames
* @param mixed $existingValues
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@
'OC\\Core\\Command\\Config\\Import' => $baseDir . '/core/Command/Config/Import.php',
'OC\\Core\\Command\\Config\\ListConfigs' => $baseDir . '/core/Command/Config/ListConfigs.php',
'OC\\Core\\Command\\Config\\System\\Base' => $baseDir . '/core/Command/Config/System/Base.php',
'OC\\Core\\Command\\Config\\System\\CastHelper' => $baseDir . '/core/Command/Config/System/CastHelper.php',
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => $baseDir . '/core/Command/Config/System/DeleteConfig.php',
'OC\\Core\\Command\\Config\\System\\GetConfig' => $baseDir . '/core/Command/Config/System/GetConfig.php',
'OC\\Core\\Command\\Config\\System\\SetConfig' => $baseDir . '/core/Command/Config/System/SetConfig.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Config\\Import' => __DIR__ . '/../../..' . '/core/Command/Config/Import.php',
'OC\\Core\\Command\\Config\\ListConfigs' => __DIR__ . '/../../..' . '/core/Command/Config/ListConfigs.php',
'OC\\Core\\Command\\Config\\System\\Base' => __DIR__ . '/../../..' . '/core/Command/Config/System/Base.php',
'OC\\Core\\Command\\Config\\System\\CastHelper' => __DIR__ . '/../../..' . '/core/Command/Config/System/CastHelper.php',
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/DeleteConfig.php',
'OC\\Core\\Command\\Config\\System\\GetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/GetConfig.php',
'OC\\Core\\Command\\Config\\System\\SetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/SetConfig.php',
Expand Down
69 changes: 69 additions & 0 deletions tests/Core/Command/Config/System/CastHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace Core\Command\Config\System;

use OC\Core\Command\Config\System\CastHelper;
use Test\TestCase;

class CastHelperTest extends TestCase {
private CastHelper $castHelper;

protected function setUp(): void {
parent::setUp();
$this->castHelper = new CastHelper();
}

public static function castValueProvider(): array {
return [
[null, 'string', ['value' => '', 'readable-value' => 'empty string']],

['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],

['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],

['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],

['', 'null', ['value' => null, 'readable-value' => 'null']],

['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
];
}

/**
* @dataProvider castValueProvider
*/
public function testCastValue($value, $type, $expectedValue): void {
$this->assertSame(
$expectedValue,
$this->castHelper->castValue($value, $type)
);
}

public static function castValueInvalidProvider(): array {
return [
['123', 'foobar'],

[null, 'integer'],
['abc', 'integer'],
['76ggg', 'double'],
['true', 'float'],
['foobar', 'boolean'],
];
}

/**
* @dataProvider castValueInvalidProvider
*/
public function testCastValueInvalid($value, $type): void {
$this->expectException(\InvalidArgumentException::class);

$this->castHelper->castValue($value, $type);
}
}
3 changes: 2 additions & 1 deletion tests/Core/Command/Config/System/SetConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Tests\Core\Command\Config\System;

use OC\Core\Command\Config\System\CastHelper;
use OC\Core\Command\Config\System\SetConfig;
use OC\SystemConfig;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -35,7 +36,7 @@ protected function setUp(): void {
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();

/** @var \OC\SystemConfig $systemConfig */
$this->command = new SetConfig($systemConfig);
$this->command = new SetConfig($systemConfig, new CastHelper());
}


Expand Down