Skip to content
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Method | Description
`boolean($value, $message = '')` | Check that a value is a boolean
`scalar($value, $message = '')` | Check that a value is a scalar
`object($value, $message = '')` | Check that a value is an object
`objectish($value, $message = '')` | Check that a value is an object or a string of a class that exists
`resource($value, $type = null, $message = '')` | Check that a value is a resource
`isCallable($value, $message = '')` | Check that a value is a callable
`isArray($value, $message = '')` | Check that a value is an array
Expand Down
31 changes: 26 additions & 5 deletions bin/src/MixinGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ trait Mixin
private function nullOr(ReflectionMethod $method, int $indent): ?string
{
return $this->assertion($method, 'nullOr%s', '%s|null', $indent, function (string $firstParameter, string $parameters) use ($method) {
return "null === {$firstParameter} || static::{$method->name}({$firstParameter}, {$parameters});";
return <<<BODY
null === {$firstParameter} || static::{$method->name}({$firstParameter}, {$parameters});

return {$firstParameter};
BODY;
});
}

Expand All @@ -155,6 +159,8 @@ private function all(ReflectionMethod $method, int $indent): ?string
foreach ({$firstParameter} as \$entry) {
static::{$method->name}(\$entry, {$parameters});
}

return {$firstParameter};
BODY;
});
}
Expand All @@ -176,6 +182,8 @@ private function allNullOr(ReflectionMethod $method, int $indent): ?string
foreach ({$firstParameter} as \$entry) {
null === \$entry || static::{$method->name}(\$entry, {$parameters});
}

return {$firstParameter};
BODY;
});
}
Expand Down Expand Up @@ -238,6 +246,9 @@ private function assertion(ReflectionMethod $method, string $methodNameTemplate,

$paramsAdded = false;

$nativeReturnType = reset($parameterTypes) ?? 'mixed';
$phpdocReturnType = 'mixed';

$phpdocLines = [];
foreach ($parsedComment as $key => $values) {
if ($this->shouldSkipAnnotation($newMethodName, $key)) {
Expand Down Expand Up @@ -267,6 +278,10 @@ private function assertion(ReflectionMethod $method, string $methodNameTemplate,
}
}

if ('psalm-return' === $key || 'return' === $key) {
continue;
}

if ('param' === $key) {
[$longestType, $longestName] = $this->findLongestTypeAndName($values);
}
Expand All @@ -277,6 +292,8 @@ private function assertion(ReflectionMethod $method, string $methodNameTemplate,

if ('psalm-assert' === $key) {
$type = $this->applyTypeTemplate($type, $typeTemplate);

$phpdocReturnType = $type;
}

if ('param' === $key) {
Expand All @@ -301,6 +318,9 @@ private function assertion(ReflectionMethod $method, string $methodNameTemplate,
}
}

$phpdocLines[] = '@return '.$phpdocReturnType;
$phpdocLines[] = '';

$phpdocLinesDeduplicatedEmptyLines = [];
foreach ($phpdocLines as $line) {
$currentLine = trim($line);
Expand All @@ -314,7 +334,7 @@ private function assertion(ReflectionMethod $method, string $methodNameTemplate,
$phpdocLinesDeduplicatedEmptyLines[] = $line;
}

return $this->staticMethod($newMethodName, $parameters, $parameterTypes, $parametersDefaults, $phpdocLinesDeduplicatedEmptyLines, $indent, $body);
return $this->staticMethod($newMethodName, $parameters, $parameterTypes, $parametersDefaults, $phpdocLinesDeduplicatedEmptyLines, $indent, $body, $nativeReturnType);
}

private function reduceParameterType(ReflectionType $type): string
Expand Down Expand Up @@ -351,7 +371,7 @@ private function shouldSkipAnnotation(string $newMethodName, string $key): bool
return false;
}

return 'psalm-assert' === $key;
return 'psalm-assert' === $key || 'psalm-return' === $key;
}

/**
Expand Down Expand Up @@ -400,15 +420,16 @@ private function findLongestTypeAndName(array $values): array
* @param array $phpdocLines
* @param int $indent
* @param callable $body
* @param string $returnType
*
* @return string
*/
private function staticMethod(string $name, array $parameters, array $types, array $defaults, array $phpdocLines, int $indent, callable $body): string
private function staticMethod(string $name, array $parameters, array $types, array $defaults, array $phpdocLines, int $indent, callable $body, string $returnType): string
{
$indentation = str_repeat(' ', $indent);

$staticFunction = $this->phpdoc($phpdocLines, $indent)."\n";
$staticFunction .= $indentation.'public static function '.$name.$this->functionParameters($parameters, $types, $defaults).": void\n"
$staticFunction .= $indentation.'public static function '.$name.$this->functionParameters($parameters, $types, $defaults).": {$returnType}\n"
.$indentation."{\n";

$firstParameter = '$'.array_shift($parameters);
Expand Down
Loading