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
16 changes: 14 additions & 2 deletions src/Analyser/ConstantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -424,7 +426,7 @@ public function resolveConstantType(string $constantName, Type $constantType): T
return $constantType;
}
if (in_array($constantName, $this->dynamicConstantNames, true)) {
return $constantType->generalize(GeneralizePrecision::lessSpecific());
return $this->generalizeDynamicConstantType($constantType);
}
}

Expand Down Expand Up @@ -459,13 +461,23 @@ public function resolveClassConstantType(string $className, string $constantName
}

if ($constantType->isConstantValue()->yes()) {
return $constantType->generalize(GeneralizePrecision::lessSpecific());
return $this->generalizeDynamicConstantType($constantType);
}
}

return $constantType;
}

private function generalizeDynamicConstantType(Type $constantType): Type
{
$generalized = $constantType->generalize(GeneralizePrecision::lessSpecific());
if ($generalized->equals(new ConstantArrayType([], []))) {
return new ArrayType(new MixedType(), new MixedType());
}

return $generalized;
}

private function createInteger(?int $min, ?int $max): Type
{
if ($min !== null && $min === $max) {
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/Bug8526IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\PHPStanTestCase;
use PHPUnit\Framework\Attributes\CoversNothing;
use function array_merge;
use function array_unique;

#[CoversNothing]
class Bug8526IntegrationTest extends PHPStanTestCase
{

public function testBug8526(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-8526.php');
$this->assertNoErrors($errors);
}

/**
* @return list<Error>
*/
private function runAnalyse(string $file): array
{
$file = $this->getFileHelper()->normalizePath($file);

$analyser = self::getContainer()->getByType(Analyser::class);
$finalizer = self::getContainer()->getByType(AnalyserResultFinalizer::class);
$errors = $finalizer->finalize(
$analyser->analyse([$file], null, null, true),
false,
true,
)->getErrors();
foreach ($errors as $error) {
$this->assertSame($file, $error->getFilePath());
}

return $errors;
}

public static function getAdditionalConfigFiles(): array
{
return array_unique(
array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/bug-8526.neon',
],
),
);
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/bug-8526.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- ../../../conf/bleedingEdge.neon

parameters:
dynamicConstantNames:
- FOO
- DYNAMICARRAY
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-8526.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug8526;

define('FOO', true);
define('DYNAMICARRAY', []);

function doFoo(): void
{
if (isset(DYNAMICARRAY['MyKey'])) {
echo 'has key';
}
if (FOO) {
echo 'foo';
}
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/dynamic-constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
define('GLOBAL_PURE_CONSTANT', 123);
define('GLOBAL_DYNAMIC_CONSTANT', false);
define('GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES', null);
define('GLOBAL_DYNAMIC_EMPTY_ARRAY', []);

class DynamicConstantClass
{
Expand All @@ -22,6 +23,8 @@ class DynamicConstantClass

/** @var int */
const DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT = null;

const DYNAMIC_EMPTY_ARRAY_NO_PHPDOC = [];
}

class NoDynamicConstantClass
Expand All @@ -41,5 +44,7 @@ private function rip()
assertType('string|null', DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT);
assertType('list<string>', DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT);
assertType('int', DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT);
assertType('array', DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_NO_PHPDOC);
assertType('array', GLOBAL_DYNAMIC_EMPTY_ARRAY);
}
}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/dynamic-constants.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ parameters:
- DynamicConstants\DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT
- DynamicConstants\DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT
- DynamicConstants\DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT
- DynamicConstants\DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_NO_PHPDOC
- GLOBAL_DYNAMIC_CONSTANT
- GLOBAL_DYNAMIC_EMPTY_ARRAY
DynamicConstants\DynamicConstantClass::DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES_IN_CLASS: 'string|null'
GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES: 'string|null'
Loading