Skip to content

Commit c657260

Browse files
committed
Fix internal error with checkDynamicProperties: true
1 parent 109b3c9 commit c657260

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

src/Type/IntersectionType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public function getUnresolvedInstancePropertyPrototype(string $propertyName, Cla
561561

562562
$propertiesCount = count($propertyPrototypes);
563563
if ($propertiesCount === 0) {
564-
throw new ShouldNotHappenException();
564+
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
565565
}
566566

567567
if ($propertiesCount === 1) {
@@ -594,7 +594,7 @@ public function getUnresolvedStaticPropertyPrototype(string $propertyName, Class
594594

595595
$propertiesCount = count($propertyPrototypes);
596596
if ($propertiesCount === 0) {
597-
throw new ShouldNotHappenException();
597+
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
598598
}
599599

600600
if ($propertiesCount === 1) {

src/Type/UnionType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public function getUnresolvedInstancePropertyPrototype(string $propertyName, Cla
516516

517517
$propertiesCount = count($propertyPrototypes);
518518
if ($propertiesCount === 0) {
519-
throw new ShouldNotHappenException();
519+
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
520520
}
521521

522522
if ($propertiesCount === 1) {
@@ -549,7 +549,7 @@ public function getUnresolvedStaticPropertyPrototype(string $propertyName, Class
549549

550550
$propertiesCount = count($propertyPrototypes);
551551
if ($propertiesCount === 0) {
552-
throw new ShouldNotHappenException();
552+
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
553553
}
554554

555555
if ($propertiesCount === 1) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Testing\PHPStanTestCase;
6+
7+
class AnalyserWithCheckDynamicPropertiesTest extends PHPStanTestCase
8+
{
9+
10+
public function testBug13529(): void
11+
{
12+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13529.php');
13+
$this->assertCount(1, $errors);
14+
$this->assertSame('Access to an undefined property object::$bar.', $errors[0]->getMessage());
15+
$this->assertSame(8, $errors[0]->getLine());
16+
}
17+
18+
/**
19+
* @param string[]|null $allAnalysedFiles
20+
* @return Error[]
21+
*/
22+
private function runAnalyse(string $file): array
23+
{
24+
$file = $this->getFileHelper()->normalizePath($file);
25+
26+
$analyser = self::getContainer()->getByType(Analyser::class);
27+
$finalizer = self::getContainer()->getByType(AnalyserResultFinalizer::class);
28+
$errors = $finalizer->finalize(
29+
$analyser->analyse([$file], null, null, true),
30+
false,
31+
true,
32+
)->getErrors();
33+
foreach ($errors as $error) {
34+
$this->assertSame($file, $error->getFilePath());
35+
}
36+
37+
return $errors;
38+
}
39+
40+
public static function getAdditionalConfigFiles(): array
41+
{
42+
return array_unique(
43+
array_merge(
44+
parent::getAdditionalConfigFiles(),
45+
[
46+
__DIR__ . '/checkDynamicProperties.neon',
47+
],
48+
),
49+
);
50+
}
51+
52+
53+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
checkDynamicProperties: true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Bug13529;
4+
5+
function broken(string $x): void {
6+
$tmp = json_decode($x, false);
7+
8+
if (!isset($tmp->foo) || !isset($tmp->bar)) {
9+
}
10+
}

0 commit comments

Comments
 (0)