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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function processNode(Node $node, Scope $scope): ?array

$printedParamTypesString = $this->collectorMetadataPrinter->printParamTypesToString(
$node,
$classReflection->getName()
$classReflection,
$scope
);
return [$classReflection->getName(), $methodName, $printedParamTypesString, $node->getLine()];
}
Expand Down
56 changes: 42 additions & 14 deletions src/Printer/CollectorMetadataPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\UnionType;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ClosureType;
Expand Down Expand Up @@ -88,29 +89,56 @@ public function printArgTypesAsString(
return implode('|', $stringArgTypes);
}

public function printParamTypesToString(ClassMethod $classMethod, string $className): string
{
public function printParamTypesToString(
ClassMethod $classMethod,
ClassReflection $classReflection,
Scope $scope
): string {
$className = $classReflection->getName();

$parametersReflection = [];
if ($classReflection->hasMethod($classMethod->name->name)) {
$methodReflection = $classReflection->getMethod($classMethod->name->name, $scope);
$variants = $methodReflection->getVariants();
if (count($variants) === 1) {
$parametersReflection = $variants[0]->getParameters();
}
}

$printedParamTypes = [];
foreach ($classMethod->params as $param) {
foreach ($classMethod->params as $i => $param) {
if ($param->type === null) {
$printedParamTypes[] = '';
continue;
}

$paramType = $this->transformSelfToClassName($param->type, $className);
if ($paramType instanceof NullableType) {
// unite to phpstan type
$paramType = new UnionType([$paramType->type, new Identifier('null')]);
$phpdocType = null;
if (array_key_exists($i, $parametersReflection)) {
$paramphpdocType = $parametersReflection[$i]->getPhpDocType();
if (! $paramphpdocType instanceof MixedType) {
$phpdocType = $paramphpdocType;
}
}

if ($paramType instanceof UnionType || $paramType instanceof NodeIntersectionType) {
$paramType = $this->resolveSortedTypes($paramType, $className);
}
if ($phpdocType instanceof Type) {
$printedParamType = $this->printTypeToString($phpdocType);
} else {
$paramType = $this->transformSelfToClassName($param->type, $className);

if ($paramType instanceof NullableType) {
// unite to phpstan type
$paramType = new UnionType([$paramType->type, new Identifier('null')]);
}

$printedParamType = $this->standard->prettyPrint([$paramType]);
$printedParamType = str_replace('\Closure', 'callable', $printedParamType);
$printedParamType = ltrim($printedParamType, '\\');
$printedParamType = str_replace('|\\', '|', $printedParamType);
if ($paramType instanceof UnionType || $paramType instanceof NodeIntersectionType) {
$paramType = $this->resolveSortedTypes($paramType, $className);
}

$printedParamType = $this->standard->prettyPrint([$paramType]);
$printedParamType = str_replace('\Closure', 'callable', $printedParamType);
$printedParamType = ltrim($printedParamType, '\\');
$printedParamType = str_replace('|\\', '|', $printedParamType);
}

// to avoid DateTime vs DateTimeImmutable vs DateTimeInterface conflicts
$printedParamType = $this->normalizeDateTime($printedParamType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class User
public function doFoo(GenericA $g):void
{
}
}

/** @param GenericA<string> $g */
function doFoo($g):void {
$user = new User();
$user->doFoo($g);
/** @param GenericA<string> $g */
function doBar($g):void {
$this->doFoo($g);
}
}