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
71 changes: 71 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 @@ -2967,6 +2968,57 @@
return $specifiedTypes;
}

/**
* When $castExpr casts $valueExpr to an int and back to a string — i.e. the
* `(string) (int) $valueExpr` round-trip (in any combination of the (string)/(int)
* casts) — returns $valueExpr so the
* comparison `$castExpr === $valueExpr` can narrow it to a decimal / non-decimal
* integer string. This is the canonical "decimal integer string" round-trip that
* ConstantStringType::isDecimalIntegerString() checks with `(string) (int) $value === $value`.
*
* The cast forms are stripped from the AST only to match the value expression; whether
* the casts actually compute the int-then-string round-trip is confirmed through the
* type system via Type::toInteger() and Type::toString() rather than by relying on the
* exact order or shape of the casts.
*/
private function getDecimalIntegerStringRoundTripExpr(Expr $castExpr, Expr $valueExpr, Scope $scope): ?Expr
{
$valueType = $scope->getType($valueExpr);
if (!$valueType->isString()->yes()) {

Check warning on line 2987 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ private function getDecimalIntegerStringRoundTripExpr(Expr $castExpr, Expr $valueExpr, Scope $scope): ?Expr { $valueType = $scope->getType($valueExpr); - if (!$valueType->isString()->yes()) { + if ($valueType->isString()->no()) { return null; }
return null;
}

$baseExpr = $castExpr;
$unwrapped = false;
while (($inner = $this->getCastInnerExpr($baseExpr)) !== null) {
$baseExpr = $inner;
$unwrapped = true;
}

if (
!$unwrapped
|| $this->exprPrinter->printExpr($baseExpr) !== $this->exprPrinter->printExpr($valueExpr)
) {
return null;
}

return $scope->getType($castExpr)->equals($valueType->toInteger()->toString())
? $valueExpr
: null;
}

/**
* Strips a single (string)/(int) cast, returning its inner expression.
*/
private function getCastInnerExpr(Expr $expr): ?Expr
{
if ($expr instanceof Expr\Cast\String_ || $expr instanceof Expr\Cast\Int_) {
return $expr->expr;
}

return null;
}

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

// (string) (int) $x === $x
if (!$context->null()) {
$decimalValueExpr = $this->getDecimalIntegerStringRoundTripExpr($unwrappedLeftExpr, $unwrappedRightExpr, $scope)
?? $this->getDecimalIntegerStringRoundTripExpr($unwrappedRightExpr, $unwrappedLeftExpr, $scope);

if ($decimalValueExpr !== null) {
$decimalValueType = $scope->getType($decimalValueExpr);
$decimalIntegerString = new AccessoryDecimalIntegerStringType();
return $this->create(
$decimalValueExpr,
$context->truthy()

Check warning on line 3294 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $decimalIntegerString = new AccessoryDecimalIntegerStringType(); return $this->create( $decimalValueExpr, - $context->truthy() + $context->true() ? TypeCombinator::intersect($decimalValueType, $decimalIntegerString) : TypeCombinator::remove($decimalValueType, $decimalIntegerString), TypeSpecifierContext::createTruthy(),

Check warning on line 3294 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $decimalIntegerString = new AccessoryDecimalIntegerStringType(); return $this->create( $decimalValueExpr, - $context->truthy() + $context->true() ? TypeCombinator::intersect($decimalValueType, $decimalIntegerString) : TypeCombinator::remove($decimalValueType, $decimalIntegerString), TypeSpecifierContext::createTruthy(),
? TypeCombinator::intersect($decimalValueType, $decimalIntegerString)
: TypeCombinator::remove($decimalValueType, $decimalIntegerString),
TypeSpecifierContext::createTruthy(),
$scope,
)->setRootExpr($expr);
}
}

if ($rightType->isString()->yes()) {
$types = null;
foreach ($rightType->getConstantStrings() as $constantString) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryDecimalIntegerStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function __construct(private bool $inverse = false)
{
}

public function isInverse(): bool
{
return $this->inverse;
}

public function getReferencedClasses(): array
{
return [];
Expand Down
4 changes: 4 additions & 0 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ public function tryRemove(Type $typeToRemove): ?Type
return new ConstantStringType('');
}

if ($typeToRemove instanceof AccessoryDecimalIntegerStringType) {
return TypeCombinator::intersect($this, new AccessoryDecimalIntegerStringType(!$typeToRemove->isInverse()));
}

return null;
}

Expand Down
58 changes: 58 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,58 @@
<?php declare(strict_types = 1);

namespace DecimalIntStringCast;

use function PHPStan\Testing\assertType;

class Foo
{

public function castIdentical(string $s): void
{
if ((string) (int) $s === $s) {
Comment thread
staabm marked this conversation as resolved.
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 castIdenticalDifferentOrder(string $s): void
{
// the casts can appear in any combination as long as the chain
// computes the int-then-string round-trip
if ((string) (int) (string) (int) $s === $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 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);
}
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5737,6 +5737,18 @@ public static function dataRemove(): array
NeverType::class,
'*NEVER*=implicit',
],
[
new StringType(),
new AccessoryDecimalIntegerStringType(),
IntersectionType::class,
'non-decimal-int-string',
],
[
new StringType(),
new AccessoryDecimalIntegerStringType(inverse: true),
IntersectionType::class,
'decimal-int-string',
],
[
new ConstantBooleanType(true),
new ConstantBooleanType(false),
Expand Down
Loading