Skip to content
Closed
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ parameters:
-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
identifier: phpstanApi.instanceofType
count: 4
count: 5
path: src/Analyser/TypeSpecifier.php

-
Expand Down
46 changes: 46 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,52 @@
return $exprType;
}

if (
$issetExpr instanceof ArrayDimFetch
&& $issetExpr->dim !== null
) {
$varType = $scope->getType($issetExpr->var);
if (!$varType instanceof MixedType) {
$dimType = $scope->getType($issetExpr->dim);

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we have to explicitely check those instanceof.
I think it could work for every dimType.

but this seems to be the same strategy than

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the quoted TypeSpecifier location we need the
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {

because HasOffsetType constructor types

$types = $varType instanceof UnionType ? $varType->getTypes() : [$varType];
$typesToRemove = [];
foreach ($types as $innerType) {
$hasOffset = $innerType->hasOffsetValueType($dimType);
if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) {

Check warning on line 936 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": @@ @@ $typesToRemove = []; foreach ($types as $innerType) { $hasOffset = $innerType->hasOffsetValueType($dimType); - if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { + if (!$hasOffset->yes() || $innerType->getOffsetValueType($dimType)->isNull()->yes()) { continue; }

Check warning on line 936 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": @@ @@ $typesToRemove = []; foreach ($types as $innerType) { $hasOffset = $innerType->hasOffsetValueType($dimType); - if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { + if ($hasOffset->no() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { continue; }

Check warning on line 936 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\TrinaryLogicMutator": @@ @@ $typesToRemove = []; foreach ($types as $innerType) { $hasOffset = $innerType->hasOffsetValueType($dimType); - if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { + if (!$hasOffset->yes() || $innerType->getOffsetValueType($dimType)->isNull()->yes()) { continue; }

Check warning on line 936 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\TrinaryLogicMutator": @@ @@ $typesToRemove = []; foreach ($types as $innerType) { $hasOffset = $innerType->hasOffsetValueType($dimType); - if (!$hasOffset->yes() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { + if ($hasOffset->no() || !$innerType->getOffsetValueType($dimType)->isNull()->no()) { continue; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It miss some case with array{foo?: bool} or array{foo: string|null}

continue;
}

$typesToRemove[] = $innerType;
}

if ($typesToRemove !== []) {
$typeToRemove = TypeCombinator::union(...$typesToRemove);
$result = $this->create(
$issetExpr->var,
$typeToRemove,
TypeSpecifierContext::createFalse(),
$scope,
)->setRootExpr($expr);

if ($scope->hasExpressionType($issetExpr->var)->maybe()) {
$result = $result->unionWith(
$this->create(
new IssetExpr($issetExpr->var),
new NullType(),
TypeSpecifierContext::createTruthy(),
$scope,
)->setRootExpr($expr),
);
}

return $result;
}
}
}
}

return new SpecifiedTypes();
}

Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10544.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug10544;

use function PHPStan\Testing\assertType;

class Boo {
/**
* @param array{check1:bool,boo:string}|array{check2:bool,foo:string} $param
*/
public function foo(array $param): string {
if (isset($param['check1'])) {
assertType('array{check1: bool, boo: string}', $param);
return $param['boo'];
}
assertType('array{check2: bool, foo: string}', $param);
return $param['foo'];
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12401.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug12401;

use function PHPStan\Testing\assertType;

/**
* @param array{a: string, b: string}|array{c: string} $data
*/
function test(array $data): void {
if (isset($data['a'])) {
assertType('array{a: string, b: string}', $data);
} else {
assertType('array{c: string}', $data);
}

if (isset($data['a'])) {
assertType('array{a: string, b: string}', $data);
} elseif (isset($data['c'])) {
assertType('array{c: string}', $data);
} else {
assertType('*NEVER*', $data);
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug5128;

use function PHPStan\Testing\assertType;

/**
* @param array{a: string}|array{b: string} $array
*/
function a(array $array): string {
if (isset($array['a'])) {
assertType('array{a: string}', $array);
return $array['a'];
}

assertType('array{b: string}', $array);
return $array['b'];
}
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-8724.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug8724;

use function PHPStan\Testing\assertType;

/**
* @phpstan-type NumberFilterStructure array{
* type: 'equals'|'notEqual'|'lessThan'|'lessThanOrEqual'|'greaterThan'|'greaterThanOrEqual'|'inRange'|'blank'|'notBlank',
* }
* @phpstan-type CombinedNumberFilterStructure array{
* operator: 'AND'|'OR'
* }
*/
class HelloWorld
{
/** @param NumberFilterStructure|CombinedNumberFilterStructure $filter */
public function test(array $filter): void
{
if (isset($filter['operator'])) {
assertType("array{operator: 'AND'|'OR'}", $filter);
return;
}

assertType("array{type: 'blank'|'equals'|'greaterThan'|'greaterThanOrEqual'|'inRange'|'lessThan'|'lessThanOrEqual'|'notBlank'|'notEqual'}", $filter);
echo $filter['type'];
}
}
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9908.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace Bug9908;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function test(): void
{
$a = [];
if (rand() % 2) {
$a = ['bar' => 'string'];
}

if (isset($a['bar'])) {
$a['bar'] = 1;
}

assertType("array{}|array{bar: 1}", $a);
}

/**
* @param array{bar?: int} $foo
*/
public function sayHello(array $foo): void
{
echo 'Hello' . print_r($foo, true);
}

public function test2(): void
{
$a = [];
if (rand() % 2) {
$a = ['bar' => 'string'];
}

if (isset($a['bar'])) {
$a['bar'] = 1;
}

$this->sayHello($a);
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/tagged-unions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function doFoo4(array $foo)
if (isset($foo['C'])) {
assertType("array{A: string, C: 1}", $foo);
} else {
assertType("array{A: int, B: 1}|array{A: string, C: 1}", $foo); // could be array{A: int, B: 1}
assertType("array{A: int, B: 1}", $foo);
}

assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
Expand Down
Loading