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
86 changes: 86 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
Expand Down Expand Up @@ -2898,6 +2899,60 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
return $specifiedTypes;
}

/**
* Returns the inner expression E when $expr casts E to a string and back to an int,
* i.e. `(string) (int) E`, `strval(intval(E))` or any mix of the cast/function forms.
* This is the canonical "decimal integer string" round-trip that
* ConstantStringType::isDecimalIntegerString() checks with `(string) (int) $value === $value`.
*/
private function getDecimalIntegerStringCastedExpr(Expr $expr): ?Expr
{
$intCasted = $this->getStringCastedExpr($expr);
if ($intCasted === null) {
return null;
}

return $this->getIntCastedExpr($intCasted);
}

private function getStringCastedExpr(Expr $expr): ?Expr
{
if ($expr instanceof Expr\Cast\String_) {
return $expr->expr;
}

if (
$expr instanceof FuncCall
&& $expr->name instanceof Name
&& !$expr->isFirstClassCallable()
&& strtolower($expr->name->toString()) === 'strval'
&& count($expr->getArgs()) === 1
) {
return $expr->getArgs()[0]->value;
}

return null;
}

private function getIntCastedExpr(Expr $expr): ?Expr
{
if ($expr instanceof Expr\Cast\Int_) {
return $expr->expr;
}

if (
$expr instanceof FuncCall
&& $expr->name instanceof Name
&& !$expr->isFirstClassCallable()
&& strtolower($expr->name->toString()) === 'intval'
&& count($expr->getArgs()) === 1
) {
return $expr->getArgs()[0]->value;
}

return null;
}

private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$leftExpr = $expr->left;
Expand Down Expand Up @@ -3160,6 +3215,37 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
}
}

// (string) (int) $x === $x (and the strval(intval()) equivalents)
if (!$context->null()) {
$leftCastedExpr = $this->getDecimalIntegerStringCastedExpr($unwrappedLeftExpr);
$rightCastedExpr = $this->getDecimalIntegerStringCastedExpr($unwrappedRightExpr);

$decimalValueExpr = null;
if (
$leftCastedExpr !== null
&& $this->exprPrinter->printExpr($leftCastedExpr) === $this->exprPrinter->printExpr($unwrappedRightExpr)
) {
$decimalValueExpr = $unwrappedRightExpr;
} elseif (
$rightCastedExpr !== null
&& $this->exprPrinter->printExpr($rightCastedExpr) === $this->exprPrinter->printExpr($unwrappedLeftExpr)
) {
$decimalValueExpr = $unwrappedLeftExpr;
}

if ($decimalValueExpr !== null) {
$decimalValueType = $scope->getType($decimalValueExpr);
if ($decimalValueType->isString()->yes()) {
return $this->create(
$decimalValueExpr,
TypeCombinator::intersect($decimalValueType, new AccessoryDecimalIntegerStringType($context->falsey())),
TypeSpecifierContext::createTruthy(),
$scope,
)->setRootExpr($expr);
}
}
}

if ($rightType->isString()->yes()) {
$types = null;
foreach ($rightType->getConstantStrings() as $constantString) {
Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/decimal-int-string-cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace DecimalIntStringCast;

use function PHPStan\Testing\assertType;
use function intval;
use function strval;

class Foo
{

public function castIdentical(string $s): void
{
if ((string) (int) $s === $s) {
assertType('decimal-int-string', $s);
} else {
assertType('non-decimal-int-string', $s);
}
}

public function castIdenticalFlipped(string $s): void
{
if ($s === (string) (int) $s) {
assertType('decimal-int-string', $s);
} else {
assertType('non-decimal-int-string', $s);
}
}

public function castNotIdentical(string $s): void
{
if ((string) (int) $s !== $s) {
assertType('non-decimal-int-string', $s);
} else {
assertType('decimal-int-string', $s);
}
}

public function strvalIntval(string $s): void
{
if (strval(intval($s)) === $s) {
assertType('decimal-int-string', $s);
} else {
assertType('non-decimal-int-string', $s);
}
}

public function mixedCastForms(string $s): void
{
if (strval((int) $s) === $s) {
assertType('decimal-int-string', $s);
}

if ((string) intval($s) === $s) {
assertType('decimal-int-string', $s);
}
}

public function notAlwaysString(int|string $s): void
{
if ((string) (int) $s === $s) {
assertType('decimal-int-string', $s);
} else {
// $s can still be an int here, so we cannot narrow to non-decimal-int-string
assertType('int|string', $s);
}
}

}
Loading