Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 18 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,14 @@ public function getBitwiseAndTypeFromTypes(Type $leftType, Type $rightType): Typ
$rightType = $this->optimizeScalarType($rightType);
}

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
$leftIsString = $leftType->isString();
$rightIsString = $rightType->isString();
if ($leftIsString->yes() && $rightIsString->yes()) {
return new StringType();
}
if ($leftIsString->maybe() && $rightIsString->maybe()) {
return new ErrorType();
}

$leftNumberType = $leftType->toNumber();
$rightNumberType = $rightType->toNumber();
Expand Down Expand Up @@ -1082,9 +1087,14 @@ public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
$rightType = $this->optimizeScalarType($rightType);
}

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
$leftIsString = $leftType->isString();
$rightIsString = $rightType->isString();
if ($leftIsString->yes() && $rightIsString->yes()) {
return new StringType();
}
if ($leftIsString->maybe() && $rightIsString->maybe()) {
return new ErrorType();
}

if (TypeCombinator::union($leftType->toNumber(), $rightType->toNumber()) instanceof ErrorType) {
return new ErrorType();
Expand Down Expand Up @@ -1146,9 +1156,14 @@ public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Typ
$rightType = $this->optimizeScalarType($rightType);
}

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
$leftIsString = $leftType->isString();
$rightIsString = $rightType->isString();
if ($leftIsString->yes() && $rightIsString->yes()) {
return new StringType();
}
if ($leftIsString->maybe() && $rightIsString->maybe()) {
return new ErrorType();
}

if (TypeCombinator::union($leftType->toNumber(), $rightType->toNumber()) instanceof ErrorType) {
return new ErrorType();
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function doBitwiseNot($a, int $b): void
public function doBitwiseAnd($a, $b, int $c, int $d): void
{
assertType('int', $a & $b);
assertNativeType('int', $a & $b);
assertNativeType('*ERROR*', $a & $b);
Copy link
Contributor Author

@VincentLanglet VincentLanglet Dec 5, 2025

Choose a reason for hiding this comment

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

This is equivalent to mixed & mixed which is now reported as an error. (we can't be sure it's an int, it could be string & string for instance or worst, int & string)

Copy link
Contributor

Choose a reason for hiding this comment

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

if we can't know what it is, why is it an ERROR then? shouldn't it be mixed instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current mixed behavior is weird. A good example of the issue is

int ^ mixed = ? // currently int ; so no error is reported InvalidBinaryOperationRule
string ^ mixed = ? // currently Error ; an error is reported by InvalidBinaryOperationRule
mixed ^ mixed = ? // currently int ; so no error is reported by InvalidBinaryOperationRule

I'm not sure what should be done. Either error for all or something like

int ^ mixed = int
string ^ mixed = string
mixed ^ mixed = (int|string)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried another solution, tell me your opinion about it @staabm :)

assertType('1', 1 & 1);
assertNativeType('1', 1 & 1);
assertType('int', $c & $d);
Expand All @@ -125,7 +125,7 @@ public function doBitwiseAnd($a, $b, int $c, int $d): void
public function doBitwiseOr($a, $b, int $c, int $d): void
{
assertType('int', $a | $b);
assertNativeType('int', $a | $b);
assertNativeType('*ERROR*', $a | $b);
assertType('1', 1 | 1);
assertNativeType('1', 1 | 1);
assertType('int', $c | $d);
Expand All @@ -140,7 +140,7 @@ public function doBitwiseOr($a, $b, int $c, int $d): void
public function doBitwiseXor($a, $b, int $c, int $d): void
{
assertType('int', $a ^ $b);
assertNativeType('int', $a ^ $b);
assertNativeType('*ERROR*', $a ^ $b);
assertType('0', 1 ^ 1);
assertNativeType('0', 1 ^ 1);
assertType('int', $c ^ $d);
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bitwise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bitwise;

use function PHPStan\Testing\assertType;

/**
* @param string|int $stringOrInt
* @param mixed $mixed
*/
function test(int $int, string $string, $stringOrInt, $mixed) : void
{
assertType('int', $int & $int);
assertType('*ERROR*', $int & $string);
assertType('*ERROR*', $int & $stringOrInt);
assertType('int', $int & $mixed);
assertType('string', $string & $string);
assertType('*ERROR*', $string & $stringOrInt);
assertType('*ERROR*', $string & $mixed);
assertType('*ERROR*', $stringOrInt & $stringOrInt);
assertType('*ERROR*', $stringOrInt & $mixed);
assertType('*ERROR*', $mixed & $mixed);

assertType('int', $int | $int);
assertType('*ERROR*', $int | $string);
assertType('*ERROR*', $int | $stringOrInt);
assertType('int', $int | $mixed);
assertType('string', $string | $string);
assertType('*ERROR*', $string | $stringOrInt);
assertType('*ERROR*', $string | $mixed);
assertType('*ERROR*', $stringOrInt | $stringOrInt);
assertType('*ERROR*', $stringOrInt | $mixed);
assertType('*ERROR*', $mixed | $mixed);

assertType('int', $int ^ $int);
assertType('*ERROR*', $int ^ $string);
assertType('*ERROR*', $int ^ $stringOrInt);
assertType('int', $int ^ $mixed);
assertType('string', $string ^ $string);
assertType('*ERROR*', $string ^ $stringOrInt);
assertType('*ERROR*', $string ^ $mixed);
assertType('*ERROR*', $stringOrInt ^ $stringOrInt);
assertType('*ERROR*', $stringOrInt ^ $mixed);
assertType('*ERROR*', $mixed ^ $mixed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,11 @@ public function testBug13784(): void
$this->analyse([__DIR__ . '/data/bug-13784.php'], []);
}

public function testBug8094(): void
{
$this->analyse([__DIR__ . '/data/bug-8094.php'], []);
}

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

namespace Bug8094;

function mask_encode($data, $mask)
{
$mask = substr($mask, 0, strlen($data));
$data ^= $mask;
return(base64_encode($data));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was reporting an error on level 5 on php 7.4 https://phpstan.org/r/12e63cdd-d516-4db2-a314-51da4f5c3101

}
Loading