Skip to content

Commit 4f5d9f1

Browse files
authored
Rename $options parameter to $attributes (#269)
1 parent a3d9d65 commit 4f5d9f1

4 files changed

Lines changed: 137 additions & 131 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Chg #221: Throw `LogicException` in `Tag::id()` when id is empty string (@razvbir)
66
- Chg #267: Make all `CheckboxItem` and `RadioItem` properties required (@vjik)
77
- Chg #234: Remove tag attributes sorting (@FrankiFixx)
8+
- Chg #269: Rename `$options` parameter to `$attributes` in `Html::addCssClass()`, `Html::removeCssClass()`,
9+
`Html::addCssStyle()` and `Html::removeCssStyle()` methods (@vjik)
810

911
## 3.13.0 March 13, 2026
1012

UPGRADE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ application when you upgrade the package from one version to another.
1717
according to a predefined priority list (`type`, `id`, `class`, `name`, `value`, etc.). Now attributes are rendered
1818
in the order they are set. If your code or tests depend on a specific attribute order in the rendered HTML, you need
1919
to update them.
20+
- The `$options` parameter has been renamed to `$attributes` in the following methods: `Html::addCssClass()`,
21+
`Html::removeCssClass()`, `Html::addCssStyle()` and `Html::removeCssStyle()`. If you use named arguments when
22+
calling these methods, update them accordingly.

src/Html.php

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
* Html provides a set of static methods for generating commonly used HTML tags.
101101
*
102102
* Nearly all the methods in this class allow setting additional HTML attributes for the HTML tags they generate.
103-
* You can specify, for example, `class`, `style` or `id` for an HTML element using the `$options` parameter. See the
103+
* You can specify, for example, `class`, `style` or `id` for an HTML element using the `$attributes` parameter. See the
104104
* documentation of the {@see tag()} method for more details.
105105
*/
106106
final class Html
@@ -1695,28 +1695,29 @@ public static function renderTagAttributes(array $attributes): string
16951695
}
16961696

16971697
/**
1698-
* Adds a CSS class (or several classes) to the specified options.
1698+
* Adds a CSS class (or several classes) to the specified attributes.
16991699
*
1700-
* If the CSS class is already in the options, it will not be added again. If class specification at given options
1701-
* is an array, and some class placed there with the named (string) key, overriding of such key will have no
1702-
* effect. For example:
1700+
* If the CSS class is already in the attributes, it will not be added again. If class specification at given
1701+
* attributes is an array, and some class placed there with the named (string) key, overriding of such key will have
1702+
* no effect. For example:
17031703
*
17041704
* ```php
1705-
* $options = ['class' => ['persistent' => 'initial']];
1705+
* $attributes = ['class' => ['persistent' => 'initial']];
17061706
*
17071707
* // ['class' => ['persistent' => 'initial']];
1708-
* Html::addCssClass($options, ['persistent' => 'override']);
1708+
* Html::addCssClass($attributes, ['persistent' => 'override']);
17091709
* ```
17101710
*
17111711
* @see removeCssClass()
17121712
*
1713-
* @param array $options The options to be modified. All string values in the array must be valid UTF-8 strings.
1713+
* @param array $attributes The attributes to be modified. All string values in the array must be valid UTF-8
1714+
* strings.
17141715
* @param BackedEnum|BackedEnum[]|null[]|string|string[]|null $class The CSS class(es) to be added. Null values will
17151716
* be ignored.
17161717
*
17171718
* @psalm-param BackedEnum|string|array<array-key,BackedEnum|string|null>|null $class
17181719
*/
1719-
public static function addCssClass(array &$options, BackedEnum|array|string|null $class): void
1720+
public static function addCssClass(array &$attributes, BackedEnum|array|string|null $class): void
17201721
{
17211722
if ($class === null) {
17221723
return;
@@ -1746,83 +1747,83 @@ public static function addCssClass(array &$options, BackedEnum|array|string|null
17461747
$class = $filteredClass;
17471748
}
17481749

1749-
if (isset($options['class'])) {
1750-
if (is_array($options['class'])) {
1751-
/** @psalm-var string[] $options['class'] */
1752-
$options['class'] = self::mergeCssClasses($options['class'], (array) $class);
1750+
if (isset($attributes['class'])) {
1751+
if (is_array($attributes['class'])) {
1752+
/** @psalm-var string[] $attributes['class'] */
1753+
$attributes['class'] = self::mergeCssClasses($attributes['class'], (array) $class);
17531754
} else {
17541755
/**
1755-
* @psalm-var string $options['class']
1756-
* @var string[] $classes We assume that `$options['class']` is valid UTF-8 string, so `preg_split()`
1756+
* @psalm-var string $attributes['class']
1757+
* @var string[] $classes We assume that `$attributes['class']` is valid UTF-8 string, so `preg_split()`
17571758
* never returns `false`.
17581759
*/
1759-
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
1760+
$classes = preg_split('/\s+/', $attributes['class'], -1, PREG_SPLIT_NO_EMPTY);
17601761
$classes = self::mergeCssClasses($classes, (array) $class);
1761-
$options['class'] = is_array($class) ? $classes : implode(' ', $classes);
1762+
$attributes['class'] = is_array($class) ? $classes : implode(' ', $classes);
17621763
}
17631764
} else {
1764-
$options['class'] = $class;
1765+
$attributes['class'] = $class;
17651766
}
17661767
}
17671768

17681769
/**
1769-
* Removes a CSS class from the specified options.
1770+
* Removes a CSS class from the specified attributes.
17701771
*
17711772
* @see addCssClass()
17721773
*
1773-
* @param array $options The options to be modified.
1774+
* @param array $attributes The attributes to be modified.
17741775
* @param string|string[] $class The CSS class(es) to be removed.
17751776
*/
1776-
public static function removeCssClass(array &$options, string|array $class): void
1777+
public static function removeCssClass(array &$attributes, string|array $class): void
17771778
{
1778-
if (isset($options['class'])) {
1779-
if (is_array($options['class'])) {
1780-
$classes = array_diff($options['class'], (array) $class);
1779+
if (isset($attributes['class'])) {
1780+
if (is_array($attributes['class'])) {
1781+
$classes = array_diff($attributes['class'], (array) $class);
17811782
if (empty($classes)) {
1782-
unset($options['class']);
1783+
unset($attributes['class']);
17831784
} else {
1784-
$options['class'] = $classes;
1785+
$attributes['class'] = $classes;
17851786
}
17861787
} else {
17871788
/** @var string[] */
1788-
$classes = preg_split('/\s+/', (string) $options['class'], -1, PREG_SPLIT_NO_EMPTY);
1789+
$classes = preg_split('/\s+/', (string) $attributes['class'], -1, PREG_SPLIT_NO_EMPTY);
17891790
$classes = array_diff($classes, (array) $class);
17901791
if (empty($classes)) {
1791-
unset($options['class']);
1792+
unset($attributes['class']);
17921793
} else {
1793-
$options['class'] = implode(' ', $classes);
1794+
$attributes['class'] = implode(' ', $classes);
17941795
}
17951796
}
17961797
}
17971798
}
17981799

17991800
/**
1800-
* Adds the specified CSS styles to the HTML options.
1801+
* Adds the specified CSS styles to the HTML attributes.
18011802
*
1802-
* If the options already contain a `style` element, the new style will be merged
1803+
* If the attributes already contain a `style` element, the new style will be merged
18031804
* with the existing one. If a CSS property exists in both the new and the old styles,
18041805
* the old one may be overwritten if `$overwrite` is true.
18051806
*
18061807
* For example,
18071808
*
18081809
* ```php
1809-
* Html::addCssStyle($options, 'width: 100px; height: 200px');
1810+
* Html::addCssStyle($attributes, 'width: 100px; height: 200px');
18101811
* ```
18111812
*
18121813
* @see removeCssStyle()
18131814
*
1814-
* @param array $options The HTML options to be modified.
1815+
* @param array $attributes The HTML attributes to be modified.
18151816
* @param string|string[] $style The new style string (e.g. `'width: 100px; height: 200px'`) or array
18161817
* (e.g. `['width' => '100px', 'height' => '200px']`).
1817-
* @param bool $overwrite Whether to overwrite existing CSS properties if the new style contain them too.
1818+
* @param bool $overwrite Whether to overwrite existing CSS properties if the new style contains them too.
18181819
*
18191820
* @psalm-param array<string, string>|string $style
18201821
*/
1821-
public static function addCssStyle(array &$options, array|string $style, bool $overwrite = true): void
1822+
public static function addCssStyle(array &$attributes, array|string $style, bool $overwrite = true): void
18221823
{
1823-
if (!empty($options['style'])) {
1824-
/** @psalm-var array<string,string>|string $options['style'] */
1825-
$oldStyle = is_array($options['style']) ? $options['style'] : self::cssStyleToArray($options['style']);
1824+
if (!empty($attributes['style'])) {
1825+
/** @psalm-var array<string,string>|string $attributes['style'] */
1826+
$oldStyle = is_array($attributes['style']) ? $attributes['style'] : self::cssStyleToArray($attributes['style']);
18261827
$newStyle = is_array($style) ? $style : self::cssStyleToArray($style);
18271828
if (!$overwrite) {
18281829
foreach ($newStyle as $property => $_value) {
@@ -1833,33 +1834,33 @@ public static function addCssStyle(array &$options, array|string $style, bool $o
18331834
}
18341835
$style = array_merge($oldStyle, $newStyle);
18351836
}
1836-
$options['style'] = is_array($style) ? self::cssStyleFromArray($style) : $style;
1837+
$attributes['style'] = is_array($style) ? self::cssStyleFromArray($style) : $style;
18371838
}
18381839

18391840
/**
1840-
* Removes the specified CSS styles from the HTML options.
1841+
* Removes the specified CSS styles from the HTML attributes.
18411842
*
18421843
* For example,
18431844
*
18441845
* ```php
1845-
* Html::removeCssStyle($options, ['width', 'height']);
1846+
* Html::removeCssStyle($attributes, ['width', 'height']);
18461847
* ```
18471848
*
18481849
* @see addCssStyle()
18491850
*
1850-
* @param array $options The HTML options to be modified.
1851+
* @param array $attributes The HTML attributes to be modified.
18511852
* @param string|string[] $properties The CSS properties to be removed. You may use a string if you are removing a
18521853
* single property.
18531854
*/
1854-
public static function removeCssStyle(array &$options, string|array $properties): void
1855+
public static function removeCssStyle(array &$attributes, string|array $properties): void
18551856
{
1856-
if (!empty($options['style'])) {
1857-
/** @psalm-var array<string,string>|string $options['style'] */
1858-
$style = is_array($options['style']) ? $options['style'] : self::cssStyleToArray($options['style']);
1857+
if (!empty($attributes['style'])) {
1858+
/** @psalm-var array<string,string>|string $attributes['style'] */
1859+
$style = is_array($attributes['style']) ? $attributes['style'] : self::cssStyleToArray($attributes['style']);
18591860
foreach ((array) $properties as $property) {
18601861
unset($style[$property]);
18611862
}
1862-
$options['style'] = self::cssStyleFromArray($style);
1863+
$attributes['style'] = self::cssStyleFromArray($style);
18631864
}
18641865
}
18651866

0 commit comments

Comments
 (0)