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
35 changes: 34 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6808,15 +6808,48 @@
}

$additionalExpressions = [];
$offsetValueType = $valueToWrite;
$lastDimKey = array_key_last($dimFetchStack);

// Compute improved intermediate types bottom-up using scope types.
// The top-down derivation from the root type loses constant array
// precision (e.g. array{-1: 0, 0: 0} becomes array<-1|0, int>).
// By applying the write to the scope's tracked constant array type,
// we preserve the constant array structure through loop generalization.
$improvedTypes = [];
$childPostWriteType = $originalValueToWrite;
for ($key = ($lastDimKey ?? 0) - 1; $key >= 0; $key--) {
$dimFetch = $dimFetchStack[$key];
if ($dimFetch->dim === null) {
break;
}

$nextDimFetch = $dimFetchStack[$key + 1];
if ($nextDimFetch->dim === null || !$scope->hasExpressionType($dimFetch)->yes()) {

Check warning on line 6827 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $nextDimFetch = $dimFetchStack[$key + 1]; - if ($nextDimFetch->dim === null || !$scope->hasExpressionType($dimFetch)->yes()) { + if ($nextDimFetch->dim === null || $scope->hasExpressionType($dimFetch)->no()) { break; }

Check warning on line 6827 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $nextDimFetch = $dimFetchStack[$key + 1]; - if ($nextDimFetch->dim === null || !$scope->hasExpressionType($dimFetch)->yes()) { + if ($nextDimFetch->dim === null || $scope->hasExpressionType($dimFetch)->no()) { break; }
break;
}

$scopeType = $scope->getType($dimFetch);
$childOffset = $scope->getType($nextDimFetch->dim);

if (!$scopeType->hasOffsetValueType($childOffset)->yes()) {

Check warning on line 6834 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $scopeType = $scope->getType($dimFetch); $childOffset = $scope->getType($nextDimFetch->dim); - if (!$scopeType->hasOffsetValueType($childOffset)->yes()) { + if ($scopeType->hasOffsetValueType($childOffset)->no()) { break; }

Check warning on line 6834 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $scopeType = $scope->getType($dimFetch); $childOffset = $scope->getType($nextDimFetch->dim); - if (!$scopeType->hasOffsetValueType($childOffset)->yes()) { + if ($scopeType->hasOffsetValueType($childOffset)->no()) { break; }
break;
}

$improvedType = $scopeType->setExistingOffsetValueType($childOffset, $childPostWriteType);
$improvedTypes[$key] = $improvedType;
$childPostWriteType = $improvedType;
}

$offsetValueType = $valueToWrite;
foreach ($dimFetchStack as $key => $dimFetch) {
if ($dimFetch->dim === null) {
continue;
}

if ($key === $lastDimKey) {
$offsetValueType = $originalValueToWrite;
} elseif (isset($improvedTypes[$key])) {
$offsetValueType = $improvedTypes[$key];
} else {
$offsetType = $scope->getType($dimFetch->dim);
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,4 +1145,11 @@ public function testBug11276(): void
$this->analyse([__DIR__ . '/data/bug-11276.php'], []);
}

public function testBug13669(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-13669.php'], []);
}

}
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-13669.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace Bug13669;

final class Foo
{
/**
* @var array<int, array<MailStatus::CODE_*, int>>
*/
private array $mailCounts;

/** @var array<int, array<MailStatus::CODE_*>> */
private array $sources;

/** @param array<int, array<MailStatus::CODE_*>> $sources */
private function __construct(array $sources)
{
$this->mailCounts = [];
$this->sources = $sources;
}


public function countMailStates(): void
{
foreach ($this->sources as $templateId => $mails) {
$this->mailCounts[$templateId] = [
MailStatus::CODE_DELETED => 0,
MailStatus::CODE_NOT_ACTIVE => 0,
MailStatus::CODE_ACTIVE => 0,
MailStatus::CODE_SIMULATION => 0,
];

foreach ($mails as $mail) {
++$this->mailCounts[$templateId][$mail];
}
}
}

}

final class MailStatus
{
public const CODE_DELETED = -1;

public const CODE_NOT_ACTIVE = 0;

public const CODE_SIMULATION = 1;

public const CODE_ACTIVE = 2;
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Operators/InvalidBinaryOperationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,22 @@ public function testBug10595(): void
$this->analyse([__DIR__ . '/data/bug-10595.php'], []);
}

public function testBug10349(): void
{
$this->analyse([__DIR__ . '/data/bug-10349.php'], [
[
'Binary operation "+=" between bool|float|int|string and int results in an error.',
17,
],
[
'Binary operation "+=" between bool|float|int|string and int results in an error.',
42,
],
[
'Binary operation "+=" between bool|float|int|string and int results in an error.',
59,
],
]);
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Rules/Operators/data/bug-10349.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Bug10349;

class Foo
{
/**
* @param int $ptr
* @param array<int, array<string, scalar>> $expected
*
* @return array<int, array<string, scalar>>
*/
private function issue_1_A($ptr, $expected)
{
foreach ($expected as $key => $param) {
if ($param['number-1'] !== false) {
$expected[$key]['number-1'] += $ptr;
}

if ($param['number-2'] !== false) {
$expected[$key]['number-2'] += $ptr;
}
}

return $expected;
}

/**
* @param int $ptr
* @param array<int, array<string, scalar>> $expected
*
* @return array<int, array<string, scalar>>
*/
private function issue_1_B($ptr, $expected)
{
foreach ($expected as $key => $param) {
if (is_int($expected[$key]['number-1'])) {
$expected[$key]['number-1'] += $ptr;
}

if ($param['number-2'] !== false) {
$expected[$key]['number-2'] += $ptr;
}
}

return $expected;
}

/**
* @param int $ptr
* @param array<int, array<string, scalar>> $expected
*
* @return array<int, array<string, scalar>>
*/
private function issue_2($ptr, $expected)
{
foreach ($expected as $key => $param) {
if (is_int($param['number-1'])) {
$expected[$key]['number-1'] += $ptr;
}
if (is_int($param['number-2'])) {
$expected[$key]['number-2'] += $ptr;
}
}

return $expected;
}
}
Loading