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
26 changes: 20 additions & 6 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
}
}

$scopeBeforeArgs = $scope;
$argsResult = $nodeScopeResolver->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallbackForArgs, $context);
$scope = $argsResult->getScope();
$hasYield = $argsResult->hasYield();
Expand Down Expand Up @@ -395,8 +396,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$stmt,
$arrayArg,
new NativeTypeExpr(
$this->getArrayFunctionAppendingType($functionReflection, $scope, $normalizedExpr),
$this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs, $normalizedExpr),
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
),
$nodeCallback,
)->getScope();
Expand Down Expand Up @@ -434,9 +435,22 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$arrayArgType = $scope->getType($arrayArg);
$arrayArgNativeType = $scope->getNativeType($arrayArg);

$offsetType = $scope->getType($normalizedExpr->getArgs()[1]->value);
$lengthType = isset($normalizedExpr->getArgs()[2]) ? $scope->getType($normalizedExpr->getArgs()[2]->value) : new NullType();
$replacementType = isset($normalizedExpr->getArgs()[3]) ? $scope->getType($normalizedExpr->getArgs()[3]->value) : new ConstantArrayType([], []);
$offsetType = $scopeBeforeArgs->getType($normalizedExpr->getArgs()[1]->value);

if (isset($normalizedExpr->getArgs()[2])) {
$lengthType = $scopeBeforeArgs->getType($normalizedExpr->getArgs()[2]->value);
} else {
$lengthType = new NullType();
}

if (isset($normalizedExpr->getArgs()[3])) {
$replacementArg = $normalizedExpr->getArgs()[3]->value;
$replacementType = $scopeBeforeArgs->getType($replacementArg);
$replacementNativeType = $scopeBeforeArgs->getNativeType($replacementArg);
} else {
$replacementType = new ConstantArrayType([], []);
$replacementNativeType = new ConstantArrayType([], []);
}

$scope = $nodeScopeResolver->processVirtualAssign(
$scope,
Expand All @@ -445,7 +459,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$arrayArg,
new NativeTypeExpr(
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementNativeType),
),
$nodeCallback,
)->getScope();
Expand Down
18 changes: 12 additions & 6 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class ArrayType implements Type

private ?Type $cachedIterableKeyType = null;

private ?TrinaryLogic $isList = null;

/** @api */
public function __construct(Type $keyType, private Type $itemType)
{
Expand Down Expand Up @@ -296,15 +298,19 @@ public function isConstantArray(): TrinaryLogic

public function isList(): TrinaryLogic
{
if (IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($this->getKeyType())->no()) {
return TrinaryLogic::createNo();
}
if ($this->isList === null) {
if (IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($this->getKeyType())->no()) {
return $this->isList = TrinaryLogic::createNo();
}

if ($this->getKeyType()->isSuperTypeOf(new ConstantIntegerType(0))->no()) {
return TrinaryLogic::createNo();
if ($this->getKeyType()->isSuperTypeOf(new ConstantIntegerType(0))->no()) {
return $this->isList = TrinaryLogic::createNo();
}

return $this->isList = TrinaryLogic::createMaybe();
}

return TrinaryLogic::createMaybe();
return $this->isList;
}

public function isConstantValue(): TrinaryLogic
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,12 @@ public function testBug14596(): void
$this->assertNotEmpty($errors);
}

public function testBug9172(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9172.php');
$this->assertNotEmpty($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return list<Error>
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9172.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug9172Integration;

final class HelloWorld
{
/** @var int<0, self::MAX_DEPOSIT> */
public const MIN_DEPOSIT = 1_000;

/** @var int<self::MIN_DEPOSIT, max> */
public const MAX_DEPOSIT = 20_000;

/** @param int<self::MIN_DEPOSIT, self::MAX_DEPOSIT> $amount */
public function deposit(int $amount): void
{
}
}

final class CircularValues
{
/** @var int<0, self::MAX> */
public const MIN = self::MAX - 19_000;

/** @var int<self::MIN, max> */
public const MAX = self::MIN + 19_000;
}
76 changes: 76 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13510.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Bug13510;

use function PHPStan\Testing\assertType;

final class Foo
{

/** @param non-empty-list<int> $arr */
public function test(array $arr): void
{
array_unshift($arr, array_pop($arr));
assertType('non-empty-list<int>', $arr);
}

/** @param non-empty-list<int> $arr */
public function testTwoLines(array $arr): void
{
$popped = array_pop($arr);
array_unshift($arr, $popped);
assertType('non-empty-list<int>', $arr);
}
}

class Bar
{
/** @var array<int> */
public array $arr = [];

public function test(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
assertType('non-empty-array<int>', $this->arr);
array_unshift($this->arr, array_pop($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayPush(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_push($this->arr, array_pop($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayUnshiftWithArrayShift(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_unshift($this->arr, array_shift($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayPushWithArrayShift(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_push($this->arr, array_shift($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArraySplice(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_splice($this->arr, 0, 0, [array_pop($this->arr)]);
assertType('non-empty-array<(int<0, max>|string), int>', $this->arr);
}
}
Loading