Skip to content

Commit

Permalink
preg_match requires non-empty-string for its pattern parameter
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Sep 6, 2023
1 parent 2f9d04a commit 99e3355
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"laminas/laminas-i18n": "^2.23",
"laminas/laminas-session": "^2.16",
"laminas/laminas-uri": "^2.10.0",
"phpunit/phpunit": "^10.1.3",
"phpunit/phpunit": "^10.3.3",
"psalm/plugin-phpunit": "^0.18.4",
"psr/http-client": "^1.0.2",
"psr/http-factory": "^1.0.2",
"vimeo/psalm": "^5.12"
"vimeo/psalm": "^5.15"
},
"suggest": {
"laminas/laminas-db": "Laminas\\Db component, required by the (No)RecordExists validator",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.13.0@a0a9c27630bcf8301ee78cb06741d2907d8c9fef">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="bin/update_hostname_validator.php">
<MissingClosureParamType>
<code>$domain</code>
Expand Down Expand Up @@ -2365,7 +2365,6 @@
<code>is_array($pattern)</code>
</DocblockTypeContradiction>
<MixedArgument>
<code><![CDATA[$pattern['pattern']]]></code>
<code>$value</code>
</MixedArgument>
<MoreSpecificImplementedParamType>
Expand Down
10 changes: 5 additions & 5 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Regex extends AbstractValidator
/**
* Regular expression pattern
*
* @var string
* @var non-empty-string
*/
protected $pattern;

/**
* Sets validator options
*
* @param string|array|Traversable $pattern
* @param non-empty-string|array|Traversable $pattern
* @throws Exception\InvalidArgumentException On missing 'pattern' parameter.
*/
public function __construct($pattern)
Expand All @@ -60,7 +60,7 @@ public function __construct($pattern)
throw new Exception\InvalidArgumentException('Invalid options provided to constructor');
}

if (! array_key_exists('pattern', $pattern)) {
if (! array_key_exists('pattern', $pattern) || ! is_string($pattern['pattern']) || $pattern['pattern'] === '') {
throw new Exception\InvalidArgumentException("Missing option 'pattern'");
}

Expand All @@ -72,7 +72,7 @@ public function __construct($pattern)
/**
* Returns the pattern option
*
* @return string
* @return non-empty-string|null
*/
public function getPattern()
{
Expand All @@ -82,7 +82,7 @@ public function getPattern()
/**
* Sets the pattern option
*
* @param string $pattern
* @param non-empty-string $pattern
* @throws Exception\InvalidArgumentException If there is a fatal error in pattern matching.
* @return $this Provides a fluent interface
*/
Expand Down
27 changes: 17 additions & 10 deletions test/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,32 @@ public function testEqualsMessageVariables(): void
}

/**
* @psalm-return array<string, array{0: mixed}>
* @psalm-return array<string, array{0: mixed, 1: non-empty-string}>
*/
public static function invalidConstructorArgumentsProvider(): array
{
return [
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.0],
'object' => [(object) []],
'true' => [true, 'Invalid options provided to constructor'],
'false' => [false, 'Invalid options provided to constructor'],
'zero' => [0, 'Invalid options provided to constructor'],
'int' => [1, 'Invalid options provided to constructor'],
'zero-float' => [0.0, 'Invalid options provided to constructor'],
'float' => [1.0, 'Invalid options provided to constructor'],
'object' => [(object) [], 'Invalid options provided to constructor'],
'empty-string' => ['', 'Internal error parsing the pattern'],
'missing-pattern-key' => [[], "Missing option 'pattern'"],
'pattern-key-not-string' => [['pattern' => false], "Missing option 'pattern'"],
'pattern-key-empty-string' => [['pattern' => ''], "Missing option 'pattern'"],
];
}

#[DataProvider('invalidConstructorArgumentsProvider')]
public function testConstructorRaisesExceptionWhenProvidedInvalidArguments(mixed $options): void
{
public function testConstructorRaisesExceptionWhenProvidedInvalidArguments(
mixed $options,
string $expectedMessage,
): void {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);

new Regex($options);
}
Expand Down

0 comments on commit 99e3355

Please sign in to comment.