Skip to content

Commit d5659fb

Browse files
authored
Clarify psalm types for Html::generateId() and Tag::id() (#220)
1 parent 4a07b1d commit d5659fb

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 3.7.1 under development
44

5-
- no changes in this release.
5+
- Enh #220: Add `non-empty-string` psalm type of `Html::generateId()` method result (@vjik)
6+
- Enh #220: Add `non-empty-string|null` psalm type of `Tag::id()` method parameter (@vjik)
67

78
## 3.7.0 September 18, 2024
89

src/Html.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ final class Html
155155
* Returns an autogenerated sequential ID.
156156
*
157157
* @return string Autogenerated ID.
158+
*
159+
* @psalm-return non-empty-string
158160
*/
159161
public static function generateId(string $prefix = 'i'): string
160162
{

src/Tag/Base/Tag.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ final public function attribute(string $name, mixed $value): static
6868
* Set tag ID.
6969
*
7070
* @param string|null $id Tag ID.
71+
*
72+
* @psalm-param non-empty-string|null $id
7173
*/
7274
final public function id(?string $id): static
7375
{

src/Tag/Input/Range.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ final class Range extends InputTag
2424
*/
2525
private string $outputTag = 'span';
2626
private array $outputAttributes = [];
27+
28+
/**
29+
* @psalm-var non-empty-string|null
30+
*/
2731
private ?string $outputId = null;
2832

2933
/**
@@ -111,7 +115,7 @@ protected function prepareAttributes(): void
111115
$this->attributes['type'] = 'range';
112116

113117
if ($this->showOutput) {
114-
$this->outputId = (string) ($this->outputAttributes['id'] ?? Html::generateId('rangeOutput'));
118+
$this->fillOutputId();
115119
$this->attributes['oninput'] = 'document.getElementById("' . $this->outputId . '").innerHTML=this.value';
116120
}
117121
}
@@ -128,4 +132,13 @@ protected function after(): string
128132
->id($this->outputId)
129133
->render();
130134
}
135+
136+
/**
137+
* @psalm-assert non-empty-string $this->outputId
138+
*/
139+
private function fillOutputId(): void
140+
{
141+
$id = (string) ($this->outputAttributes['id'] ?? '');
142+
$this->outputId = $id === '' ? Html::generateId('rangeOutput') : $id;
143+
}
131144
}

0 commit comments

Comments
 (0)