Skip to content

Commit

Permalink
A bit safer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 2, 2024
1 parent 0e69f0d commit a8c38ec
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Rule/ForbidNotNormalizedTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use function get_object_vars;
use function implode;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function spl_object_id;
Expand Down Expand Up @@ -572,9 +573,10 @@ private function processMultiTypePhpDocNode(
foreach ($multiTypeNode->types as $type) {
if ($type instanceof UnionTypeNode) {
$dnf = $this->typeNodeResolver->resolve($multiTypeNode, $nameSpace)->describe(VerbosityLevel::typeOnly());
$line = $this->extractLineFromPhpDocTypeNode($type);

$errors[] = RuleErrorBuilder::message("Found non-normalized type {$multiTypeNode}{$forWhat}: this is not disjunctive normal form, use {$dnf}")
->line($type->getAttribute('line'))
->line($line)
->identifier('shipmonk.nonNormalizedType')
->build();
}
Expand All @@ -591,17 +593,20 @@ private function processMultiTypePhpDocNode(
$typeA = $this->typeNodeResolver->resolve($typeNodeA, $nameSpace);
$typeB = $this->typeNodeResolver->resolve($typeNodeB, $nameSpace);

$typeALine = $this->extractLineFromPhpDocTypeNode($typeNodeA);
$typeBLine = $this->extractLineFromPhpDocTypeNode($typeNodeB);

if ($typeA->isSuperTypeOf($typeB)->yes()) {
$errors[] = RuleErrorBuilder::message("Found non-normalized type {$multiTypeNode}{$forWhat}: {$typeNodeB} is a subtype of {$typeNodeA}.")
->line($typeNodeB->getAttribute('line'))
->line($typeBLine)
->identifier('shipmonk.nonNormalizedType')
->build();
continue;
}

if ($typeB->isSuperTypeOf($typeA)->yes()) {
$errors[] = RuleErrorBuilder::message("Found non-normalized type {$multiTypeNode}{$forWhat}: {$typeNodeA} is a subtype of {$typeNodeB}.")
->line($typeNodeA->getAttribute('line'))
->line($typeALine)
->identifier('shipmonk.nonNormalizedType')
->build();
}
Expand All @@ -611,6 +616,17 @@ private function processMultiTypePhpDocNode(
return $errors;
}

private function extractLineFromPhpDocTypeNode(TypeNode $node): int
{
$line = $node->getAttribute('line');

if (!is_int($line)) {
throw new LogicException('Missing custom line attribute in node: ' . $node);
}

return $line;
}

private function getPropertyNameFromNativeNode(Property $node): string
{
$propertyNames = [];
Expand Down

0 comments on commit a8c38ec

Please sign in to comment.