generated from php-standard-library/repository-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
59
src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters