Skip to content

Commit

Permalink
Map phpDoc parameter names for native methods because stub parameter …
Browse files Browse the repository at this point in the history
…names might be different
  • Loading branch information
ondrejmirtes committed Nov 13, 2020
1 parent 128492d commit 9afec60
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ private function createMethod(
}
foreach ($variantNumbers as $variantNumber) {
$methodSignature = $this->signatureMapProvider->getMethodSignature($declaringClassName, $methodReflection->getName(), $reflectionMethod, $variantNumber);
$phpDocParameterNameMapping = [];
foreach ($methodSignature->getParameters() as $parameter) {
$phpDocParameterNameMapping[$parameter->getName()] = $parameter->getName();
}
$stubPhpDocReturnType = null;
$stubPhpDocParameterTypes = [];
$stubPhpDocParameterVariadicity = [];
Expand Down Expand Up @@ -517,10 +521,19 @@ private function createMethod(
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;
}

$phpDocParameterNameMapping[$signatureParameters[$paramI]->getName()] = $reflectionParameter->getName();
}
}
}
}
$variants[] = $this->createNativeMethodVariant($methodSignature, $stubPhpDocParameterTypes, $stubPhpDocParameterVariadicity, $stubPhpDocReturnType, $phpDocParameterTypes, $phpDocReturnType);
$variants[] = $this->createNativeMethodVariant($methodSignature, $stubPhpDocParameterTypes, $stubPhpDocParameterVariadicity, $stubPhpDocReturnType, $phpDocParameterTypes, $phpDocReturnType, $phpDocParameterNameMapping);
}

if ($this->signatureMapProvider->hasMethodMetadata($declaringClassName, $methodReflection->getName())) {
Expand Down Expand Up @@ -676,6 +689,7 @@ private function createMethod(
* @param Type|null $stubPhpDocReturnType
* @param array<string, Type> $phpDocParameterTypes
* @param Type|null $phpDocReturnType
* @param array<string, string> $phpDocParameterNameMapping
* @return FunctionVariantWithPhpDocs
*/
private function createNativeMethodVariant(
Expand All @@ -684,19 +698,22 @@ private function createNativeMethodVariant(
array $stubPhpDocParameterVariadicity,
?Type $stubPhpDocReturnType,
array $phpDocParameterTypes,
?Type $phpDocReturnType
?Type $phpDocReturnType,
array $phpDocParameterNameMapping
): FunctionVariantWithPhpDocs
{
$parameters = [];
foreach ($methodSignature->getParameters() as $parameterSignature) {
$type = null;
$phpDocType = null;

$phpDocParameterName = $phpDocParameterNameMapping[$parameterSignature->getName()] ?? $parameterSignature->getName();

if (isset($stubPhpDocParameterTypes[$parameterSignature->getName()])) {
$type = $stubPhpDocParameterTypes[$parameterSignature->getName()];
$phpDocType = $stubPhpDocParameterTypes[$parameterSignature->getName()];
} elseif (isset($phpDocParameterTypes[$parameterSignature->getName()])) {
$phpDocType = $phpDocParameterTypes[$parameterSignature->getName()];
} elseif (isset($phpDocParameterTypes[$phpDocParameterName])) {
$phpDocType = $phpDocParameterTypes[$phpDocParameterName];
}

$parameters[] = new NativeParameterWithPhpDocsReflection(
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,11 @@ public function testBug4006(): void
$this->analyse([__DIR__ . '/data/bug-4006.php'], []);
}

public function testBug4084(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-4084.php'], []);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4084.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug4084;

class Handler implements \SessionUpdateTimestampHandlerInterface
{
/**
* @param string $sessionId
* @param string $data
*/
public function updateTimestamp($sessionId, $data) { return true; }

/**
* @param string $sessionId The session id
*/
public function validateId($sessionId) { return true; }
}

0 comments on commit 9afec60

Please sign in to comment.