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
4 changes: 0 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3680,10 +3680,6 @@ public function addTypeToExpression(Expr $expr, Type $type): self

if ($originalExprType->equals($nativeType)) {
$newType = TypeCombinator::intersect($type, $originalExprType);
if ($newType->isConstantScalarValue()->yes() && $newType->equals($originalExprType)) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this code was added in the past to improve performance.

after measuring again it seems we no longer need this logic, as it seems we fixed the bottleneck somewhere else.

before this PR

➜  phpstan-src git:(2.1.x) hyperfine 'vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php' --runs=3 --warmup=1
Benchmark 1: vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php
  Time (mean ± σ):     15.854 s ±  0.095 s    [User: 15.462 s, System: 0.383 s]
  Range (min … max):   15.749 s … 15.934 s    3 runs

with deleted logic

➜  phpstan-src git:(bug13526) hyperfine 'vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php' --runs=3 --warmup=1
Benchmark 1: vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php
  Time (mean ± σ):     15.840 s ±  0.213 s    [User: 15.455 s, System: 0.375 s]
  Range (min … max):   15.642 s … 16.065 s    3 runs

// don't add the same type over and over again to improve performance
return $this;
}
return $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,4 +1155,10 @@ public function testBug11276(): void
$this->analyse([__DIR__ . '/data/bug-11276.php'], []);
}

public function testBug13526(): void
{
$this->reportPossiblyNonexistentConstantArrayOffset = true;
$this->analyse([__DIR__ . '/data/bug-13526.php'], []);
}

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

namespace Bug13526;

class Foo
{
public function foo(string $item, bool $a): ?int
{
$map1 = [
'MAP_1_ITEM_1' => 1,
'MAP_1_ITEM_2' => 2,
];
$map2 = [
'MAP_2_ITEM_1' => 1,
'MAP_2_ITEM_2' => 2,
];

$map = $a ? $map1 : $map2;

if (array_key_exists($item, $map)) {
return $map[$item];
}

return null;
}
}
Loading