Skip to content

Commit

Permalink
fix multiple return type bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Apr 11, 2021
1 parent 3c577ca commit adab2a0
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php
Expand Up @@ -19,7 +19,7 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
public static function getFunctionIds(): array
{
return [
'psl\str\count',
'psl\iter\count',
];
}

Expand Down Expand Up @@ -56,7 +56,22 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

// array{foo: bar} -> literal-int(1)
if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
return Type::getInt(false, count($array_argument_type->properties));
// Psalm allows extra properties in keyed arrays, so we can't return a literal integer
// for this.
//
// return Type::getInt(false, count($array_argument_type->properties));

if (count($array_argument_type->properties) >= 1) {
return Type::getPositiveInt();
}

return Type::getInt();
}

if ($array_argument_type instanceof Type\Atomic\TArray) {
if ($array_argument_type->type_params[0]->isEmpty() && $array_argument_type->type_params[1]->isEmpty()) {
return Type::getInt(false, 0);
}
}

return Type::getInt();
Expand Down
55 changes: 55 additions & 0 deletions src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Psl\Psalm\EventHandler\Iter\FirstKey;

use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psl\Psalm\Argument;

final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return non-empty-list<lowercase-string>
*/
public static function getFunctionIds(): array
{
return [
'psl\iter\first_key',
];
}

public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union
{
$argument_type = Argument::getType($event->getCallArgs(), $event->getStatementsSource(), 0);
if (null === $argument_type) {
return null;
}

$array_argument_type = $argument_type->getAtomicTypes()['array'] ?? null;
if (null === $array_argument_type) {
return null;
}

if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) {
return clone $array_argument_type->type_params[0];
}

if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
return Type::getInt();
}

if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
//
// foreach ($properties as $property) {
// return clone $property;
// }
return clone $array_argument_type->getGenericKeyType();
}

return null;
}
}
59 changes: 59 additions & 0 deletions src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Psl\Psalm\EventHandler\Iter\LastKey;

use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psl\Psalm\Argument;

final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return non-empty-list<lowercase-string>
*/
public static function getFunctionIds(): array
{
return [
'psl\iter\last_key',
];
}

public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union
{
$argument_type = Argument::getType($event->getCallArgs(), $event->getStatementsSource(), 0);
if (null === $argument_type) {
return null;
}

$array_argument_type = $argument_type->getAtomicTypes()['array'] ?? null;
if (null === $array_argument_type) {
return null;
}

if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) {
return clone $array_argument_type->type_params[0];
}

if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
return Type::getInt();
}

if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
//
// $properties = $array_argument_type->properties;
// $last_property = null;
// foreach ($properties as $property) {
// $last_property = $property;
// }
//
// return clone $last_property;
return clone $array_argument_type->getGenericKeyType();
}

return null;
}
}
6 changes: 3 additions & 3 deletions src/EventHandler/Str/Repeat/FunctionReturnTypeProvider.php
Expand Up @@ -26,7 +26,7 @@ public static function getFunctionIds(): array
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union
{
$argument_type = Argument::getType($event->getCallArgs(), $event->getStatementsSource(), 0);
if ($argument_type === null || !$argument_type->hasLowercaseString()) {
if ($argument_type === null) {
return Type::getString();
}

Expand All @@ -43,12 +43,12 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
return new Type\Union([new Type\Atomic\TLowercaseString()]);
}

if ($string_argument_type instanceof Type\Atomic\TLiteralString) {
if ($argument_type->hasLiteralString()) {
$multiplier_argument_type = Argument::getType($event->getCallArgs(), $event->getStatementsSource(), 1);
if (null !== $multiplier_argument_type && $multiplier_argument_type->hasLiteralInt()) {
/** @psalm-suppress MissingThrowsDocblock */
return Type::getString(str_repeat(
$string_argument_type->value,
$argument_type->getSingleStringLiteral()->value,
$multiplier_argument_type->getSingleIntLiteral()->value
));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin.php
Expand Up @@ -31,7 +31,9 @@ private function getHooks(): iterable
{
// Psl\Iter hooks
yield EventHandler\Iter\First\FunctionReturnTypeProvider::class;
yield EventHandler\Iter\FirstKey\FunctionReturnTypeProvider::class;
yield EventHandler\Iter\Last\FunctionReturnTypeProvider::class;
yield EventHandler\Iter\LastKey\FunctionReturnTypeProvider::class;
yield EventHandler\Iter\Count\FunctionReturnTypeProvider::class;

// Psl\Regex hooks
Expand Down

0 comments on commit adab2a0

Please sign in to comment.