Skip to content

Commit fa27078

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.1.x
2 parents d4107e4 + f356b16 commit fa27078

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): ar
7676
}
7777
if ($tagValue->variableName !== '') {
7878
$variableName = substr($tagValue->variableName, 1);
79-
$resolved[$variableName] = new VarTag($type);
79+
$resolved[$variableName] = new VarTag($type, true);
8080
} else {
81-
$varTag = new VarTag($type);
81+
$varTag = new VarTag($type, true);
8282
$tagResolved[] = $varTag;
8383
}
8484
}

src/PhpDoc/ResolvedPhpDocBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ private static function mergeVarTags(array $varTags, array $parents, array $pare
886886
private static function mergeOneParentVarTags(self $parent, PhpDocBlock $phpDocBlock): ?array
887887
{
888888
foreach ($parent->getVarTags() as $key => $parentVarTag) {
889-
return [$key => self::resolveTemplateTypeInTag($parentVarTag, $phpDocBlock, TemplateTypeVariance::createInvariant())];
889+
return [$key => self::resolveTemplateTypeInTag($parentVarTag->toImplicit(), $phpDocBlock, TemplateTypeVariance::createInvariant())];
890890
}
891891

892892
return null;

src/PhpDoc/Tag/VarTag.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
final class VarTag implements TypedTag
1111
{
1212

13-
public function __construct(private Type $type)
13+
public function __construct(private Type $type, private bool $isExplicit)
1414
{
1515
}
1616

@@ -21,7 +21,17 @@ public function getType(): Type
2121

2222
public function withType(Type $type): self
2323
{
24-
return new self($type);
24+
return new self($type, $this->isExplicit);
25+
}
26+
27+
public function isExplicit(): bool
28+
{
29+
return $this->isExplicit;
30+
}
31+
32+
public function toImplicit(): self
33+
{
34+
return new self($this->type, false);
2535
}
2636

2737
}

src/Reflection/ClassReflection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,6 @@ public function getConstant(string $name): ClassConstantReflection
11151115
}
11161116
$isInternal = $resolvedPhpDoc->isInternal();
11171117
$isFinal = $resolvedPhpDoc->isFinal();
1118-
$varTags = $resolvedPhpDoc->getVarTags();
1119-
if (isset($varTags[0]) && count($varTags) === 1) {
1120-
$phpDocType = $varTags[0]->getType();
1121-
}
11221118

11231119
$nativeType = null;
11241120
if ($reflectionConstant->getType() !== null) {
@@ -1127,6 +1123,14 @@ public function getConstant(string $name): ClassConstantReflection
11271123
$nativeType = $this->signatureMapProvider->getClassConstantMetadata($declaringClass->getName(), $name)['nativeType'];
11281124
}
11291125

1126+
$varTags = $resolvedPhpDoc->getVarTags();
1127+
if (isset($varTags[0]) && count($varTags) === 1) {
1128+
$varTag = $varTags[0];
1129+
if ($varTag->isExplicit() || $nativeType === null || $nativeType->isSuperTypeOf($varTag->getType())->yes()) {
1130+
$phpDocType = $varTag->getType();
1131+
}
1132+
}
1133+
11301134
$this->constants[$name] = new RealClassClassConstantReflection(
11311135
$this->initializerExprTypeResolver,
11321136
$declaringClass,

tests/PHPStan/Rules/PhpDoc/IncompatibleClassConstantPhpDocTypeRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ public function testNativeType(): void
5050
]);
5151
}
5252

53+
public function testBug10911(): void
54+
{
55+
if (PHP_VERSION_ID < 80300) {
56+
$this->markTestSkipped('Test requires PHP 8.3.');
57+
}
58+
59+
$this->analyse([__DIR__ . '/data/bug-10911.php'], []);
60+
}
61+
5362
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php // lint >= 8.3
2+
3+
namespace Bug10911;
4+
5+
abstract class Model
6+
{
7+
/**
8+
* The name of the "created at" column.
9+
*
10+
* @var string|null
11+
*/
12+
const CREATED_AT = 'created_at';
13+
14+
/**
15+
* The name of the "updated at" column.
16+
*
17+
* @var string|null
18+
*/
19+
const UPDATED_AT = 'updated_at';
20+
}
21+
22+
class TestModel extends Model
23+
{
24+
const string CREATED_AT = 'data_criacao';
25+
const string UPDATED_AT = 'data_alteracao';
26+
const DELETED_AT = null;
27+
}

0 commit comments

Comments
 (0)