Skip to content

Commit

Permalink
Merge pull request #6121 from weirdan/fix-6119
Browse files Browse the repository at this point in the history
Allow 0 for all bitmask types
  • Loading branch information
weirdan authored Jul 16, 2021
2 parents f015c30 + 4a51600 commit 90687d7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ public static function getComputedIntsFromMask(array $potential_ints) : array
$potential_values = array_merge($new_values, $potential_values);
}

array_unshift($potential_values, 0);
$potential_values = array_unique($potential_values);

return array_map(
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TIntMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Represents the type that is the result of a bitmask combination of its parameters.
* `int-mask<1, 2, 4>` corresponds to `1|2|3|4|5|6|7`
* `int-mask<1, 2, 4>` corresponds to `0|1|2|3|4|5|6|7`
*/
class TIntMask extends TInt
{
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TIntMaskOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Represents the type that is the result of a bitmask combination of its parameters.
* This is the same concept as TIntMask but TIntMaskOf is used with with a reference to constants in code
* `int-mask<MyClass::CLASS_CONSTANT_*>` will corresponds to `1|2|3|4|5|6|7` if there are three constant 1, 2 and 4
* `int-mask<MyClass::CLASS_CONSTANT_*>` will corresponds to `0|1|2|3|4|5|6|7` if there are three constant 1, 2 and 4
*/
class TIntMaskOf extends TInt
{
Expand Down
22 changes: 22 additions & 0 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,14 @@ function takesFlags(int $flags) : void {
takesFlags(FileFlag::MODIFIED | FileFlag::NEW);'
],
'intMaskWithZero' => [
'<?php
/** @param int-mask<1,2> $_flags */
function takesFlags(int $_flags): void {}
takesFlags(0);
'
],
'intMaskOfWithClassWildcard' => [
'<?php
class FileFlag {
Expand All @@ -1202,6 +1210,20 @@ function takesFlags(int $flags) : void {
takesFlags(FileFlag::MODIFIED | FileFlag::NEW);'
],
'intMaskOfWithZero' => [
'<?php
class FileFlag {
public const OPEN = 1;
public const MODIFIED = 2;
public const NEW = 4;
}
/** @param int-mask-of<FileFlag::*> $_flags */
function takesFlags(int $_flags): void {}
takesFlags(0);
'
],
'emptyStringFirst' => [
'<?php
/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TypeParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,15 +917,15 @@ public function testIntMaskWithInts(): void

$docblock_type = Type::parseString('int-mask<1, 2, 4>');

$this->assertSame('1|2|3|4|5|6|7', $docblock_type->getId());
$this->assertSame('0|1|2|3|4|5|6|7', $docblock_type->getId());

$docblock_type = Type::parseString('int-mask<1, 4>');

$this->assertSame('1|4|5', $docblock_type->getId());
$this->assertSame('0|1|4|5', $docblock_type->getId());

$docblock_type = Type::parseString('int-mask<PREG_PATTERN_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>');

$this->assertSame('1|256|257|512|513|768|769', $docblock_type->getId());
$this->assertSame('0|1|256|257|512|513|768|769', $docblock_type->getId());
}

public function testIntMaskWithClassConstant(): void
Expand Down

0 comments on commit 90687d7

Please sign in to comment.