Skip to content

Commit

Permalink
TypeString::toArray[Unique](): throw exception on incorrect input
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 10, 2024
1 parent 3b72a79 commit 5c9b44c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 11 additions & 1 deletion PHPCSUtils/Utils/TypeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace PHPCSUtils\Utils;

use PHP_CodeSniffer\Exceptions\RuntimeException;

/**
* Utility functions for use when examining type strings.
*
Expand Down Expand Up @@ -303,10 +305,16 @@ public static function isDNF($typeString)
* Defaults to true.
*
* @return array<string> List containing all seen types in the order they were encountered.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If passed $typeString is not a string.
*/
public static function toArray($typeString, $normalize = true)
{
if (\is_string($typeString) === false || \trim($typeString) === '') {
if (\is_string($typeString) === false) {
throw new RuntimeException('The $typeString parameter must be passed a string');
}

if (\trim($typeString) === '') {
return [];
}

Expand Down Expand Up @@ -341,6 +349,8 @@ public static function toArray($typeString, $normalize = true)
* Defaults to true.
*
* @return array<string, string> Associative array with the unique types as both the key as well as the value.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If passed $typeString is not a string.
*/
public static function toArrayUnique($typeString, $normalize = true)
{
Expand Down
16 changes: 11 additions & 5 deletions Tests/Utils/TypeString/ToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use PHPCSUtils\Tests\TypeProviderHelper;
use PHPCSUtils\Utils\TypeString;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* Tests for the \PHPCSUtils\Utils\TypeString::toArray() and
Expand All @@ -35,9 +35,12 @@ final class ToArrayTest extends TestCase
*
* @return void
*/
public function testToArrayReturnsEmptyArrayOnNonStringInput($input)
public function testToArrayThrowsExceptionOnNonStringInput($input)
{
$this->assertSame([], TypeString::toArray($input));
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
$this->expectExceptionMessage('The $typeString parameter must be passed a string');

TypeString::toArray($input);
}

/**
Expand All @@ -49,9 +52,12 @@ public function testToArrayReturnsEmptyArrayOnNonStringInput($input)
*
* @return void
*/
public function testToArrayUniqueReturnsEmptyArrayOnNonStringInput($input)
public function testToArrayUniqueThrowsExceptionOnNonStringInput($input)
{
$this->assertSame([], TypeString::toArrayUnique($input));
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
$this->expectExceptionMessage('The $typeString parameter must be passed a string');

TypeString::toArrayUnique($input);
}

/**
Expand Down

0 comments on commit 5c9b44c

Please sign in to comment.