Skip to content
Draft
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
39 changes: 33 additions & 6 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,17 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
return $this->getNeverType($leftType, $rightType);
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
if ($leftType instanceof IntegerRangeType) {
$leftTypes = $leftType->getFiniteTypes();
} else {
$leftTypes = $leftType->getConstantScalarTypes();
}
if ($rightType instanceof IntegerRangeType) {
$rightTypes = $rightType->getFiniteTypes();
} else {
$rightTypes = $rightType->getConstantScalarTypes();
}

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
Expand Down Expand Up @@ -680,8 +689,17 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
return $this->getNeverType($leftType, $rightType);
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
if ($leftType instanceof IntegerRangeType) {
$leftTypes = $leftType->getFiniteTypes();
} else {
$leftTypes = $leftType->getConstantScalarTypes();
}
if ($rightType instanceof IntegerRangeType) {
$rightTypes = $rightType->getFiniteTypes();
} else {
$rightTypes = $rightType->getConstantScalarTypes();
}

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
Expand Down Expand Up @@ -739,8 +757,17 @@ public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCall
return $this->getNeverType($leftType, $rightType);
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
if ($leftType instanceof IntegerRangeType) {
$leftTypes = $leftType->getFiniteTypes();
} else {
$leftTypes = $leftType->getConstantScalarTypes();
}
if ($rightType instanceof IntegerRangeType) {
$rightTypes = $rightType->getFiniteTypes();
} else {
$rightTypes = $rightType->getConstantScalarTypes();
}

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,18 @@ public function testBug13035(): void
$this->analyse([__DIR__ . '/data/bug-13035.php'], []);
}

public function testBug7912(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7912.php'], [
[
'Property Bug7912\A::$has (int<0, 1>) does not accept 999.',
35,
],
]);
}

public function testBug13654(): void
{
$this->checkExplicitMixed = true;
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7912.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Bug7912;

class A
{
/** @var int<0,1> */
public int $has = 0;

/** @var int<0,1> */
public int $not = 0;
}

class B
{
/** @var int<0,1> */
public int $has = 1;
}

$a = new A();
$b = new B();

// The following versions throw an error, even though | between 0,1 will always be 0,1
$a->has |= $b->has;
$a->has = $a->has | $b->has;

// The following versions don't:
$a->has = 0 | 1;
$a->has |= 1;

$int = 1;
$a->has |= $int;

// This properly errors:
$a->has |= 999;

// And these all work:
/** @var int<0,1> */
$c = 0;
/** @var int<0,1> */
$e = 1;
$c |= $e;
$c |= $a->has;
Loading