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
16 changes: 11 additions & 5 deletions src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ public function specifyTypes(
&& !$keyType instanceof ConstantStringType
) {
if ($context->true()) {
if ($arrayType->isIterableAtLeastOnce()->no()) {
return $this->typeSpecifier->create(
$specifiedTypes = new SpecifiedTypes();

if (count($keyType->getConstantScalarTypes()) <= 1) {
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
$array,
new NonEmptyArrayType(),
$context,
$scope,
);
));
}

if ($arrayType->isIterableAtLeastOnce()->no()) {
return $specifiedTypes;
}

$arrayKeyType = $arrayType->getIterableKeyType();
Expand All @@ -82,12 +88,12 @@ public function specifyTypes(
$arrayKeyType = TypeCombinator::union($arrayKeyType, $arrayKeyType->toString());
}

$specifiedTypes = $this->typeSpecifier->create(
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
$key,
$arrayKeyType,
$context,
$scope,
);
));

$arrayDimFetch = new ArrayDimFetch(
$array,
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug13674a.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bug13674a;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param array<int> $arrayA
* @param list<int> $listA
*/
public function sayHello($arrayA, $listA, int $i): void
{
if (array_key_exists($i, $arrayA)) {
assertType('non-empty-array<int>', $arrayA);
} else {
assertType('array<int>', $arrayA);
}
assertType('array<int>', $arrayA);

if (array_key_exists($i, $listA)) {
assertType('non-empty-list<int>', $listA);
} else {
assertType('list<int>', $listA);
}
assertType('list<int>', $listA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,16 @@ public function testBug7000(): void
]);
}

public function testBug7000b(): void
{
$this->analyse([__DIR__ . '/data/bug-7000b.php'], [
[
"Offset 'require'|'require-dev' might not exist on array{require?: array<string, string>, require-dev?: array<string, string>}.",
16,
],
]);
}

public function testBug6508(): void
{
$this->analyse([__DIR__ . '/data/bug-6508.php'], []);
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-7000b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Bug7000b;

class Foo
{
public function doBar(): void
{
/** @var array{require?: array<string, string>, require-dev?: array<string, string>} $composer */
$composer = array();
/** @var 'require'|'require-dev' $foo */
$foo = '';
foreach (array('require', 'require-dev') as $linkType) {
if (array_key_exists($linkType, $composer)) {
foreach ($composer[$linkType] as $x) {} // should not report error
foreach ($composer[$foo] as $x) {} // should report error. It can be $linkType = 'require', $foo = 'require-dev'
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function doFoo(array $percentageIntervals, array $changes): void
if ($percentageInterval->isInInterval((float) $changeInPercents)) {
$key = $percentageInterval->getFormatted();
if (array_key_exists($key, $intervalResults)) {
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
$intervalResults[$key]['itemsCount'] += $itemsCount;
assertType('non-empty-array<array{itemsCount: (array|float|int), interval: mixed}>', $intervalResults);
Expand Down
Loading