Skip to content

Commit

Permalink
fixup! Add sniff to detect @var usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 18, 2024
1 parent 623544c commit ac14f8f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions moodle/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ protected static function suggestType(string $varType): string {

$type1 = self::suggestType($type1);
$type2 = self::suggestType($type2);
if ($type2 !== '') {
$type2 = ' => ' . $type2;
}

return "array($type1$type2)";
if ($type1 && !$type2) {
return "array<array-key, {$type1}>";
}
return "array<{$type1}, {$type2}>";
} else {
return 'array';
}
Expand Down
2 changes: 2 additions & 0 deletions moodle/Tests/Sniffs/Commenting/VariableCommentSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public static function fixtureProvider(): array {
21 => 'Expected "array" but found "ARRAY()" for @var tag in member variable comment',
27 => 'Expected "bool" but found "boolean" for @var tag in member variable comment',
30 => 'Expected "bool[]" but found "boolean[]" for @var tag in member variable comment',
33 => 'Expected "array<int, bool>" but found "array(int => bool)" for @var tag in member variable comment',
36 => 'Expected "array" but found "array(int > bool)" for @var tag in member variable comment',
39 => 'Expected "array<array-key, int>" but found "array(int)" for @var tag in member variable comment',
],
'warnings' => [],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ class type_check {
/** @var array(int > bool) List of ints */
protected array $arrayofints;

/** @var array(int) List of ints */
protected array $notalistarray;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class type_check {
/** @var bool[] This should be a bool[] */
protected array $arrayofbool;

/** @var array(int => bool) Mapping of ints to bools */
/** @var array<int, bool> Mapping of ints to bools */
protected array $arrayofthings;

/** @var array List of ints */
protected array $arrayofints;

/** @var array<array-key, int> List of ints */
protected array $notalistarray;
}

0 comments on commit ac14f8f

Please sign in to comment.