Skip to content

Commit

Permalink
feat(lexicon): specify default in constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Apr 23, 2024
1 parent a63f8bd commit 59b72ad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 105 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
'OCP\\Comments\\NotFoundException' => $baseDir . '/lib/public/Comments/NotFoundException.php',
'OCP\\Common\\Exception\\NotFoundException' => $baseDir . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\ConfigLexicon\\ConfigLexiconEntry' => $baseDir . '/lib/public/ConfigLexicon/ConfigLexiconEntry.php',
'OCP\\ConfigLexicon\\ConfigLexiconValueType' => $baseDir . '/lib/public/ConfigLexicon/ConfigLexiconValueType.php',
'OCP\\ConfigLexicon\\IConfigLexicon' => $baseDir . '/lib/public/ConfigLexicon/IConfigLexicon.php',
'OCP\\ConfigLexicon\\IConfigLexiconEntry' => $baseDir . '/lib/public/ConfigLexicon/IConfigLexiconEntry.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.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 @@ -250,6 +250,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Comments\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Comments/NotFoundException.php',
'OCP\\Common\\Exception\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\ConfigLexicon\\ConfigLexiconEntry' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/ConfigLexiconEntry.php',
'OCP\\ConfigLexicon\\ConfigLexiconValueType' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/ConfigLexiconValueType.php',
'OCP\\ConfigLexicon\\IConfigLexicon' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/IConfigLexicon.php',
'OCP\\ConfigLexicon\\IConfigLexiconEntry' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/IConfigLexiconEntry.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php',
Expand Down
66 changes: 26 additions & 40 deletions lib/public/ConfigLexicon/ConfigLexiconEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,22 @@ class ConfigLexiconEntry implements IConfigLexiconEntry {
public function __construct(
private readonly string $key,
private readonly ConfigLexiconValueType $type,
null|string|int|float|bool|array $default = null,
string $definition = '',
private readonly bool $lazy = false,
private readonly bool $sensitive = false,
private readonly bool $deprecated = false
) {
if ($default !== null) {
$this->default = match ($type) {
ConfigLexiconValueType::STRING => $this->convertFromString($default),

Check failure on line 57 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:57:64: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromString expects string, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromString expects string, but array<array-key, mixed>|scalar provided

Check failure on line 57 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:57:64: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromString expects string, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)
ConfigLexiconValueType::INT => $this->convertFromInt($default),

Check failure on line 58 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:58:58: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromInt expects int, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromInt expects int, but array<array-key, mixed>|scalar provided

Check failure on line 58 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:58:58: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromInt expects int, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)
ConfigLexiconValueType::FLOAT => $this->convertFromFloat($default),

Check failure on line 59 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:59:62: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromFloat expects float, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromFloat expects float, but array<array-key, mixed>|scalar provided

Check failure on line 59 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:59:62: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromFloat expects float, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)
ConfigLexiconValueType::BOOL => $this->convertFromBool($default),

Check failure on line 60 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:60:60: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromBool expects bool, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromBool expects bool, but array<array-key, mixed>|scalar provided

Check failure on line 60 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/public/ConfigLexicon/ConfigLexiconEntry.php:60:60: InvalidArgument: Argument 1 of OCP\ConfigLexicon\ConfigLexiconEntry::convertFromBool expects bool, but array<array-key, mixed>|scalar provided (see https://psalm.dev/004)
ConfigLexiconValueType::ARRAY => $this->convertFromArray($default)
};
}

/** @psalm-suppress UndefinedClass */
if (\OC::$CLI) { // only store definition if ran from CLI
$this->definition = $definition;
Expand Down Expand Up @@ -79,68 +90,43 @@ public function getValueType(): ConfigLexiconValueType {
}

/**
* @inheritDoc
*
* @param string $default
*
* @return self
* @since 30.0.0
* @return string
*/
public function withDefaultString(string $default): self {
$this->default = $default;
return $this;
private function convertFromString(string $default): string {

Check failure on line 96 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/ConfigLexicon/ConfigLexiconEntry.php:96:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
return $default;
}

/**
* @inheritDoc
*
* @param int $default
*
* @return self
* @since 30.0.0
* @return string
*/
public function withDefaultInt(int $default): self {
$this->default = (string) $default;
return $this;
private function convertFromInt(int $default): string {

Check failure on line 104 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/ConfigLexicon/ConfigLexiconEntry.php:104:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
return (string) $default;
}

/**
* @inheritDoc
*
* @param float $default
*
* @return self
* @since 30.0.0
* @return string
*/
public function withDefaultFloat(float $default): self {
$this->default = (string) $default;
return $this;
private function convertFromFloat(float $default): string {

Check failure on line 112 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/ConfigLexicon/ConfigLexiconEntry.php:112:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
return (string) $default;
}

/**
* @inheritDoc
*
* @param bool $default
*
* @return self
* @since 30.0.0
* @return string
*/
public function withDefaultBool(bool $default): self {
$this->default = ($default) ? '1' : '0';
return $this;
private function convertFromBool(bool $default): string {

Check failure on line 120 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/ConfigLexicon/ConfigLexiconEntry.php:120:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
return ($default) ? '1' : '0';
}

/**
* @inheritDoc
*
* @param array $default
*
* @return self
* @since 30.0.0
* @return string
*/
public function withDefaultArray(array $default): self {
$this->default = json_encode($default);
return $this;
private function convertFromArray(array $default): string {

Check failure on line 128 in lib/public/ConfigLexicon/ConfigLexiconEntry.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/ConfigLexicon/ConfigLexiconEntry.php:128:2: InvalidDocblock: @SInCE is required for methods in OCP. (see https://psalm.dev/008)
return json_encode($default);
}

/**
Expand Down
44 changes: 44 additions & 0 deletions lib/public/ConfigLexicon/ConfigLexiconValueType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Maxence Lange <[email protected]>
*
* @author Maxence Lange <[email protected]>
*
* @license AGPL-3.0 or later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCP\ConfigLexicon;

/**
* Listing of available value type for config lexicon
*
* @see IConfigLexicon
* @since 30.0.0
*/
enum ConfigLexiconValueType: string {
/** @since 30.0.0 */
case STRING = 'string';
/** @since 30.0.0 */
case INT = 'int';
/** @since 30.0.0 */
case FLOAT = 'float';
/** @since 30.0.0 */
case BOOL = 'bool';
/** @since 30.0.0 */
case ARRAY = 'array';
}
65 changes: 0 additions & 65 deletions lib/public/ConfigLexicon/IConfigLexiconEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,13 @@

use OCP\IAppConfig;

enum ConfigLexiconValueType {
/** @since 30.0.0 */
case STRING;
/** @since 30.0.0 */
case INT;
/** @since 30.0.0 */
case FLOAT;
/** @since 30.0.0 */
case BOOL;
/** @since 30.0.0 */
case ARRAY;
}

/**
* Model that represent config values within an app config lexicon.
*
* @see IConfigLexicon
* @since 30.0.0
*/
interface IConfigLexiconEntry {

/**
* returns the config key.
*
Expand All @@ -64,56 +50,6 @@ public function getKey(): string;
*/
public function getValueType(): ConfigLexiconValueType;

/**
* set default value (as string) for config value.
*
* @param string $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultString(string $default): self;

/**
* set default value (as int) for config value.
*
* @param int $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultInt(int $default): self;

/**
* set default value (as float) for config value.
*
* @param float $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultFloat(float $default): self;

/**
* set default value (as bool) for config value.
*
* @param bool $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultBool(bool $default): self;

/**
* set default value (as array) for config value.
*
* @param array $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultArray(array $default): self;

/**
* returns the default value set for this config key.
* default value is returned as string or NULL if not set.
Expand All @@ -123,7 +59,6 @@ public function withDefaultArray(array $default): self;
*/
public function getDefault(): ?string;


/**
* returns the description for config key, only available when process is initiated from occ.
* returns empty string if not set or if process is not initiated from occ.
Expand Down

0 comments on commit 59b72ad

Please sign in to comment.