reviewer-1.2.x-dev/modules/reviewer_test_kit/src/Plugin/reviewer/Task/ConfigTaskBase.php
modules/reviewer_test_kit/src/Plugin/reviewer/Task/ConfigTaskBase.php
<?php
declare(strict_types=1);
namespace Drupal\reviewer_test_kit\Plugin\reviewer\Task;
use Drupal\Component\Utility\NestedArray;
use Drupal\reviewer\Reviewer\Result\ResultInterface;
use Drupal\reviewer\Reviewer\Task\FixableInterface;
use Drupal\reviewer\Reviewer\Task\TaskBase;
/**
* Base task for checking and fixing simple configuration values.
*
* Tasks extending this class only need to define the $configName and
* $expectedValues properties, the task takes care of checking and fixing
* values automatically.
*
* $expectedValues values can be prefixed with '!' to match anything but the
* given value, however, these values cannot be fixed automatically because the
* correct value is not defined.
*
* Here is an example of a task which checks views UI settings:
*
* @code
* #[Task('views_ui')]
* final class ViewsUi extends ConfigTaskBase {
*
* protected string $configName = 'views.settings';
*
* protected array $expectedValues = [
* 'ui.show.advanced_column' => TRUE,
* 'ui.show.default_display' => TRUE,
* 'ui.show.sql_query.enabled' => TRUE,
* 'ui.exposed_filter_any_label' => 'new_any',
* ];
*
* }
* @endcode
*/
abstract class ConfigTaskBase extends TaskBase implements FixableInterface {
use NestedConfigTrait;
/**
* The name of the configuration object this task checks.
*
* The config name is a value that can be loaded by
* \Drupal\Core\Config\ConfigFactoryInterface::get(). If the config does not
* yet exist in storage, a \LogicException will be thrown when attempting to
* fix errors.
*
* @see \Drupal\Core\Config\ConfigFactoryInterface::get
*/
protected string $configName = '';
/**
* The expected configuration values.
*
* Only listed values are checked, omitted values are ignored. Keys use the
* format expected by \Drupal\Core\Config\ConfigBase::get(). The expected
* values are set when fixing errors.
*
* @var array<string, array|scalar>
*
* @see \Drupal\Core\Config\ConfigBase::get
*/
protected array $expectedValues = [];
/**
* {@inheritdoc}
*/
public function check(): ResultInterface {
if (!$this->configName) {
throw new \LogicException(sprintf('Property %s::configName cannot be empty.', static::class));
}
if (!$this->expectedValues) {
throw new \LogicException(sprintf('Property %s::expectedValues cannot be empty.', static::class));
}
$parents = [];
$errors = [];
$config = $this->configFactory->get($this->configName)->getRawData();
foreach ($this->expectedValues($this->expectedValues, $parents) as $expected) {
$passed = NestedArray::getValue($config, $parents) === $this->stripNegation($expected);
if ($this->negated($expected)) {
$passed = !$passed;
}
if (!$passed) {
$errors[] = implode('.', $parents);
}
}
return $this->createCheckResult(
\count($errors) === 0,
sprintf('Configuration for "%s" is correct.', $this->configName),
sprintf('Configuration for "%s" is incorrect: %s.', $this->configName, implode(', ', $errors)),
);
}
/**
* {@inheritdoc}
*/
public function fix(): ResultInterface {
$config = $this->configFactory->getEditable($this->configName);
if ($config->isNew()) {
throw new \LogicException(sprintf('Task %s cannot fix configuration "%s" because it does not exist.', static::class, $this->configName));
}
$data = $config->getRawData();
$negated = [];
$parents = [];
foreach ($this->expectedValues($this->expectedValues, $parents) as $expected) {
// Negated values cannot be corrected, because we can only tell what a
// correct value is not, what a correct value is.
if ($this->negated($expected)) {
$negated[] = implode('.', $parents);
continue;
}
NestedArray::setValue($data, $parents, $expected);
}
$config->setData($data)->save();
$failure_text = \count($negated) === 0
? sprintf('Unable to fix all "%s" configuration.', $this->configName)
: sprintf('Unable to fix configuration "%s" due to negated values.', implode('", "', $negated));
return $this->createFixResult(
$this->check()->getStatus(),
sprintf('Fixed all "%s" configuration.', $this->configName),
$failure_text,
);
}
}
