Skip to content

Commit c6c918f

Browse files
committed
Deep analysis and fixes for StringAssertions: corrected PHPDoc @param types, fixed @return types, added @var assertions for Psalm type safety
1 parent af0c774 commit c6c918f

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

src/Traits/StringAssertions.php

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ trait StringAssertions
2323
* fact('abc123')->matchesRegularExpression('/^[a-z]+\d+$/'); // Passes
2424
* fact('123abc')->matchesRegularExpression('/^[a-z]+\d+$/'); // Fails
2525
*
26-
* @param non-empty-string $pattern The regular expression pattern to match against.
27-
* @param string $message Optional custom error message.
28-
*
29-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
26+
* @param string $pattern The regular expression pattern to match against.
27+
* @param string $message Optional custom error message.
28+
*
29+
* @return self Enables fluent chaining of assertion methods.
3030
*/
3131
public function matchesRegularExpression(string $pattern, string $message = ''): self
3232
{
@@ -44,10 +44,10 @@ public function matchesRegularExpression(string $pattern, string $message = ''):
4444
* fact('123abc')->notMatchesRegularExpression('/^[a-z]+\d+$/'); // Passes
4545
* fact('abc123')->notMatchesRegularExpression('/^[a-z]+\d+$/'); // Fails
4646
*
47-
* @param non-empty-string $pattern The regular expression pattern that should not match.
48-
* @param string $message Optional custom error message.
49-
*
50-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
47+
* @param string $pattern The regular expression pattern that should not match.
48+
* @param string $message Optional custom error message.
49+
*
50+
* @return self Enables fluent chaining of assertion methods.
5151
*/
5252
public function notMatchesRegularExpression(string $pattern, string $message = ''): self
5353
{
@@ -69,10 +69,10 @@ public function notMatchesRegularExpression(string $pattern, string $message = '
6969
* fact('hello world')->containsString('world'); // Passes
7070
* fact('hello world')->containsString('foo'); // Fails
7171
*
72-
* @param non-empty-string $string The substring to search for.
73-
* @param string $message Optional custom error message.
74-
*
75-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
72+
* @param string $string The substring to search for.
73+
* @param string $message Optional custom error message.
74+
*
75+
* @return self Enables fluent chaining of assertion methods.
7676
*/
7777
public function containsString(string $string, string $message = ''): self
7878
{
@@ -160,18 +160,19 @@ public function notContainsStringIgnoringCase(string $string, string $message =
160160
* @param string $prefix The prefix to check for (must not be empty).
161161
* @param string $message Optional custom error message.
162162
*
163-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
164-
*
165-
* Example usage:
166-
* fact('hello world')->startsWith('hello'); // Passes
167-
* fact('world hello')->startsWith('hello'); // Fails
168-
*/
169-
public function startsWith(string $prefix, string $message = ''): self
163+
* @return self Enables fluent chaining of assertion methods.
164+
*
165+
* Example usage:
166+
* fact('hello world')->startsWith('hello'); // Passes
167+
* fact('world hello')->startsWith('hello'); // Fails
168+
*/
169+
public function startsWith(string $prefix, string $message = ''): self
170170
{
171171
if (strlen($prefix) === 0) {
172172
Assert::fail('Prefix cannot be an empty string.');
173173
}
174174

175+
/** @var non-empty-string $prefix */
175176
Assert::assertStringStartsWith($prefix, $this->variable, $message);
176177

177178
return $this;
@@ -185,17 +186,19 @@ public function startsWith(string $prefix, string $message = ''): self
185186
* @param string $suffix The suffix to check for (must not be empty).
186187
* @param string $message Optional custom error message.
187188
*
188-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
189-
*
190-
* Example usage:
191-
* fact('file.txt')->endsWith('.txt'); // Passes
192-
* fact('txt.file')->endsWith('.txt'); // Fails
193-
*/
194-
public function endsWith(string $suffix, string $message = ''): self
189+
* @return self Enables fluent chaining of assertion methods.
190+
*
191+
* Example usage:
192+
* fact('file.txt')->endsWith('.txt'); // Passes
193+
* fact('txt.file')->endsWith('.txt'); // Fails
194+
*/
195+
public function endsWith(string $suffix, string $message = ''): self
195196
{
196197
if (strlen($suffix) === 0) {
197198
Assert::fail('Suffix cannot be an empty string.');
198199
}
200+
201+
/** @var non-empty-string $suffix */
199202
Assert::assertStringEndsWith($suffix, $this->variable, $message);
200203

201204
return $this;
@@ -205,40 +208,40 @@ public function endsWith(string $suffix, string $message = ''): self
205208

206209
// region Length Methods
207210

208-
/**
209-
* Asserts that a string has a specific length.
210-
*
211-
* This method checks if the length of the actual string equals the expected length.
212-
*
213-
* @param int $length The expected length.
214-
* @param string $message Optional custom error message.
215-
*
216-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
217-
*
218-
* Example usage:
219-
* fact('abc')->hasLength(3); // Passes
220-
* fact('abcd')->hasLength(3); // Fails
221-
*/
211+
/**
212+
* Asserts that a string has a specific length.
213+
*
214+
* This method checks if the length of the actual string equals the expected length.
215+
*
216+
* @param int $length The expected length.
217+
* @param string $message Optional custom error message.
218+
*
219+
* @return self Enables fluent chaining of assertion methods.
220+
*
221+
* Example usage:
222+
* fact('abc')->hasLength(3); // Passes
223+
* fact('abcd')->hasLength(3); // Fails
224+
*/
222225
public function hasLength(int $length, string $message = ''): self
223226
{
224227
Assert::assertEquals($length, strlen($this->variable), $message);
225228

226229
return $this;
227230
}
228231

229-
/**
230-
* Asserts that a string is empty.
231-
*
232-
* This method checks if the actual value is an empty string.
233-
*
234-
* @param string $message Optional custom error message.
235-
*
236-
* @return self|fluentAssertions Enables fluent chaining of assertion methods.
237-
*
238-
* Example usage:
239-
* fact('')->isEmptyString(); // Passes
240-
* fact('hello')->isEmptyString(); // Fails
241-
*/
232+
/**
233+
* Asserts that a string is empty.
234+
*
235+
* This method checks if the actual value is an empty string.
236+
*
237+
* @param string $message Optional custom error message.
238+
*
239+
* @return self Enables fluent chaining of assertion methods.
240+
*
241+
* Example usage:
242+
* fact('')->isEmptyString(); // Passes
243+
* fact('hello')->isEmptyString(); // Fails
244+
*/
242245
public function isEmptyString(string $message = ''): self
243246
{
244247
Assert::assertEmpty($this->variable, $message);

0 commit comments

Comments
 (0)