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
53 changes: 53 additions & 0 deletions src/Analyser/Generator/ExprHandler/BitwiseXorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprTypeResolver;
use function array_merge;

/**
* @implements ExprHandler<Expr\BinaryOp\BitwiseXor>
*/
#[AutowiredService]
final class BitwiseXorHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

public function supports(Expr $expr): bool
{
return $expr instanceof Expr\BinaryOp\BitwiseXor;
}

public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
{
$leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback);
$rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback);

return new ExprAnalysisResult(
$this->initializerExprTypeResolver->getBitwiseXorTypeFromTypes($leftResult->type, $rightResult->type),
$this->initializerExprTypeResolver->getBitwiseXorTypeFromTypes($leftResult->nativeType, $rightResult->nativeType),
$rightResult->scope,
hasYield: $leftResult->hasYield || $rightResult->hasYield,
isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating,
throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints),
impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints),
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
5 changes: 5 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,11 @@ public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCall
$leftType = $getTypeCallback($left);
$rightType = $getTypeCallback($right);

return $this->getBitwiseXorTypeFromTypes($leftType, $rightType);
}

public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Type
{
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
return $this->getNeverType($leftType, $rightType);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ public function doBitwiseOr($a, $b, int $c, int $d): void
assertNativeType('int', $c | $d);
}

/**
* @param int $a
* @param int $b
* @return void
*/
public function doBitwiseXor($a, $b, int $c, int $d): void
{
assertType('int', $a ^ $b);
assertNativeType('int', $a ^ $b);
assertType('0', 1 ^ 1);
assertNativeType('0', 1 ^ 1);
assertType('int', $c ^ $d);
assertNativeType('int', $c ^ $d);
}

/**
* @param int $a
* @param int $b
Expand Down
Loading