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

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
if ($leftType instanceof MixedType && $rightType instanceof MixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

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

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

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
if ($leftType instanceof MixedType && $rightType instanceof MixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

$leftIsString = $leftType->isString();
$rightIsString = $rightType->isString();
if (
($leftIsString->yes() || $leftType instanceof MixedType)
&& ($rightIsString->yes() || $rightType instanceof MixedType)
) {
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 +1170,21 @@ public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Typ
$rightType = $this->optimizeScalarType($rightType);
}

if ($leftType->isString()->yes() && $rightType->isString()->yes()) {
if ($leftType instanceof MixedType && $rightType instanceof MixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

$leftIsString = $leftType->isString();
$rightIsString = $rightType->isString();
if (
($leftIsString->yes() || $leftType instanceof MixedType)
&& ($rightIsString->yes() || $rightType instanceof MixedType)
) {
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('(int|string)', $a & $b);
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('(int|string)', $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('(int|string)', $a ^ $b);
assertType('0', 1 ^ 1);
assertNativeType('0', 1 ^ 1);
assertType('int', $c ^ $d);
Expand Down
63 changes: 63 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bitwise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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('*ERROR*', $string & $int);
assertType('string', $string & $string);
assertType('*ERROR*', $string & $stringOrInt);
assertType('string', $string & $mixed);
assertType('*ERROR*', $stringOrInt & $int);
assertType('*ERROR*', $stringOrInt & $string);
assertType('*ERROR*', $stringOrInt & $stringOrInt);
assertType('*ERROR*', $stringOrInt & $mixed);
assertType('int', $mixed & $int);
assertType('string', $mixed & $string);
assertType('*ERROR*', $mixed & $stringOrInt);
assertType('(int|string)', $mixed & $mixed);

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

assertType('int', $int ^ $int);
assertType('*ERROR*', $int ^ $string);
assertType('*ERROR*', $int ^ $stringOrInt);
assertType('int', $int ^ $mixed);
assertType('*ERROR*', $string ^ $int);
assertType('string', $string ^ $string);
assertType('*ERROR*', $string ^ $stringOrInt);
assertType('string', $string ^ $mixed);
assertType('*ERROR*', $stringOrInt ^ $int);
assertType('*ERROR*', $stringOrInt ^ $string);
assertType('*ERROR*', $stringOrInt ^ $stringOrInt);
assertType('*ERROR*', $stringOrInt ^ $mixed);
assertType('int', $mixed ^ $int);
assertType('string', $mixed ^ $string);
assertType('*ERROR*', $mixed ^ $stringOrInt);
assertType('(int|string)', $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));
}
Loading