Skip to content

Commit e34f2d1

Browse files
authored
Fix #221: Throw LogicException in Tag::id() when id is empty string
1 parent 3806168 commit e34f2d1

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Enh #261: Enhance `RadioList::addRadioWrapClass()` method for cleaner class addition (@vjik)
77
- Enh #263: Explicitly import classes and constants in "use" section (@mspirkov)
88
- Chg #243: Deprecate static constructors such as `TagName::tag()` in favor of `new TagName()` (@razvbir)
9+
- Chg #221: Throw `LogicException` in `Tag::id()` when id is empty string (@razvbir)
910

1011
## 3.12.0 December 13, 2025
1112

src/Tag/Base/Tag.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Html\Tag\Base;
66

77
use BackedEnum;
8+
use LogicException;
89
use Yiisoft\Html\Html;
910
use Yiisoft\Html\NoEncodeStringableInterface;
1011

@@ -75,10 +76,14 @@ final public function attribute(string $name, mixed $value): static
7576
*
7677
* @param string|null $id Tag ID.
7778
*
78-
* @psalm-param non-empty-string|null $id
79+
* @throws LogicException
7980
*/
8081
final public function id(?string $id): static
8182
{
83+
if ($id === '') {
84+
throw new LogicException('The tag id cannot be an empty string.');
85+
}
86+
8287
$new = clone $this;
8388
$new->attributes['id'] = $id;
8489
return $new;

tests/Tag/Base/TagTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Html\Tests\Tag\Base;
66

7+
use LogicException;
78
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\TestCase;
910
use Yiisoft\Html\Tests\Objects\TestTag;
@@ -132,6 +133,13 @@ public function testId(string $expected, ?string $id): void
132133
$this->assertSame($expected, (string) (new TestTag())->id($id));
133134
}
134135

136+
public function testIdEmptyString(): void
137+
{
138+
$this->expectException(LogicException::class);
139+
$this->expectExceptionMessage('The tag id cannot be an empty string.');
140+
(new TestTag())->id('');
141+
}
142+
135143
public static function dataAddClass(): array
136144
{
137145
return [

0 commit comments

Comments
 (0)