-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(form): Add ariaDescribedBySuffix property and update getAttributes method for improved accessibility support.
#60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,6 +60,52 @@ final class Button extends BaseInline | |||||||||||||||||||||||||||||||||||
| use HasTabindex; | ||||||||||||||||||||||||||||||||||||
| use HasValue; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Suffix appended to the element `id` when `aria-describedby` is set to `true`. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * An empty string falls back to `'-help'` during rendering. | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| protected string $ariaDescribedBySuffix = ''; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Sets the suffix appended to the element `id` when `aria-describedby` is set to `true`. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * @param string $value Suffix to append to the element `id` for `aria-describedby`. Defaults to `'-help'`. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * @return static New instance with the updated `ariaDescribedBySuffix` value. | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| public function ariaDescribedBySuffix(string $value): static | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $new = clone $this; | ||||||||||||||||||||||||||||||||||||
| $new->ariaDescribedBySuffix = $value; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return $new; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Returns the array of HTML attributes for the element. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * @return array Attributes array assigned to the element. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * @phpstan-return mixed[] | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| public function getAttributes(): array | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $attributes = parent::getAttributes(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** @phpstan-var string|null $id */ | ||||||||||||||||||||||||||||||||||||
| $id = $this->getAttribute('id', null); | ||||||||||||||||||||||||||||||||||||
| $ariaDescribedBy = $this->getAttribute('aria-describedby', null); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| $ariaDescribedBySuffix = $this->ariaDescribedBySuffix === '' ? '-help' : "-{$this->ariaDescribedBySuffix}"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if ($ariaDescribedBy === true || $ariaDescribedBy === 'true') { | ||||||||||||||||||||||||||||||||||||
| $attributes['aria-describedby'] = $id !== null ? "{$id}{$ariaDescribedBySuffix}" : null; | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+96
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize empty/whitespace If 🛠️ Suggested fix /** `@phpstan-var` string|null $id */
$id = $this->getAttribute('id', null);
+$id = is_string($id) && trim($id) === '' ? null : $id;
$ariaDescribedBy = $this->getAttribute('aria-describedby', null);📝 Committable suggestion
Suggested change
🧰 Tools🪛 PHPMD (2.15.0)[warning] 97-97: Avoid variables with short names like $id. Configured minimum length is 3. (undefined) (ShortVariable) [warning] 100-100: Avoid excessively long variable names like $ariaDescribedBySuffix. Keep variable name length under 20. (undefined) (LongVariable) 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 2868 🏁 Script executed: Length of output: 0 🏁 Script executed: Length of output: 0
The edge case CodeRabbit highlighted is when $id = $this->getAttribute('id', null); // Returns ''
// ...
if ($ariaDescribedBy === true || $ariaDescribedBy === 'true') {
$attributes['aria-describedby'] = $id !== null ? "{$id}{$ariaDescribedBySuffix}" : null;
// Produces: '' . '-help' = '-help' (invalid reference)
}Is this edge case already prevented elsewhere (e.g., does 🧠 Learnings used |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return $attributes; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Sets the `type` attribute. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,52 @@ final class Form extends BaseBlock | |
| use HasRel; | ||
| use HasTarget; | ||
|
|
||
| /** | ||
| * Suffix appended to the element `id` when `aria-describedby` is set to `true`. | ||
| * | ||
| * An empty string falls back to `'-help'` during rendering. | ||
| */ | ||
| protected string $ariaDescribedBySuffix = ''; | ||
|
|
||
| /** | ||
| * Sets the suffix appended to the element `id` when `aria-describedby` is set to `true`. | ||
| * | ||
| * @param string $value Suffix to append to the element `id` for `aria-describedby`. Defaults to `'-help'`. | ||
| * | ||
| * @return static New instance with the updated `ariaDescribedBySuffix` value. | ||
| */ | ||
| public function ariaDescribedBySuffix(string $value): static | ||
| { | ||
| $new = clone $this; | ||
| $new->ariaDescribedBySuffix = $value; | ||
|
|
||
| return $new; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the array of HTML attributes for the element. | ||
| * | ||
| * @return array Attributes array assigned to the element. | ||
| * | ||
| * @phpstan-return mixed[] | ||
| */ | ||
| public function getAttributes(): array | ||
| { | ||
| $attributes = parent::getAttributes(); | ||
|
|
||
| /** @phpstan-var string|null $id */ | ||
| $id = $this->getAttribute('id', null); | ||
| $ariaDescribedBy = $this->getAttribute('aria-describedby', null); | ||
|
|
||
| $ariaDescribedBySuffix = $this->ariaDescribedBySuffix === '' ? '-help' : "-{$this->ariaDescribedBySuffix}"; | ||
|
|
||
| if ($ariaDescribedBy === true || $ariaDescribedBy === 'true') { | ||
| $attributes['aria-describedby'] = $id !== null ? "{$id}{$ariaDescribedBySuffix}" : null; | ||
| } | ||
|
Comment on lines
+78
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against empty IDs when composing If 🛠️ Suggested fix /** `@phpstan-var` string|null $id */
$id = $this->getAttribute('id', null);
+ $id = is_string($id) && trim($id) === '' ? null : $id;🧰 Tools🪛 PHPMD (2.15.0)[warning] 79-79: Avoid variables with short names like $id. Configured minimum length is 3. (undefined) (ShortVariable) [warning] 82-82: Avoid excessively long variable names like $ariaDescribedBySuffix. Keep variable name length under 20. (undefined) (LongVariable) 🤖 Prompt for AI Agents |
||
|
|
||
| return $attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the tag enumeration for the `<form>` element. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.