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
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Plugin\Hook\MethodExistenceProviderInterface;
|
|
|
|
use Psalm\Plugin\Hook\MethodParamsProviderInterface;
|
|
|
|
use Psalm\Plugin\Hook\MethodReturnTypeProviderInterface;
|
2019-03-01 14:57:10 +01:00
|
|
|
use Psalm\StatementsSource;
|
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'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function doesMethodExist(
|
|
|
|
string $fq_classlike_name,
|
2020-08-10 05:19:59 +02:00
|
|
|
string $method_name_lowercase,
|
2020-09-07 01:36:47 +02:00
|
|
|
?StatementsSource $source = null,
|
|
|
|
?CodeLocation $code_location = null
|
2020-09-13 22:39:06 +02:00
|
|
|
): ?bool {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-03-23 19:27:54 +01:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
* @return ?array<int, \Psalm\Storage\FunctionLikeParameter>
|
|
|
|
*/
|
|
|
|
public static function getMethodParams(
|
|
|
|
string $fq_classlike_name,
|
|
|
|
string $method_name_lowercase,
|
2020-09-07 01:36:47 +02:00
|
|
|
?array $call_args = null,
|
|
|
|
?StatementsSource $statements_source = null,
|
|
|
|
?Context $context = null,
|
|
|
|
?CodeLocation $code_location = null
|
2020-09-13 22:39:06 +02:00
|
|
|
): ?array {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-03-23 19:27:54 +01:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
*/
|
|
|
|
public static function getMethodReturnType(
|
|
|
|
StatementsSource $source,
|
|
|
|
string $fq_classlike_name,
|
2020-08-10 05:19:59 +02:00
|
|
|
string $method_name_lowercase,
|
2019-03-01 14:57:10 +01:00
|
|
|
array $call_args,
|
|
|
|
Context $context,
|
2019-03-07 20:56:18 +01:00
|
|
|
CodeLocation $code_location,
|
2020-09-07 01:36:47 +02:00
|
|
|
?array $template_type_parameters = null,
|
|
|
|
?string $called_fq_classlike_name = null,
|
|
|
|
?string $called_method_name_lowercase = null
|
2020-09-12 17:24:05 +02:00
|
|
|
): ?Type\Union {
|
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
|
|
|
}
|
|
|
|
}
|