Skip to content

Commit c99e2b9

Browse files
authored
ForbidCustomFunctionsRule: better config validation (#83)
1 parent 411a5b3 commit c99e2b9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Rule/ForbidCustomFunctionsRule.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\TypeUtils;
2121
use function count;
2222
use function explode;
23+
use function gettype;
2324
use function is_string;
2425
use function sprintf;
2526

@@ -40,15 +41,19 @@ class ForbidCustomFunctionsRule implements Rule
4041
private ReflectionProvider $reflectionProvider;
4142

4243
/**
43-
* @param array<string, mixed> $forbiddenFunctions
44+
* @param array<mixed, mixed> $forbiddenFunctions
4445
*/
4546
public function __construct(array $forbiddenFunctions, ReflectionProvider $reflectionProvider)
4647
{
4748
$this->reflectionProvider = $reflectionProvider;
4849

4950
foreach ($forbiddenFunctions as $forbiddenFunction => $description) {
51+
if (!is_string($forbiddenFunction)) {
52+
throw new LogicException("Unexpected forbidden function name, string expected, got $forbiddenFunction. Usage: ['var_dump' => 'Remove debug code!'].");
53+
}
54+
5055
if (!is_string($description)) {
51-
throw new LogicException('Unexpected forbidden function description, string expected');
56+
throw new LogicException('Unexpected forbidden function description, string expected, got ' . gettype($description) . '. Usage: [\'var_dump\' => \'Remove debug code!\'].');
5257
}
5358

5459
$parts = explode('::', $forbiddenFunction);

tests/Rule/ForbidCustomFunctionsRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@ public function testClass(): void
3535
$this->analyseFile(__DIR__ . '/data/ForbidCustomFunctionsRule/code.php');
3636
}
3737

38+
public function testInvalidConfig1(): void
39+
{
40+
self::expectExceptionMessage("Unexpected forbidden function name, string expected, got 0. Usage: ['var_dump' => 'Remove debug code!'].");
41+
new ForbidCustomFunctionsRule(['var_dump'], $this->createMock(ReflectionProvider::class));
42+
}
43+
44+
public function testInvalidConfig2(): void
45+
{
46+
self::expectExceptionMessage("Unexpected forbidden function description, string expected, got array. Usage: ['var_dump' => 'Remove debug code!'].");
47+
new ForbidCustomFunctionsRule(['var_dump' => []], $this->createMock(ReflectionProvider::class));
48+
}
49+
50+
public function testInvalidConfig3(): void
51+
{
52+
self::expectExceptionMessage('Unexpected format of forbidden function Class::method::12, expected Namespace\Class::methodName');
53+
new ForbidCustomFunctionsRule(['Class::method::12' => 'Description'], $this->createMock(ReflectionProvider::class));
54+
}
55+
3856
}

0 commit comments

Comments
 (0)