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: 2 additions & 2 deletions src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): ar
}
if ($tagValue->variableName !== '') {
$variableName = substr($tagValue->variableName, 1);
$resolved[$variableName] = new VarTag($type);
$resolved[$variableName] = new VarTag($type, true);
} else {
$varTag = new VarTag($type);
$varTag = new VarTag($type, true);
$tagResolved[] = $varTag;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpDoc/ResolvedPhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ private static function mergeVarTags(array $varTags, array $parents, array $pare
private static function mergeOneParentVarTags(self $parent, PhpDocBlock $phpDocBlock): ?array
{
foreach ($parent->getVarTags() as $key => $parentVarTag) {
return [$key => self::resolveTemplateTypeInTag($parentVarTag, $phpDocBlock, TemplateTypeVariance::createInvariant())];
return [$key => self::resolveTemplateTypeInTag($parentVarTag->toImplicit(), $phpDocBlock, TemplateTypeVariance::createInvariant())];
}

return null;
Expand Down
14 changes: 12 additions & 2 deletions src/PhpDoc/Tag/VarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class VarTag implements TypedTag
{

public function __construct(private Type $type)
public function __construct(private Type $type, private bool $isExplicit)
{
}

Expand All @@ -25,7 +25,17 @@ public function getType(): Type
*/
public function withType(Type $type): TypedTag
{
return new self($type);
return new self($type, $this->isExplicit);
}

public function isExplicit(): bool
{
return $this->isExplicit;
}

public function toImplicit(): self
{
return new self($this->type, false);
}

}
12 changes: 8 additions & 4 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,6 @@ public function getConstant(string $name): ClassConstantReflection
$isDeprecated = $resolvedPhpDoc->isDeprecated();
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$varTags = $resolvedPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$phpDocType = $varTags[0]->getType();
}

$nativeType = null;
if ($reflectionConstant->getType() !== null) {
Expand All @@ -1106,6 +1102,14 @@ public function getConstant(string $name): ClassConstantReflection
$nativeType = $this->signatureMapProvider->getClassConstantMetadata($declaringClass->getName(), $name)['nativeType'];
}

$varTags = $resolvedPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$varTag = $varTags[0];
if ($varTag->isExplicit() || $nativeType === null || $nativeType->isSuperTypeOf($varTag->getType())->yes()) {
$phpDocType = $varTag->getType();
}
}

$this->constants[$name] = new ClassConstantReflection(
$this->initializerExprTypeResolver,
$declaringClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public function testNativeType(): void
]);
}

public function testBug10911(): void
{
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3.');
}

$this->analyse([__DIR__ . '/data/bug-10911.php'], []);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-10911.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 8.3

namespace Bug10911;

abstract class Model
{
/**
* The name of the "created at" column.
*
* @var string|null
*/
const CREATED_AT = 'created_at';

/**
* The name of the "updated at" column.
*
* @var string|null
*/
const UPDATED_AT = 'updated_at';
}

class TestModel extends Model
{
const string CREATED_AT = 'data_criacao';
const string UPDATED_AT = 'data_alteracao';
const DELETED_AT = null;
}
Loading