Skip to content

Commit e2fc802

Browse files
Florian Krämercursoragent
andcommitted
Add constructor parameter validation to both rules
PropertyMustMatchRule now validates that propertyPatterns is non-empty. ForbiddenAccessorsRule now validates that classPatterns is non-empty and that visibility values are valid (public, protected, private). Invalid configurations now throw InvalidArgumentException immediately instead of silently producing no results. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c551f84 commit e2fc802

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/Architecture/ForbiddenAccessorsRule.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class ForbiddenAccessorsRule implements Rule
4040
private const GETTER_PATTERN = '/^get[A-Z]/';
4141
private const SETTER_PATTERN = '/^set[A-Z]/';
4242

43+
private const VALID_VISIBILITIES = ['public', 'protected', 'private'];
44+
4345
/**
4446
* @param array<string> $classPatterns Regex patterns to match against class FQCNs.
4547
* @param bool $forbidGetters Whether to forbid getXxx() methods.
@@ -52,6 +54,20 @@ public function __construct(
5254
protected bool $forbidSetters = true,
5355
protected array $visibility = ['public']
5456
) {
57+
if ($classPatterns === []) {
58+
throw new \InvalidArgumentException('At least one class pattern must be provided.');
59+
}
60+
61+
$invalidVisibilities = array_diff($this->visibility, self::VALID_VISIBILITIES);
62+
if ($invalidVisibilities !== []) {
63+
throw new \InvalidArgumentException(
64+
sprintf(
65+
'Invalid visibility value(s): %s. Must be one of: %s.',
66+
implode(', ', $invalidVisibilities),
67+
implode(', ', self::VALID_VISIBILITIES)
68+
)
69+
);
70+
}
5571
}
5672

5773
public function getNodeType(): string

src/Architecture/PropertyMustMatchRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class PropertyMustMatchRule implements Rule
6464
public function __construct(
6565
protected array $propertyPatterns
6666
) {
67+
if ($propertyPatterns === []) {
68+
throw new \InvalidArgumentException('At least one property pattern must be provided.');
69+
}
6770
}
6871

6972
public function getNodeType(): string

0 commit comments

Comments
 (0)