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

/**
* @implements ExprHandler<Expr\Cast\Array_>
*/
#[AutowiredService]
final class CastArrayHandler implements ExprHandler
{

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

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

return new ExprAnalysisResult(
$exprResult->type->toArray(),
$exprResult->nativeType->toArray(),
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

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

/**
* @implements ExprHandler<Expr\Cast\Bool_>
*/
#[AutowiredService]
final class CastBoolHandler implements ExprHandler
{

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

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

return new ExprAnalysisResult(
$exprResult->type->toBoolean(),
$exprResult->nativeType->toBoolean(),
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

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

/**
* @implements ExprHandler<Expr\Cast\Double>
*/
#[AutowiredService]
final class CastDoubleHandler implements ExprHandler
{

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

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

return new ExprAnalysisResult(
$exprResult->type->toFloat(),
$exprResult->nativeType->toFloat(),
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

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

/**
* @implements ExprHandler<Expr\Cast\String_>
*/
#[AutowiredService]
final class CastStringHandler implements ExprHandler
{

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

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

return new ExprAnalysisResult(
$exprResult->type->toString(),
$exprResult->nativeType->toString(),
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ public function doSpaceship($a, $b, string $c, string $d): void
assertNativeType('int<-1, 1>', $c <=> $d);
}

function doCast() {
$a = '1';

assertType('1', (int) $a);
assertType("array{'1'}", (array) $a);
// assertType("array{'1'}", (object) $a);
assertType('1.0', (double) $a);
assertType('true', (bool) $a);
assertType("'1'", (string) $a);
}

}

function (): void {
Expand Down
Loading