Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,15 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
return new StringType();
}

if (TypeCombinator::union($leftType->toNumber(), $rightType->toNumber()) instanceof ErrorType) {
$unionType = TypeCombinator::union($leftType->toNumber(), $rightType->toNumber());
if ($unionType instanceof ErrorType) {
return new ErrorType();
}

if ($unionType->isInteger()->yes()) {
return $unionType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Other bitwise operations need to be fixed as well
  2. Is this really correct? What if I do int<100, 150> | int<500, 1000>?
Copy link
Contributor

@jtojnar jtojnar Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It would be a nice to have or at least.
  2. For a simpler example, 1 ∈ int<1,1> and 2 ∈ int<2,2> but 1|2 = 3 ∉ (int<1,1> | int<2,2>)
}

return new IntegerType();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/image-size.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/base64_decode.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9404.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9384.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9384.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace Bug9384b;

use function PHPStan\Testing\assertType;

class BinaryOr
{
private const TYPE_NONE = 0;
private const TYPE_TRACK_DEPRECATIONS = 1;
private const TYPE_TRIGGER_ERROR = 2;

/** @var int-mask-of<self::TYPE_*>|null */
private static $type;

public static function enableTrackingDeprecations(): void
{
assertType('int<0, 3>|null', self::$type);
assertType('1', self::TYPE_TRACK_DEPRECATIONS);
assertType('int<0, 3>', self::$type | self::TYPE_TRACK_DEPRECATIONS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,15 @@ public function testWritingReadonlyProperty(): void
]);
}

public function testBug9384(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-9384.php'], [
[
'Static property Bug9384\Deprecation::$type (int<0, 3>|null) does not accept 10|int<0, 3>.',
18,
],
]);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-9384.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug9384;

class Deprecation
{
private const TYPE_NONE = 0;
private const TYPE_TRACK_DEPRECATIONS = 1;
private const TYPE_TRIGGER_ERROR = 2;

/** @var int-mask-of<self::TYPE_*>|null */
private static $type;

public static function enableTrackingDeprecations(): void
{
self::$type |= self::TYPE_TRACK_DEPRECATIONS;

self::$type = self::$type | 10; // invalid value
}

public static function enableWithTriggerError(): void
{
self::$type |= self::TYPE_TRIGGER_ERROR;
}

public static function disable(): void
{
self::$type = self::TYPE_NONE;
}
}