@@ -34,12 +34,20 @@ final class TextDirective implements Directive
3434 (?:,-(?<suffix>.+))? # optional suffix (to end)
3535 $/x ' ;
3636
37+ /**
38+ * @param non-empty-string $start
39+ * @param ?non-empty-string $end
40+ * @param ?non-empty-string $prefix
41+ * @param ?non-empty-string $suffix
42+ */
3743 public function __construct (
3844 public readonly string $ start ,
3945 public readonly ?string $ end = null ,
4046 public readonly ?string $ prefix = null ,
4147 public readonly ?string $ suffix = null ,
4248 ) {
49+ ('' !== $ this ->start && '' !== $ this ->end && '' !== $ this ->prefix && '' !== $ this ->suffix )
50+ || throw new SyntaxError ('The start part can not be the empty string. ' );
4351 }
4452
4553 /**
@@ -64,18 +72,20 @@ public static function fromValue(Stringable|string $text): self
6472 $ matches ['prefix ' ] = null ;
6573 }
6674
67- $ matches ['suffix ' ] ??= null ;
75+ /** @var non-empty-string $start */
76+ $ start = (string ) self ::decode ($ matches ['start ' ]);
77+ /** @var ?non-empty-string $prefix */
78+ $ prefix = self ::decode ($ matches ['prefix ' ]);
79+ /** @var ?non-empty-string $suffix */
80+ $ suffix = self ::decode ($ matches ['suffix ' ] ?? null );
6881 $ matches ['end ' ] ??= null ;
6982 if ('' === $ matches ['end ' ]) {
7083 $ matches ['end ' ] = null ;
7184 }
85+ /** @var ?non-empty-string $end */
86+ $ end = self ::decode ($ matches ['end ' ]);
7287
73- return new self (
74- (string ) self ::decode ($ matches ['start ' ]),
75- self ::decode ($ matches ['end ' ]),
76- self ::decode ($ matches ['prefix ' ]),
77- self ::decode ($ matches ['suffix ' ]),
78- );
88+ return new self ($ start , $ end , $ prefix , $ suffix );
7989 }
8090
8191 private static function encode (?string $ value ): ?string
@@ -85,7 +95,11 @@ private static function encode(?string $value): ?string
8595
8696 private static function decode (?string $ value ): ?string
8797 {
88- return null !== $ value ? str_replace ('%20 ' , ' ' , (string ) Encoder::decodeFragment ($ value )) : null ;
98+ if (null === $ value ) {
99+ return null ;
100+ }
101+
102+ return str_replace ('%20 ' , ' ' , (string ) Encoder::decodeFragment ($ value ));
89103 }
90104
91105 public function name (): string
@@ -162,6 +176,8 @@ public function equals(mixed $directive): bool
162176 *
163177 * This method MUST retain the state of the current instance, and return
164178 * an instance that contains the new start portion.
179+ *
180+ * @param non-empty-string $text
165181 */
166182 public function startsWith (string $ text ): self
167183 {
@@ -179,6 +195,8 @@ public function startsWith(string $text): self
179195 *
180196 * This method MUST retain the state of the current instance, and return
181197 * an instance that contains the new end portion.
198+ *
199+ * @param ?non-empty-string $text
182200 */
183201 public function endsWith (?string $ text ): self
184202 {
@@ -196,6 +214,8 @@ public function endsWith(?string $text): self
196214 *
197215 * This method MUST retain the state of the current instance, and return
198216 * an instance that contains the new suffix portion.
217+ *
218+ * @param ?non-empty-string $text
199219 */
200220 public function followedBy (?string $ text ): self
201221 {
@@ -211,6 +231,8 @@ public function followedBy(?string $text): self
211231 *
212232 * This method MUST retain the state of the current instance, and return
213233 * an instance that contains the new prefix portion.
234+ *
235+ * @param ?non-empty-string $text
214236 */
215237 public function precededBy (?string $ text ): self
216238 {
0 commit comments