Skip to content

Commit

Permalink
Read PHPDocs for built-in method even if the method has multiple vari…
Browse files Browse the repository at this point in the history
…ants
  • Loading branch information
ondrejmirtes committed Apr 23, 2021
1 parent ce5f316 commit b48fd77
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ private function createMethod(
$stubPhpDocParameterVariadicity = [];
$phpDocParameterTypes = [];
$phpDocReturnType = null;
$stubPhpDocPair = null;
if (count($variantNumbers) === 1) {
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($declaringClass, $methodReflection->getName(), array_map(static function (ParameterSignature $parameterSignature): string {
return $parameterSignature->getName();
Expand All @@ -505,36 +506,37 @@ private function createMethod(
);
$stubPhpDocParameterVariadicity[$name] = $paramTag->isVariadic();
}
} elseif ($reflectionMethod !== null && $reflectionMethod->getDocComment() !== false) {
$filename = $reflectionMethod->getFileName();
if ($filename !== false) {
$phpDocBlock = $this->fileTypeMapper->getResolvedPhpDoc(
$filename,
$declaringClassName,
null,
$reflectionMethod->getName(),
$reflectionMethod->getDocComment()
);
$throwsTag = $phpDocBlock->getThrowsTag();
if ($throwsTag !== null) {
$throwType = $throwsTag->getType();
}
$returnTag = $phpDocBlock->getReturnTag();
if ($returnTag !== null) {
$phpDocReturnType = $returnTag->getType();
}
foreach ($phpDocBlock->getParamTags() as $name => $paramTag) {
$phpDocParameterTypes[$name] = $paramTag->getType();
}

$signatureParameters = $methodSignature->getParameters();
foreach ($reflectionMethod->getParameters() as $paramI => $reflectionParameter) {
if (!array_key_exists($paramI, $signatureParameters)) {
continue;
}
}
}
if ($stubPhpDocPair === null && $reflectionMethod !== null && $reflectionMethod->getDocComment() !== false) {
$filename = $reflectionMethod->getFileName();
if ($filename !== false) {
$phpDocBlock = $this->fileTypeMapper->getResolvedPhpDoc(
$filename,
$declaringClassName,
null,
$reflectionMethod->getName(),
$reflectionMethod->getDocComment()
);
$throwsTag = $phpDocBlock->getThrowsTag();
if ($throwsTag !== null) {
$throwType = $throwsTag->getType();
}
$returnTag = $phpDocBlock->getReturnTag();
if ($returnTag !== null) {
$phpDocReturnType = $returnTag->getType();
}
foreach ($phpDocBlock->getParamTags() as $name => $paramTag) {
$phpDocParameterTypes[$name] = $paramTag->getType();
}

$phpDocParameterNameMapping[$signatureParameters[$paramI]->getName()] = $reflectionParameter->getName();
$signatureParameters = $methodSignature->getParameters();
foreach ($reflectionMethod->getParameters() as $paramI => $reflectionParameter) {
if (!array_key_exists($paramI, $signatureParameters)) {
continue;
}

$phpDocParameterNameMapping[$signatureParameters[$paramI]->getName()] = $reflectionParameter->getName();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2906.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4821.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug4821;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertVariableCertainty;

class HelloWorld
{
public function sayHello(): void
{
try {
$object = new HelloWorld();
$method = new \ReflectionMethod($object, 'nonExisting');
$method->invoke($object);
return;
} catch (\ReflectionException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $object);
assertVariableCertainty(TrinaryLogic::createMaybe(), $method);
}
}
}

0 comments on commit b48fd77

Please sign in to comment.