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/SpaceshipHandler.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\Spaceship>
*/
#[AutowiredService]
final class SpaceshipHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

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

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->getSpaceshipTypeFromTypes($leftResult->type, $rightResult->type),
$this->initializerExprTypeResolver->getSpaceshipTypeFromTypes($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(),
);
}

}
13 changes: 9 additions & 4 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,12 +1156,17 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
$callbackLeftType = $getTypeCallback($left);
$callbackRightType = $getTypeCallback($right);

if ($callbackLeftType instanceof NeverType || $callbackRightType instanceof NeverType) {
return $this->getNeverType($callbackLeftType, $callbackRightType);
return $this->getSpaceshipTypeFromTypes($callbackLeftType, $callbackRightType);
}

public function getSpaceshipTypeFromTypes(Type $leftTypes, Type $rightTypes): Type
{
if ($leftTypes instanceof NeverType || $rightTypes instanceof NeverType) {
return $this->getNeverType($leftTypes, $rightTypes);
}

$leftTypes = $callbackLeftType->getConstantScalarTypes();
$rightTypes = $callbackRightType->getConstantScalarTypes();
$leftTypes = $leftTypes->getConstantScalarTypes();
$rightTypes = $rightTypes->getConstantScalarTypes();

$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ public function doShiftRight($a, $b, int $c, int $d): void
assertType('int', $c >> $d);
assertNativeType('int', $c >> $d);
}

/**
* @param string $a
* @param string $b
* @return void
*/
public function doSpaceship($a, $b, string $c, string $d): void
{
assertType('int<-1, 1>', $a <=> $b);
assertNativeType('int<-1, 1>', $a <=> $b);
assertType('-1', '1' <=> 'a');
assertNativeType('-1', '1' <=> 'a');
assertType('int<-1, 1>', $c <=> $d);
assertNativeType('int<-1, 1>', $c <=> $d);
}

}

function (): void {
Expand Down
Loading