Skip to content

Commit

Permalink
Narrow types
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 31, 2023
1 parent 12191c4 commit 53bca56
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/Complexity/Complexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,37 @@
*/
final class Complexity
{
private string $name;
/**
* @psalm-var non-empty-string
*/
private readonly string $name;

/**
* @psalm-var positive-int
*/
private int $cyclomaticComplexity;

/**
* @psalm-param non-empty-string $name
* @psalm-param positive-int $cyclomaticComplexity
*/
public function __construct(string $name, int $cyclomaticComplexity)
{
$this->name = $name;
$this->cyclomaticComplexity = $cyclomaticComplexity;
}

/**
* @psalm-return non-empty-string
*/
public function name(): string
{
return $this->name;
}

/**
* @psalm-return positive-int
*/
public function cyclomaticComplexity(): int
{
return $this->cyclomaticComplexity;
Expand Down
8 changes: 7 additions & 1 deletion src/Complexity/ComplexityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ComplexityCollection implements Countable, IteratorAggregate
/**
* @psalm-var list<Complexity>
*/
private array $items;
private readonly array $items;

public static function fromList(Complexity ...$items): self
{
Expand Down Expand Up @@ -49,6 +49,9 @@ public function getIterator(): ComplexityCollectionIterator
return new ComplexityCollectionIterator($this);
}

/**
* @psalm-return non-negative-int
*/
public function count(): int
{
return count($this->items);
Expand All @@ -59,6 +62,9 @@ public function isEmpty(): bool
return empty($this->items);
}

/**
* @psalm-return non-negative-int
*/
public function cyclomaticComplexity(): int
{
$cyclomaticComplexity = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Complexity/ComplexityCollectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ComplexityCollectionIterator implements Iterator
/**
* @psalm-var list<Complexity>
*/
private array $items;
private readonly array $items;
private int $position = 0;

public function __construct(ComplexityCollection $items)
Expand Down
14 changes: 13 additions & 1 deletion src/Visitor/ComplexityCalculatingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function result(): ComplexityCollection

/**
* @param Stmt[] $statements
*
* @psalm-return positive-int
*/
private function cyclomaticComplexity(array $statements): int
{
Expand All @@ -89,6 +91,9 @@ private function cyclomaticComplexity(array $statements): int
return $cyclomaticComplexityCalculatingVisitor->cyclomaticComplexity();
}

/**
* @psalm-return non-empty-string
*/
private function classMethodName(ClassMethod $node): string
{
$parent = $node->getAttribute('parent');
Expand All @@ -100,11 +105,18 @@ private function classMethodName(ClassMethod $node): string
return $parent->namespacedName->toString() . '::' . $node->name->toString();
}

/**
* @psalm-return non-empty-string
*/
private function functionName(Function_ $node): string
{
assert(isset($node->namespacedName));
assert($node->namespacedName instanceof Name);

return $node->namespacedName->toString();
$functionName = $node->namespacedName->toString();

assert($functionName !== '');

return $functionName;
}
}
6 changes: 6 additions & 0 deletions src/Visitor/CyclomaticComplexityCalculatingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

final class CyclomaticComplexityCalculatingVisitor extends NodeVisitorAbstract
{
/**
* @psalm-var positive-int
*/
private int $cyclomaticComplexity = 1;

public function enterNode(Node $node): void
Expand All @@ -47,6 +50,9 @@ public function enterNode(Node $node): void
}
}

/**
* @psalm-return positive-int
*/
public function cyclomaticComplexity(): int
{
return $this->cyclomaticComplexity;
Expand Down

0 comments on commit 53bca56

Please sign in to comment.