2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
2019-05-10 00:58:30 +02:00
|
|
|
namespace Psalm\Test\Config\Plugin\Hook;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\MethodExistenceProviderEvent;
|
|
|
|
use Psalm\Plugin\EventHandler\Event\MethodParamsProviderEvent;
|
|
|
|
use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Plugin\EventHandler\MethodExistenceProviderInterface;
|
|
|
|
use Psalm\Plugin\EventHandler\MethodParamsProviderInterface;
|
|
|
|
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Type;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class FooMethodProvider implements
|
|
|
|
MethodExistenceProviderInterface,
|
|
|
|
MethodParamsProviderInterface,
|
|
|
|
MethodReturnTypeProviderInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getClassLikeNames() : array
|
|
|
|
{
|
|
|
|
return ['Ns\Foo'];
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
public static function doesMethodExist(MethodExistenceProviderEvent $event): ?bool
|
|
|
|
{
|
|
|
|
$method_name_lowercase = $event->getMethodNameLowercase();
|
2020-09-01 00:56:21 +02:00
|
|
|
if ($method_name_lowercase === 'magicmethod' || $method_name_lowercase === 'magicmethod2') {
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ?array<int, \Psalm\Storage\FunctionLikeParameter>
|
|
|
|
*/
|
2021-01-06 15:05:53 +01:00
|
|
|
public static function getMethodParams(MethodParamsProviderEvent $event): ?array
|
|
|
|
{
|
|
|
|
$method_name_lowercase = $event->getMethodNameLowercase();
|
2020-09-01 00:56:21 +02:00
|
|
|
if ($method_name_lowercase === 'magicmethod' || $method_name_lowercase === 'magicmethod2') {
|
|
|
|
return [new \Psalm\Storage\FunctionLikeParameter('first', false, Type::getString())];
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
|
|
|
|
{
|
|
|
|
$method_name_lowercase = $event->getMethodNameLowercase();
|
2020-09-20 00:26:51 +02:00
|
|
|
if ($method_name_lowercase === 'magicmethod') {
|
2020-03-23 18:24:36 +01:00
|
|
|
return Type::getString();
|
|
|
|
} else {
|
|
|
|
return new \Psalm\Type\Union([new \Psalm\Type\Atomic\TNamedObject('NS\\Foo2')]);
|
|
|
|
}
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
}
|