Skip to content

Commit 29762be

Browse files
committed
phpstan / fix tests
1 parent 1c52279 commit 29762be

4 files changed

Lines changed: 30 additions & 27 deletions

File tree

‎src/RegExp.php‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ public function matches(string $string): bool
5757
}
5858

5959
/**
60-
* @return mixed[]
60+
* @return array<int|string, string|array<int,mixed>>|array<int,mixed>
6161
*/
6262
public function exec(string $haystack): array
6363
{
6464
return $this->findIn($haystack);
6565
}
6666

6767
/**
68-
* @return string[]
68+
* @return array<int|string, string|array<int,mixed>>|array<int,mixed>
6969
*/
7070
public function findIn(string $haystack): array
7171
{
72+
/** @var array<int|string, string|array<int,mixed>> $matches */
7273
$matches = [];
7374
call_user_func_array(
7475
$this->_method, /* @phpstan-ignore-line */
@@ -80,7 +81,6 @@ public function findIn(string $haystack): array
8081
]
8182
);
8283

83-
/* @phpstan-ignore-next-line */
8484
if (!isset($matches[1]) && isset($matches[0]) && is_array($matches[0])) {
8585
return $matches[0];
8686
}
@@ -89,12 +89,17 @@ public function findIn(string $haystack): array
8989
}
9090

9191

92-
public function replace(string $string, callable $callback): mixed
92+
/**
93+
* @param callable(string): string $callback
94+
* @return string|null
95+
*/
96+
public function replace(string $string, callable $callback): ?string
9397
{
9498
return preg_replace_callback(
9599
sprintf("/%s/%s", $this->_expr, $this->_flags),
96-
function ($hit) use ($callback) {
97-
return call_user_func($callback, $hit[0]);
100+
function (array $hit) use ($callback): string {
101+
$res = call_user_func($callback, $hit[0]);
102+
return (string) $res;
98103
},
99104
$string
100105
);

‎src/RegExpBuilder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public function notAhead(RegExpBuilder $r): RegExpBuilder
414414
return $this;
415415
}
416416

417-
public function asGroup(string $name = null): RegExpBuilder
417+
public function asGroup(?string $name = null): RegExpBuilder
418418
{
419419
$this->_capture = true;
420420
$this->_captureName = $name;

‎tests/RegExpBuilderTest.php‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ public function testRegExp(): void
2525
->getRegExp();
2626

2727

28-
$this->assertTrue(is_string($regEx->getFlags()));
29-
$this->assertTrue($regEx->getFlags() === "m");
30-
31-
$this->assertTrue(is_string($regEx->__toString()));
32-
$this->assertTrue(is_string($regEx->getExpression()));
28+
$this->assertSame("m", $regEx->getFlags());
3329
}
3430

3531
public function testMoney(): void
@@ -749,6 +745,7 @@ public function testNamedGroup(): void
749745
->exactly(3)->digits()->asGroup("numbers")
750746
->getRegExp();
751747

748+
/** @var array<string, mixed> $res */
752749
$res = $regEx->findIn("hello-123-abc");
753750

754751
$this->assertTrue(array_key_exists("numbers", $res));

‎tests/UsageExamplesTest.php‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public function testUsageExample3(): void
9292

9393
$matches = $regExp->exec($text);
9494

95-
$this->assertTrue($matches[0] === "SomeFile.pdf");
96-
$this->assertTrue($matches[1] === "doc_04.pdf");
97-
$this->assertTrue($matches[2] === "File.doc");
95+
$this->assertSame("SomeFile.pdf", $matches[0]);
96+
$this->assertSame("doc_04.pdf", $matches[1]);
97+
$this->assertSame("File.doc", $matches[2]);
9898
}
9999

100100
public function testReplace(): void
@@ -111,12 +111,13 @@ public function testReplace(): void
111111

112112
$text = $regExp->replace(
113113
$text,
114-
function ($match) {
115-
return (int)$match + 1;
114+
function (string $match): string {
115+
// convert to int, increment and cast back to string
116+
return (string)(((int)$match) + 1);
116117
}
117118
);
118119

119-
$this->assertTrue("99 bottles of beer on the wall" === $text);
120+
$this->assertSame("99 bottles of beer on the wall", $text);
120121
}
121122

122123
public function testPregMatchFlags(): void
@@ -144,16 +145,16 @@ public function testPregMatchFlags(): void
144145

145146
$matches = $regExp->exec($text);
146147

147-
$this->assertTrue(is_array($matches[0]));
148-
$this->assertTrue($matches[0][0] === "SomeFile.pdf");
149-
$this->assertTrue($matches[0][1] === 73);
148+
$this->assertIsArray($matches[0]);
149+
$this->assertSame("SomeFile.pdf", $matches[0][0]);
150+
$this->assertSame(73, $matches[0][1]);
150151

151-
$this->assertTrue(is_array($matches[1]));
152-
$this->assertTrue($matches[1][0] === "doc_04.pdf");
153-
$this->assertTrue($matches[1][1] === 226);
152+
$this->assertIsArray($matches[1]);
153+
$this->assertSame("doc_04.pdf", $matches[1][0]);
154+
$this->assertSame(226, $matches[1][1]);
154155

155-
$this->assertTrue(is_array($matches[2]));
156-
$this->assertTrue($matches[2][0] === "File.doc");
157-
$this->assertTrue($matches[2][1] === 419);
156+
$this->assertIsArray($matches[2]);
157+
$this->assertSame("File.doc", $matches[2][0]);
158+
$this->assertSame(419, $matches[2][1]);
158159
}
159160
}

0 commit comments

Comments
 (0)