Skip to content

Commit 7cb1e41

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents bd7bbec + 3d82a9c commit 7cb1e41

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/Analyser/ConstantResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function resolveConstantType(string $constantName, Type $constantType): T
431431
return $constantType;
432432
}
433433

434-
public function resolveClassConstantType(string $className, string $constantName, Type $constantType, ?Type $nativeType): Type
434+
public function resolveClassConstantType(string $className, string $constantName, Type $constantType, ?Type $nativeType, ?Type $phpDocType): Type
435435
{
436436
$lookupConstantName = sprintf('%s::%s', $className, $constantName);
437437
if (array_key_exists($lookupConstantName, $this->dynamicConstantNames)) {
@@ -454,6 +454,10 @@ public function resolveClassConstantType(string $className, string $constantName
454454
return $nativeType;
455455
}
456456

457+
if ($phpDocType !== null) {
458+
return $phpDocType;
459+
}
460+
457461
if ($constantType->isConstantValue()->yes()) {
458462
return $constantType->generalize(GeneralizePrecision::lessSpecific());
459463
}

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,11 +2544,13 @@ function (Type $type, callable $traverse): Type {
25442544
if ($reflectionConstant->getType() !== null) {
25452545
$nativeType = TypehintHelper::decideTypeFromReflection($reflectionConstant->getType(), selfClass: $constantClassReflection);
25462546
}
2547+
$phpDocType = $constantClassReflection->getConstant($constantName)->getPhpDocType();
25472548
$types[] = $this->constantResolver->resolveClassConstantType(
25482549
$constantClassReflection->getName(),
25492550
$constantName,
25502551
$constantType,
25512552
$nativeType,
2553+
$phpDocType,
25522554
);
25532555
unset($this->currentlyResolvingClassConstant[$resolvingName]);
25542556
continue;
@@ -2577,6 +2579,7 @@ function (Type $type, callable $traverse): Type {
25772579
$constantName,
25782580
$constantType,
25792581
$nativeType,
2582+
$constantReflection->getPhpDocType(),
25802583
);
25812584
unset($this->currentlyResolvingClassConstant[$resolvingName]);
25822585
$types[] = $constantType;

tests/PHPStan/Analyser/data/dynamic-constant.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ class DynamicConstantClass
1313
const DYNAMIC_CONSTANT_IN_CLASS = 'abcdef';
1414
const DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES_IN_CLASS = 'xyz';
1515
const PURE_CONSTANT_IN_CLASS = 'abc123def';
16+
17+
/** @var string|null */
18+
const DYNAMIC_NULL_WITH_PHPDOC_CONSTANT = null;
19+
20+
/** @var list<string> */
21+
const DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT = [];
22+
23+
/** @var int */
24+
const DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT = null;
1625
}
1726

1827
class NoDynamicConstantClass
@@ -29,5 +38,8 @@ private function rip()
2938
assertType('bool', GLOBAL_DYNAMIC_CONSTANT);
3039
assertType('123', GLOBAL_PURE_CONSTANT);
3140
assertType('string|null', GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES);
41+
assertType('string|null', DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT);
42+
assertType('list<string>', DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT);
43+
assertType('int', DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT);
3244
}
3345
}

tests/PHPStan/Analyser/dynamic-constants.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ includes:
44
parameters:
55
dynamicConstantNames:
66
- DynamicConstants\DynamicConstantClass::DYNAMIC_CONSTANT_IN_CLASS
7+
- DynamicConstants\DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT
8+
- DynamicConstants\DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT
9+
- DynamicConstants\DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT
710
- GLOBAL_DYNAMIC_CONSTANT
811
DynamicConstants\DynamicConstantClass::DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES_IN_CLASS: 'string|null'
912
GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES: 'string|null'

0 commit comments

Comments
 (0)