Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix int-mask-of to work well with operations on the int mask #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
staabm wants to merge 2 commits into phpstan:1.10.x
base: 1.10.x
Choose a base branch
Loading
from staabm:bug9384
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
View file Open in desktop
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

@ondrejmirtes ondrejmirtes Jun 8, 2023

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
edited
Loading

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
View file Open in desktop
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
View file Open in desktop
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);
}
}
View file Open in desktop
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
View file Open in desktop
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;
}
}

AltStyle によって変換されたページ (->オリジナル) /