Skip to content

Commit

Permalink
Fix normal vs. stub PHPDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 22, 2021
1 parent ae6dca5 commit e9dd3a3
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 74 deletions.
41 changes: 35 additions & 6 deletions src/PhpDoc/PhpDocInheritanceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\FileTypeMapper;
use ReflectionParameter;
use function array_map;
use function strtolower;

class PhpDocInheritanceResolver
{

private FileTypeMapper $fileTypeMapper;

private StubPhpDocProvider $stubPhpDocProvider;

public function __construct(
FileTypeMapper $fileTypeMapper,
StubPhpDocProvider $stubPhpDocProvider,
)
{
$this->fileTypeMapper = $fileTypeMapper;
$this->stubPhpDocProvider = $stubPhpDocProvider;
}

public function resolvePhpDocForProperty(
Expand All @@ -37,7 +43,7 @@ public function resolvePhpDocForProperty(
[],
);

return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $declaringTraitName, null);
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $declaringTraitName, null, $propertyName, null);
}

public function resolvePhpDocForConstant(
Expand All @@ -58,7 +64,7 @@ public function resolvePhpDocForConstant(
[],
);

return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, null, null);
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, null, null, null, $constantName);
}

/**
Expand All @@ -84,10 +90,10 @@ public function resolvePhpDocForMethod(
$positionalParameterNames,
);

return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $phpDocBlock->getTrait(), $methodName);
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $phpDocBlock->getTrait(), $methodName, null, null);
}

private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName): ResolvedPhpDocBlock
private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName, ?string $propertyName, ?string $constantName): ResolvedPhpDocBlock
{
$parents = [];
$parentPhpDocBlocks = [];
Expand All @@ -104,17 +110,40 @@ private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?strin
$parentPhpDocBlock,
$parentPhpDocBlock->getTrait(),
$functionName,
$propertyName,
$constantName,
);
$parentPhpDocBlocks[] = $parentPhpDocBlock;
}

$oneResolvedDockBlock = $this->docBlockToResolvedDocBlock($phpDocBlock, $traitName, $functionName);
$oneResolvedDockBlock = $this->docBlockToResolvedDocBlock($phpDocBlock, $traitName, $functionName, $propertyName, $constantName);
return $oneResolvedDockBlock->merge($parents, $parentPhpDocBlocks);
}

private function docBlockToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName): ResolvedPhpDocBlock
private function docBlockToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName, ?string $propertyName, ?string $constantName): ResolvedPhpDocBlock
{
$classReflection = $phpDocBlock->getClassReflection();
if ($functionName !== null && $classReflection->getNativeReflection()->hasMethod($functionName)) {
$methodReflection = $classReflection->getNativeReflection()->getMethod($functionName);
$stub = $this->stubPhpDocProvider->findMethodPhpDoc($classReflection->getName(), $functionName, array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters()));
if ($stub !== null) {
return $stub;
}
}

if ($propertyName !== null && $classReflection->getNativeReflection()->hasProperty($propertyName)) {
$stub = $this->stubPhpDocProvider->findPropertyPhpDoc($classReflection->getName(), $propertyName);
if ($stub !== null) {
return $stub;
}
}

if ($constantName !== null && $classReflection->getNativeReflection()->hasConstant($constantName)) {
$stub = $this->stubPhpDocProvider->findClassConstantPhpDoc($classReflection->getName(), $constantName);
if ($stub !== null) {
return $stub;
}
}

return $this->fileTypeMapper->getResolvedPhpDoc(
$phpDocBlock->getFile(),
Expand Down
8 changes: 0 additions & 8 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class NativeMethodReflection implements MethodReflection

private TrinaryLogic $hasSideEffects;

private ?string $stubPhpDocString;

private ?Type $throwType;

/**
Expand All @@ -43,7 +41,6 @@ public function __construct(
BuiltinMethodReflection $reflection,
array $variants,
TrinaryLogic $hasSideEffects,
?string $stubPhpDocString,
?Type $throwType,
)
{
Expand All @@ -52,7 +49,6 @@ public function __construct(
$this->reflection = $reflection;
$this->variants = $variants;
$this->hasSideEffects = $hasSideEffects;
$this->stubPhpDocString = $stubPhpDocString;
$this->throwType = $throwType;
}

Expand Down Expand Up @@ -173,10 +169,6 @@ private function isVoid(): bool

public function getDocComment(): ?string
{
if ($this->stubPhpDocString !== null) {
return $this->stubPhpDocString;
}

return $this->reflection->getDocComment();
}

Expand Down
10 changes: 0 additions & 10 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ private function createProperty(
$declaringClassName,
$propertyReflection->getName(),
);
$stubPhpDocString = null;
if ($resolvedPhpDoc === null) {
if ($declaringClassReflection->getFileName() !== null) {
$declaringTraitName = $this->findPropertyTrait($propertyReflection);
Expand Down Expand Up @@ -253,7 +252,6 @@ private function createProperty(
}
} else {
$phpDocBlockClassReflection = $declaringClassReflection;
$stubPhpDocString = $resolvedPhpDoc->getPhpDocString();
}

if ($resolvedPhpDoc !== null) {
Expand Down Expand Up @@ -338,7 +336,6 @@ private function createProperty(
$deprecatedDescription,
$isDeprecated,
$isInternal,
$stubPhpDocString,
);
}

Expand Down Expand Up @@ -463,7 +460,6 @@ private function createMethod(
$i++;
}

$stubPhpDocString = null;
$variants = [];
$reflectionMethod = null;
$throwType = null;
Expand Down Expand Up @@ -491,7 +487,6 @@ private function createMethod(
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($declaringClass, $methodReflection->getName(), array_map(static fn (ParameterSignature $parameterSignature): string => $parameterSignature->getName(), $methodSignature->getParameters()));
if ($stubPhpDocPair !== null) {
[$stubPhpDoc, $stubDeclaringClass] = $stubPhpDocPair;
$stubPhpDocString = $stubPhpDoc->getPhpDocString();
$templateTypeMap = $stubDeclaringClass->getActiveTemplateTypeMap();
$returnTag = $stubPhpDoc->getReturnTag();
if ($returnTag !== null) {
Expand Down Expand Up @@ -561,7 +556,6 @@ private function createMethod(
$methodReflection,
$variants,
$hasSideEffects,
$stubPhpDocString,
$throwType,
);
}
Expand All @@ -573,7 +567,6 @@ private function createMethod(
if ($stubPhpDocPair !== null) {
[$resolvedPhpDoc, $phpDocBlockClassReflection] = $stubPhpDocPair;
}
$stubPhpDocString = null;

if ($resolvedPhpDoc === null) {
if ($declaringClass->getFileName() !== null) {
Expand All @@ -590,8 +583,6 @@ private function createMethod(
);
$phpDocBlockClassReflection = $declaringClass;
}
} else {
$stubPhpDocString = $resolvedPhpDoc->getPhpDocString();
}

$declaringTrait = null;
Expand Down Expand Up @@ -692,7 +683,6 @@ private function createMethod(
$isDeprecated,
$isInternal,
$isFinal,
$stubPhpDocString,
$isPure,
);
}
Expand Down
8 changes: 0 additions & 8 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class PhpMethodReflection implements MethodReflection

private ?bool $isPure;

private ?string $stubPhpDocString;

/** @var FunctionVariantWithPhpDocs[]|null */
private ?array $variants = null;

Expand All @@ -110,7 +108,6 @@ public function __construct(
bool $isDeprecated,
bool $isInternal,
bool $isFinal,
?string $stubPhpDocString,
?bool $isPure,
)
{
Expand All @@ -129,7 +126,6 @@ public function __construct(
$this->isDeprecated = $isDeprecated;
$this->isInternal = $isInternal;
$this->isFinal = $isFinal;
$this->stubPhpDocString = $stubPhpDocString;
$this->isPure = $isPure;
}

Expand All @@ -145,10 +141,6 @@ public function getDeclaringTrait(): ?ClassReflection

public function getDocComment(): ?string
{
if ($this->stubPhpDocString !== null) {
return $this->stubPhpDocString;
}

return $this->reflection->getDocComment();
}

Expand Down
1 change: 0 additions & 1 deletion src/Reflection/Php/PhpMethodReflectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function create(
bool $isDeprecated,
bool $isInternal,
bool $isFinal,
?string $stubPhpDocString,
?bool $isPure = null,
): PhpMethodReflection;

Expand Down
8 changes: 0 additions & 8 deletions src/Reflection/Php/PhpPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class PhpPropertyReflection implements PropertyReflection

private bool $isInternal;

private ?string $stubPhpDocString;

public function __construct(
ClassReflection $declaringClass,
?ClassReflection $declaringTrait,
Expand All @@ -47,7 +45,6 @@ public function __construct(
?string $deprecatedDescription,
bool $isDeprecated,
bool $isInternal,
?string $stubPhpDocString,
)
{
$this->declaringClass = $declaringClass;
Expand All @@ -58,7 +55,6 @@ public function __construct(
$this->deprecatedDescription = $deprecatedDescription;
$this->isDeprecated = $isDeprecated;
$this->isInternal = $isInternal;
$this->stubPhpDocString = $stubPhpDocString;
}

public function getDeclaringClass(): ClassReflection
Expand All @@ -73,10 +69,6 @@ public function getDeclaringTrait(): ?ClassReflection

public function getDocComment(): ?string
{
if ($this->stubPhpDocString !== null) {
return $this->stubPhpDocString;
}

$docComment = $this->reflection->getDocComment();
if ($docComment === false) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser

$typeSpecifier = self::getContainer()->getService('typeSpecifier');
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper);
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper, self::getContainer()->getByType(StubPhpDocProvider::class));

$nodeScopeResolver = new NodeScopeResolver(
$reflectionProvider,
Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Levels/data/stubs-methods-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,5 @@
"message": "Anonymous function should return int but returns string.",
"line": 128,
"ignorable": true
},
{
"message": "Method StubsIntegrationTest\\YetYetAnotherFoo::doFoo() should return stdClass but returns string.",
"line": 219,
"ignorable": true
}
]
27 changes: 0 additions & 27 deletions tests/PHPStan/Levels/data/stubs-methods-6.json

This file was deleted.

0 comments on commit e9dd3a3

Please sign in to comment.