Skip to content

Commit

Permalink
♻️ reduce the Cognitive Complexity of the addGroups method
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Nov 26, 2024
1 parent a2d1d96 commit 5efc794
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
41 changes: 27 additions & 14 deletions classes/Robotstxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,40 @@ private function addGroups(mixed $groups = null): self
$groups = ['*' => ['disallow' => ['/']]];
}
if (is_array($groups)) {
foreach ($groups as $useragent => $group) {
$this->txt[] = 'user-agent: '.$useragent;
if (! is_array($group)) {
continue;
}
foreach ($group as $field => $values) {
if (! is_array($values)) {
continue;
}
foreach ($values as $value) {
$this->txt[] = implode('', [$field, ': ', $value]);
}
}
}
$this->processGroupsArray($groups);
} elseif (is_string($groups)) {
$this->txt[] = $groups;
}

return $this;
}

private function processGroupsArray(array $groups): void
{
foreach ($groups as $useragent => $group) {
$this->txt[] = 'user-agent: '.$useragent;
if (is_array($group)) {
$this->processGroupFields($group);
}
}
}

private function processGroupFields(array $group): void
{
foreach ($group as $field => $values) {
if (is_array($values)) {
$this->processFieldValues($field, $values);
}
}
}

private function processFieldValues(string $field, array $values): void
{
foreach ($values as $value) {
$this->txt[] = implode('', [$field, ': ', $value]);
}
}

private function hasSitemapFromKnownPlugin(): bool
{
return count(array_filter([
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bnomei/kirby3-robots-txt",
"type": "kirby-plugin",
"description": "Manage a virtual robots.txt from the Kirby config file",
"version": "5.0.1",
"version": "5.0.2",
"license": "MIT",
"authors": [
{
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'bnomei/kirby3-robots-txt',
'pretty_version' => '5.0.1',
'version' => '5.0.1.0',
'pretty_version' => '5.0.2',
'version' => '5.0.2.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand All @@ -11,8 +11,8 @@
),
'versions' => array(
'bnomei/kirby3-robots-txt' => array(
'pretty_version' => '5.0.1',
'version' => '5.0.1.0',
'pretty_version' => '5.0.2',
'version' => '5.0.2.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit 5efc794

Please sign in to comment.