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
51 changes: 51 additions & 0 deletions src/Analyser/Generator/ExprHandler/BitwiseNotHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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;

/**
* @implements ExprHandler<Expr\BitwiseNot>
*/
#[AutowiredService]
final class BitwiseNotHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

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

public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
{
$result = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

used enterDeep as in

$result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep());


return new ExprAnalysisResult(
$this->initializerExprTypeResolver->getBitwiseNotTypeFromType($result->type),
$this->initializerExprTypeResolver->getBitwiseNotTypeFromType($result->nativeType),
$result->scope,
hasYield: $result->hasYield,
isAlwaysTerminating: $result->isAlwaysTerminating,
throwPoints: $result->throwPoints,
impurePoints: $result->impurePoints,
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
6 changes: 6 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,12 @@ public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type
public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type
{
$exprType = $getTypeCallback($expr);

return $this->getBitwiseNotTypeFromType($exprType);
}

public function getBitwiseNotTypeFromType(Type $exprType): Type
{
return TypeTraverser::map($exprType, static function (Type $type, callable $traverse): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public function doMinus($a, $b, int $c, int $d): void
assertNativeType('int', $c - $d);
}

/**
* @param int $a
* @return void
*/
public function doBitwiseNot($a, int $b): void
{
assertType('int', ~$a);
assertNativeType('int', ~$b);
assertType('int', ~1);
assertNativeType('int', ~1);
assertType('int', ~$b);
assertNativeType('int', ~$b);
}

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