Skip to content

Commit

Permalink
Php8SignatureMapProvider - prefer functionMap return type if PHP 8 st…
Browse files Browse the repository at this point in the history
…ub file has only PHPDoc
  • Loading branch information
ondrejmirtes committed Dec 15, 2020
1 parent 75d00c9 commit 893813c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Reflection/SignatureMap/Php8SignatureMapProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,21 @@ private function mergeSignatures(FunctionSignature $nativeSignature, FunctionSig
}

$nativeReturnType = $nativeSignature->getNativeReturnType();

return new FunctionSignature(
$parameters,
TypehintHelper::decideType(
if ($nativeReturnType instanceof MixedType && !$nativeReturnType->isExplicitMixed()) {
$returnType = $functionMapSignature->getReturnType();
} else {
$returnType = TypehintHelper::decideType(
$nativeReturnType,
TypehintHelper::decideType(
$nativeSignature->getReturnType(),
$functionMapSignature->getReturnType()
)
),
);
}

return new FunctionSignature(
$parameters,
$returnType,
$nativeReturnType,
$nativeSignature->isVariadic()
);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10526,6 +10526,11 @@ public function dataBugEmptyArray(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-empty-array.php');
}

public function dataBug4205(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4205.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -10717,6 +10722,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataBug4207
* @dataProvider dataBug4206
* @dataProvider dataBugEmptyArray
* @dataProvider dataBug4205
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4205.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Bug4205;

use function PHPStan\Analyser\assertType;

function () {
$result = set_error_handler(function() {}, E_ALL);
assertType('(callable(): mixed)|null', $result);
};

0 comments on commit 893813c

Please sign in to comment.