Skip to content

Commit 0f4b293

Browse files
authored
Merge pull request #42 from Blaspsoft/fix/empty-string-check
fix: check() should not throw on empty strings (#29)
2 parents c4037af + 9739505 commit 0f4b293

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

src/BlaspService.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,19 @@ public function allLanguages(): self
246246
}
247247

248248
/**
249-
* @param string $string
249+
* @param string|null $string
250250
* @return $this
251-
* @throws Exception
252251
*/
253-
public function check(string $string): self
252+
public function check(?string $string): self
254253
{
255254
if (empty($string)) {
256-
257-
throw new Exception('No string to check');
255+
$this->sourceString = $string ?? '';
256+
$this->cleanString = $string ?? '';
257+
$this->hasProfanity = false;
258+
$this->profanitiesCount = 0;
259+
$this->uniqueProfanitiesFound = [];
260+
$this->uniqueProfanitiesMap = [];
261+
return $this;
258262
}
259263

260264
$this->sourceString = $string;

src/Facades/Blasp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Blaspsoft\Blasp\BlaspService;
77

88
/**
9-
* @method static \Blaspsoft\Blasp\BlaspService check(string $string)
9+
* @method static \Blaspsoft\Blasp\BlaspService check(?string $string)
1010
* @method static \Blaspsoft\Blasp\BlaspService configure(?array $profanities = null, ?array $falsePositives = null)
1111
* @method static \Blaspsoft\Blasp\BlaspService language(string $language)
1212
* @method static \Blaspsoft\Blasp\BlaspService english()
@@ -117,10 +117,10 @@ public static function maskWith(string $character): BlaspService
117117
/**
118118
* Check text for profanity (backwards compatible)
119119
*
120-
* @param string $string
120+
* @param string|null $string
121121
* @return \Blaspsoft\Blasp\BlaspService
122122
*/
123-
public static function check(string $string): BlaspService
123+
public static function check(?string $string): BlaspService
124124
{
125125
return static::getFacadeRoot()->check($string);
126126
}

tests/EmptyInputTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Blaspsoft\Blasp\Tests;
4+
5+
use Blaspsoft\Blasp\BlaspService;
6+
7+
class EmptyInputTest extends TestCase
8+
{
9+
protected $blaspService;
10+
11+
public function setUp(): void
12+
{
13+
parent::setUp();
14+
$this->blaspService = new BlaspService();
15+
}
16+
17+
public function test_empty_string_returns_no_profanity()
18+
{
19+
$result = $this->blaspService->check('');
20+
21+
$this->assertFalse($result->hasProfanity());
22+
$this->assertEquals(0, $result->getProfanitiesCount());
23+
$this->assertEmpty($result->getUniqueProfanitiesFound());
24+
}
25+
26+
public function test_empty_string_returns_empty_source_and_clean_strings()
27+
{
28+
$result = $this->blaspService->check('');
29+
30+
$this->assertEquals('', $result->getSourceString());
31+
$this->assertEquals('', $result->getCleanString());
32+
}
33+
34+
public function test_null_returns_no_profanity()
35+
{
36+
$result = $this->blaspService->check(null);
37+
38+
$this->assertFalse($result->hasProfanity());
39+
$this->assertEquals('', $result->getSourceString());
40+
$this->assertEquals('', $result->getCleanString());
41+
}
42+
43+
public function test_profanity_still_detected_after_empty_check()
44+
{
45+
$this->blaspService->check('');
46+
$result = $this->blaspService->check('shit');
47+
48+
$this->assertTrue($result->hasProfanity());
49+
}
50+
}

0 commit comments

Comments
 (0)