Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,27 +230,36 @@ public static function createFromViolinistConfigJsonString(string $data)

public function setConfig($config)
{
if (!$config instanceof \stdClass) {
return;
Comment thread
eiriksm marked this conversation as resolved.
}
$config = $this->normalizeConfigKeys($config);
foreach ($this->getDefaultConfig() as $key => $value) {
if (isset($config->{$key})) {
$this->config->{$key} = $config->{$key};
$this->configOptionsSet[$key] = true;
}
}
// Also make sure to set the block list config from the deprecated part.
// Plus alternative spelling from allow list.
if (!empty($config->rules)) {
$this->config->rules = $config->rules;
}
}

private function normalizeConfigKeys(\stdClass $config) : \stdClass
{
$config = clone $config;
$renamed_and_aliased = [
'blacklist' => 'blocklist',
'block_list' => 'blocklist',
'allowlist' => 'allow_list',
];
foreach ($renamed_and_aliased as $not_real => $real) {
if (isset($config->{$not_real})) {
$this->config->{$real} = $config->{$not_real};
$config->{$real} = $config->{$not_real};
Comment thread
eiriksm marked this conversation as resolved.
unset($config->{$not_real});
}
}
if (!empty($config->rules)) {
$this->config->rules = $config->rules;
}
return $config;
}

public function getComposerOutdatedFlag() : string
Expand Down Expand Up @@ -544,37 +553,44 @@ public function getCommitMessageConvention()

public function getConfigForRuleObject(\stdClass $rule_object)
{
// @todo: This is a bit duplication from below actually. Specifically
// the method ::getConfigForPackage
if (empty($rule_object->config)) {
return $this;
}
$new_config = clone $this->config;
$this->mergeConfigFromConfigObject($new_config, $rule_object->config);
return self::createFromViolinistConfig($new_config);
$clone = clone $this;
$clone->config = clone $this->config;
$clone->extendsStorage = clone $this->extendsStorage;
$normalized = $this->normalizeConfigKeys($rule_object->config);
foreach ($this->getDefaultConfig() as $key => $value) {
if (!isset($normalized->{$key})) {
continue;
}
$clone->config->{$key} = $normalized->{$key};
$clone->configOptionsSet[$key] = true;
$clone->extendsStorage->removeItemsForKey($key);
}
Comment thread
eiriksm marked this conversation as resolved.
return $clone;
}

public function getConfigForPackage(string $package_name) : self
{
// @todo: Consider de-duplicating with the method above
// (::getConfigForRuleObject).
$rules = $this->getRules();
if (empty($rules)) {
return $this;
}
$new_config = clone $this->config;
foreach ($this->config->rules as $rule) {
if (empty($rule->config)) {
continue;
}
$matches = $this->getMatcherFactory()->hasMatches($rule, $package_name);
if (!$matches) {
$result = $this;
foreach ($rules as $rule) {
if (!$this->getMatcherFactory()->hasMatches($rule, $package_name)) {
continue;
}
// Then merge the config for this rule.
$this->mergeConfigFromConfigObject($new_config, $rule->config);
$result = $result->getConfigForRuleObject($rule);
}
return self::createFromViolinistConfig($new_config);
if ($result === $this) {
$clone = clone $this;
$clone->config = clone $this->config;
$clone->extendsStorage = clone $this->extendsStorage;
return $clone;
}
return $result;
Comment thread
eiriksm marked this conversation as resolved.
}

public function getRules() : array
Expand All @@ -589,8 +605,12 @@ protected function mergeConfig(Config $other, $extends_name, $parent)
{
$keys_and_values_affected = $this->mergeConfigFromConfigObject($this->getConfig(), $other->getConfig());
$this->extendsStorage->addExtendItems($other->getExtendsStorage()->getExtendItems());
foreach ($this->getDefaultConfig() as $key => $value) {
if ($other->hasConfigForKey($key)) {
Comment thread
eiriksm marked this conversation as resolved.
$this->configOptionsSet[$key] = true;
}
}
foreach ($keys_and_values_affected as $key => $value) {
$this->configOptionsSet[$key] = true;
$this->extendsStorage->addExtendItem(new ExtendsChainItem($extends_name, $key, $value));
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/ExtendsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function addExtendItem(ExtendsChainItem $item)
{
$this->items[$item->getName()][$item->getKey()] = $item;
}

public function removeItemsForKey(string $key)
{
foreach ($this->items as $extend_name => $items) {
unset($this->items[$extend_name][$key]);
}
}
}
Loading
Loading