Skip to content

Commit 5ff2a29

Browse files
committed
refactor(a11y): streamline condition checks in accessibility rules for improved readability
1 parent 27b8049 commit 5ff2a29

7 files changed

Lines changed: 22 additions & 18 deletions

src/Rules/Aria/AriaErrorMessageIdExistsRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
3030

3131
foreach ($refs as $ref) {
3232
$refId = $this->firstMatch($ref, 1, 2);
33-
if ('' === $refId || isset($ids[$refId])) {
33+
if ('' === $refId) {
34+
continue;
35+
}
36+
if (isset($ids[$refId])) {
3437
continue;
3538
}
3639

src/Rules/Forms/InvalidFieldErrorMessageRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
5151
if (!$this->isSupportedField($tagName, $tag)) {
5252
continue;
5353
}
54-
55-
if ($this->hasNonEmptyReference($tag, 'aria-describedby') || $this->hasNonEmptyReference($tag, 'aria-errormessage')) {
54+
if ($this->hasNonEmptyReference($tag, 'aria-describedby')) {
55+
continue;
56+
}
57+
if ($this->hasNonEmptyReference($tag, 'aria-errormessage')) {
5658
continue;
5759
}
5860

src/Rules/Forms/RadioGroupAccessibleNameRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
5858
if ($radioCount < 2) {
5959
continue;
6060
}
61-
62-
if ($this->hasNonEmptyReference($groupBlock, 'aria-labelledby') || $this->hasNonEmptyReference($groupBlock, 'aria-label')) {
61+
if ($this->hasNonEmptyReference($groupBlock, 'aria-labelledby')) {
62+
continue;
63+
}
64+
if ($this->hasNonEmptyReference($groupBlock, 'aria-label')) {
6365
continue;
6466
}
6567

src/Rules/Forms/RadioGroupStructureRule.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
4242
if (count($items) < 2) {
4343
continue;
4444
}
45-
46-
if ($this->isInsideFieldsetGroup($full, $name) || $this->isInsideAriaRadioGroup($full, $name)) {
45+
if ($this->isInsideFieldsetGroup($full, $name)) {
46+
continue;
47+
}
48+
if ($this->isInsideAriaRadioGroup($full, $name)) {
4749
continue;
4850
}
4951

@@ -69,7 +71,7 @@ private function isInsideFieldsetGroup(string $full, string $name): bool
6971

7072
foreach ($fieldsets as $fieldset) {
7173
$content = $fieldset[1];
72-
$count = preg_match_all('/<input\b[^>]*\btype\s*=\s*(?:"radio"|\'radio\')[^>]*\bname\s*=\s*(?:"'.preg_quote($name, '/').'"|\''.preg_quote($name, '/').'\')[^>]*>/i', $content);
74+
$count = preg_match_all('/<input\b[^>]*\btype\s*=\s*(?:"radio"|\'radio\')[^>]*\bname\s*=\s*(?:"'.preg_quote($name, '/').'"|\''.preg_quote($name, '/')."')[^>]*>/i", $content);
7375
if ($count >= 2) {
7476
return true;
7577
}
@@ -86,7 +88,7 @@ private function isInsideAriaRadioGroup(string $full, string $name): bool
8688

8789
foreach ($groups as $group) {
8890
$content = $group[2];
89-
$count = preg_match_all('/<input\b[^>]*\btype\s*=\s*(?:"radio"|\'radio\')[^>]*\bname\s*=\s*(?:"'.preg_quote($name, '/').'"|\''.preg_quote($name, '/').'\')[^>]*>/i', $content);
91+
$count = preg_match_all('/<input\b[^>]*\btype\s*=\s*(?:"radio"|\'radio\')[^>]*\bname\s*=\s*(?:"'.preg_quote($name, '/').'"|\''.preg_quote($name, '/')."')[^>]*>/i", $content);
9092
if ($count >= 2) {
9193
return true;
9294
}

src/Rules/Media/SvgAccessibilityRule.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ private function hasSvgTitle(string $fullTag, Tokens $tokens, int $tokenIndex):
121121
// Look ahead in tokens for a title element within the SVG
122122
// Collect more content to find nested elements
123123
$extendedContent = $this->collectUntil($tokenIndex, $tokens, '</svg>', 500);
124-
if (preg_match('/<\s*title(\s+[^>]*)?>(.*?)<\/\s*title\s*>/is', $extendedContent)) {
125-
return true;
126-
}
127124

128-
return false;
125+
return (bool) preg_match('/<\s*title(\s+[^>]*)?>(.*?)<\/\s*title\s*>/is', $extendedContent);
129126
}
130127
}

src/Rules/Structure/DetailsSummaryRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
2626
return;
2727
}
2828

29-
foreach ($matches[0] as $index => $match) {
29+
foreach ($matches[0] as $match) {
3030
$detailsBlock = $match[0];
3131
$offset = $match[1];
3232
$line = 1 + substr_count(substr($full, 0, $offset), "\n");

src/Rules/Structure/TableCaptionMissingRule.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
3535
continue;
3636
}
3737

38-
if (preg_match('/<caption\b[^>]*>(.*?)<\/caption>/is', $inner, $captionMatch)) {
39-
if ('' !== trim(strip_tags($captionMatch[1]))) {
40-
continue;
41-
}
38+
if (preg_match('/<caption\b[^>]*>(.*?)<\/caption>/is', $inner, $captionMatch) && '' !== trim(strip_tags($captionMatch[1]))) {
39+
continue;
4240
}
4341

4442
$line = 1 + substr_count(substr($full, 0, $offset), "\n");

0 commit comments

Comments
 (0)