Skip to content

Commit 3e083d0

Browse files
committed
Check bitmasks where they're not allowed
1 parent bfa28dd commit 3e083d0

13 files changed

Lines changed: 100 additions & 13 deletions

src/Reflection/AllowedConstantsResult.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ final class AllowedConstantsResult
1212
public function __construct(
1313
private array $disallowedConstants,
1414
private array $violatedExclusiveGroups,
15+
private bool $bitmaskNotAllowed = false,
1516
)
1617
{
1718
}
1819

1920
public function isOk(): bool
2021
{
21-
return $this->disallowedConstants === [] && $this->violatedExclusiveGroups === [];
22+
return $this->disallowedConstants === [] && $this->violatedExclusiveGroups === [] && !$this->bitmaskNotAllowed;
23+
}
24+
25+
public function isBitmaskNotAllowed(): bool
26+
{
27+
return $this->bitmaskNotAllowed;
2228
}
2329

2430
/**

src/Reflection/ParameterAllowedConstants.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ private function resolveConstantName(ConstantReflection $constant): string
4848
*/
4949
public function check(array $constants): AllowedConstantsResult
5050
{
51+
$bitmaskNotAllowed = !$this->isBitmask() && count($constants) > 1;
52+
5153
$disallowed = [];
5254
$names = [];
5355

@@ -63,24 +65,26 @@ public function check(array $constants): AllowedConstantsResult
6365
}
6466

6567
$violated = [];
66-
foreach ($this->exclusiveGroups as $group) {
67-
$matched = [];
68-
foreach ($names as $name) {
69-
if (!in_array($name, $group, true)) {
70-
continue;
68+
if ($this->isBitmask()) {
69+
foreach ($this->exclusiveGroups as $group) {
70+
$matched = [];
71+
foreach ($names as $name) {
72+
if (!in_array($name, $group, true)) {
73+
continue;
74+
}
75+
76+
$matched[] = $name;
7177
}
7278

73-
$matched[] = $name;
74-
}
79+
if (count($matched) < 2) {
80+
continue;
81+
}
7582

76-
if (count($matched) < 2) {
77-
continue;
83+
$violated[] = $matched;
7884
}
79-
80-
$violated[] = $matched;
8185
}
8286

83-
return new AllowedConstantsResult($disallowed, $violated);
87+
return new AllowedConstantsResult($disallowed, $violated, $bitmaskNotAllowed);
8488
}
8589

8690
}

src/Rules/AttributesCheck.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public function check(
159159
'Attribute class ' . $attributeClassName . ' constructor invoked with %s, but it\'s not allowed because of @no-named-arguments.',
160160
'Constant %s is not allowed for %s of attribute class ' . $attributeClassName . ' constructor.',
161161
'Constants %s cannot be combined for %s of attribute class ' . $attributeClassName . ' constructor.',
162+
'Combining constants with | is not allowed for %s of attribute class ' . $attributeClassName . ' constructor.',
162163
);
163164

164165
foreach ($parameterErrors as $error) {

src/Rules/Classes/InstantiationRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
271271
'Class ' . $classDisplayName . ' constructor invoked with %s, but it\'s not allowed because of @no-named-arguments.',
272272
'Constant %s is not allowed for %s of class ' . $classDisplayName . ' constructor.',
273273
'Constants %s cannot be combined for %s of class ' . $classDisplayName . ' constructor.',
274+
'Combining constants with | is not allowed for %s of class ' . $classDisplayName . ' constructor.',
274275
));
275276
}
276277

src/Rules/FunctionCallParametersCheck.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function check(
9191
string $namedArgumentMessage,
9292
string $invalidConstantMessage,
9393
string $exclusiveConstantsMessage,
94+
string $bitmaskNotAllowedMessage,
9495
): array
9596
{
9697
if ($funcCall instanceof Node\Expr\MethodCall || $funcCall instanceof Node\Expr\StaticCall || $funcCall instanceof Node\Expr\FuncCall) {
@@ -440,6 +441,15 @@ public function check(
440441
->line($argumentLine)
441442
->build();
442443
}
444+
if ($result->isBitmaskNotAllowed()) {
445+
$errors[] = RuleErrorBuilder::message(sprintf(
446+
$bitmaskNotAllowedMessage,
447+
lcfirst($this->describeParameter($parameter, $argumentName ?? $i + 1)),
448+
))
449+
->identifier('argument.bitmaskNotAllowed')
450+
->line($argumentLine)
451+
->build();
452+
}
443453
}
444454
}
445455
}

src/Rules/Functions/CallCallablesRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function processNode(
141141
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
142142
'Constant %s is not allowed for %s of ' . $callableDescription . '.',
143143
'Constants %s cannot be combined for %s of ' . $callableDescription . '.',
144+
'Combining constants with | is not allowed for %s of ' . $callableDescription . '.',
144145
),
145146
);
146147
}

src/Rules/Functions/CallToFunctionParametersRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function processNode(Node $node, Scope $scope): array
7070
'Function ' . $functionName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
7171
'Constant %s is not allowed for %s of function ' . $functionName . '.',
7272
'Constants %s cannot be combined for %s of function ' . $functionName . '.',
73+
'Combining constants with | is not allowed for %s of function ' . $functionName . '.',
7374
);
7475
}
7576

src/Rules/Functions/CallUserFuncRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function processNode(Node $node, Scope $scope): array
8686
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
8787
'Constant %s is not allowed for %s of ' . $callableDescription . '.',
8888
'Constants %s cannot be combined for %s of ' . $callableDescription . '.',
89+
'Combining constants with | is not allowed for %s of ' . $callableDescription . '.',
8990
);
9091
}
9192

src/Rules/Methods/CallMethodsRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ private function processSingleMethodCall(Scope $scope, MethodCall $node, string
101101
'Method ' . $messagesMethodName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
102102
'Constant %s is not allowed for %s of method ' . $messagesMethodName . '.',
103103
'Constants %s cannot be combined for %s of method ' . $messagesMethodName . '.',
104+
'Combining constants with | is not allowed for %s of method ' . $messagesMethodName . '.',
104105
));
105106
}
106107

src/Rules/Methods/CallStaticMethodsRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ private function processSingleMethodCall(Scope $scope, StaticCall $node, string
110110
$displayMethodName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
111111
'Constant %s is not allowed for %s of ' . $lowercasedMethodName . '.',
112112
'Constants %s cannot be combined for %s of ' . $lowercasedMethodName . '.',
113+
'Combining constants with | is not allowed for %s of ' . $lowercasedMethodName . '.',
113114
));
114115

115116
return $errors;

0 commit comments

Comments
 (0)