Skip to content
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
21 changes: 18 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,14 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
$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 @@ -716,9 +721,14 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
$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 @@ -775,9 +785,14 @@ public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCall
$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
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 @@ -2361,6 +2361,11 @@ public function testBug11863(): void
$this->analyse([__DIR__ . '/data/bug-11863.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