Skip to content

Commit

Permalink
Dropped legacy plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jan 3, 2022
1 parent 0dba2a6 commit 2e24a16
Show file tree
Hide file tree
Showing 39 changed files with 56 additions and 1,589 deletions.
12 changes: 0 additions & 12 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@
<directory name="tests" />
</errorLevel>
</DeprecatedMethod>
<DeprecatedClass>
<errorLevel type="suppress">
<referencedClass name="Psalm\Plugin\Hook\*" />
</errorLevel>
</DeprecatedClass>

<DeprecatedInterface>
<errorLevel type="suppress">
<referencedClass name="Psalm\Plugin\Hook\*" />
</errorLevel>
</DeprecatedInterface>

<UnusedParam>
<errorLevel type="suppress">
Expand All @@ -92,7 +81,6 @@
<directory name="src/Psalm/Internal/Fork" />
<directory name="src/Psalm/Node" />
<file name="src/Psalm/Plugin/Shepherd.php" />
<file name="src/Psalm/Plugin/Hook/MethodReturnTypeProviderInterface.php"/>
</errorLevel>
</UnusedClass>

Expand Down
264 changes: 16 additions & 248 deletions src/Psalm/Internal/EventDispatcher.php

Large diffs are not rendered by default.

47 changes: 2 additions & 45 deletions src/Psalm/Internal/Provider/FunctionExistenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Psalm\Plugin\EventHandler\Event\FunctionExistenceProviderEvent;
use Psalm\Plugin\EventHandler\FunctionExistenceProviderInterface;
use Psalm\Plugin\Hook\FunctionExistenceProviderInterface as LegacyFunctionExistenceProviderInterface;
use Psalm\StatementsSource;

use function is_subclass_of;
Expand All @@ -24,35 +23,17 @@ class FunctionExistenceProvider
*/
private static $handlers = [];

/**
* @var array<
* lowercase-string,
* array<Closure(
* StatementsSource,
* string
* ): ?bool>
* >
*/
private static $legacy_handlers = [];

public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
}

/**
* @param class-string $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyFunctionExistenceProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'doesFunctionExist']);

foreach ($class::getFunctionIds() as $function_id) {
$this->registerLegacyClosure($function_id, $callable);
}
} elseif (is_subclass_of($class, FunctionExistenceProviderInterface::class, true)) {
if (is_subclass_of($class, FunctionExistenceProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'doesFunctionExist']);

foreach ($class::getFunctionIds() as $function_id) {
Expand All @@ -70,39 +51,15 @@ public function registerClosure(string $function_id, Closure $c): void
self::$handlers[$function_id][] = $c;
}

/**
* @param lowercase-string $function_id
* @param Closure(
* StatementsSource,
* string
* ): ?bool $c
*/
public function registerLegacyClosure(string $function_id, Closure $c): void
{
self::$legacy_handlers[$function_id][] = $c;
}

public function has(string $function_id): bool
{
return isset(self::$handlers[strtolower($function_id)]) ||
isset(self::$legacy_handlers[strtolower($function_id)]);
return isset(self::$handlers[strtolower($function_id)]);
}

public function doesFunctionExist(
StatementsSource $statements_source,
string $function_id
): ?bool {
foreach (self::$legacy_handlers[strtolower($function_id)] ?? [] as $function_handler) {
$function_exists = $function_handler(
$statements_source,
$function_id
);

if ($function_exists !== null) {
return $function_exists;
}
}

foreach (self::$handlers[strtolower($function_id)] ?? [] as $function_handler) {
$event = new FunctionExistenceProviderEvent(
$statements_source,
Expand Down
64 changes: 5 additions & 59 deletions src/Psalm/Internal/Provider/FunctionParamsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use Psalm\Context;
use Psalm\Plugin\EventHandler\Event\FunctionParamsProviderEvent;
use Psalm\Plugin\EventHandler\FunctionParamsProviderInterface;
use Psalm\Plugin\Hook\FunctionParamsProviderInterface as LegacyFunctionParamsProviderInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FunctionLikeParameter;

use function is_subclass_of;
use function strtolower;

/**
Expand All @@ -28,43 +26,20 @@ class FunctionParamsProvider
*/
private static $handlers = [];

/**
* @var array<
* lowercase-string,
* array<Closure(
* StatementsSource,
* string,
* list<Arg>,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter>>
* >
*/
private static $legacy_handlers = [];

public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
}

/**
* @param class-string<LegacyFunctionParamsProviderInterface>|class-string<FunctionParamsProviderInterface> $class
* @param class-string<FunctionParamsProviderInterface> $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyFunctionParamsProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'getFunctionParams']);
$callable = Closure::fromCallable([$class, 'getFunctionParams']);

foreach ($class::getFunctionIds() as $function_id) {
$this->registerLegacyClosure($function_id, $callable);
}
} elseif (is_subclass_of($class, FunctionParamsProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'getFunctionParams']);

foreach ($class::getFunctionIds() as $function_id) {
$this->registerClosure($function_id, $callable);
}
foreach ($class::getFunctionIds() as $function_id) {
$this->registerClosure($function_id, $callable);
}
}

Expand All @@ -76,24 +51,9 @@ public function registerClosure(string $fq_classlike_name, Closure $c): void
self::$handlers[strtolower($fq_classlike_name)][] = $c;
}

/**
* @param Closure(
* StatementsSource,
* string,
* list<Arg>,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter> $c
*/
public function registerLegacyClosure(string $fq_classlike_name, Closure $c): void
{
self::$legacy_handlers[strtolower($fq_classlike_name)][] = $c;
}

public function has(string $fq_classlike_name): bool
{
return isset(self::$handlers[strtolower($fq_classlike_name)]) ||
isset(self::$legacy_handlers[strtolower($fq_classlike_name)]);
return isset(self::$handlers[strtolower($fq_classlike_name)]);
}

/**
Expand Down Expand Up @@ -123,20 +83,6 @@ public function getFunctionParams(
}
}

foreach (self::$legacy_handlers[strtolower($function_id)] ?? [] as $class_handler) {
$result = $class_handler(
$statements_source,
$function_id,
$call_args,
$context,
$code_location
);

if ($result) {
return $result;
}
}

return null;
}
}
57 changes: 2 additions & 55 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use PhpParser;
use PhpParser\Node\Arg;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayChunkReturnTypeProvider;
Expand Down Expand Up @@ -41,7 +40,6 @@
use Psalm\Internal\Provider\ReturnTypeProvider\VersionCompareReturnTypeProvider;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface as LegacyFunctionReturnTypeProviderInterface;
use Psalm\StatementsSource;
use Psalm\Type\Union;

Expand All @@ -61,24 +59,9 @@ class FunctionReturnTypeProvider
*/
private static $handlers = [];

/**
* @var array<
* lowercase-string,
* array<Closure(
* StatementsSource,
* non-empty-string,
* list<Arg>,
* Context,
* CodeLocation
* ): ?Union>
* >
*/
private static $legacy_handlers = [];

public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];

$this->registerClass(ArrayChunkReturnTypeProvider::class);
$this->registerClass(ArrayColumnReturnTypeProvider::class);
Expand Down Expand Up @@ -119,13 +102,7 @@ public function __construct()
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyFunctionReturnTypeProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'getFunctionReturnType']);

foreach ($class::getFunctionIds() as $function_id) {
$this->registerLegacyClosure($function_id, $callable);
}
} elseif (is_subclass_of($class, FunctionReturnTypeProviderInterface::class, true)) {
if (is_subclass_of($class, FunctionReturnTypeProviderInterface::class, true)) {
$callable = Closure::fromCallable([$class, 'getFunctionReturnType']);

foreach ($class::getFunctionIds() as $function_id) {
Expand All @@ -143,25 +120,9 @@ public function registerClosure(string $function_id, Closure $c): void
self::$handlers[$function_id][] = $c;
}

/**
* @param lowercase-string $function_id
* @param Closure(
* StatementsSource,
* non-empty-string,
* list<Arg>,
* Context,
* CodeLocation
* ): ?Union $c
*/
public function registerLegacyClosure(string $function_id, Closure $c): void
{
self::$legacy_handlers[$function_id][] = $c;
}

public function has(string $function_id): bool
{
return isset(self::$handlers[strtolower($function_id)]) ||
isset(self::$legacy_handlers[strtolower($function_id)]);
return isset(self::$handlers[strtolower($function_id)]);
}

/**
Expand All @@ -174,20 +135,6 @@ public function getReturnType(
Context $context,
CodeLocation $code_location
): ?Union {
foreach (self::$legacy_handlers[strtolower($function_id)] ?? [] as $function_handler) {
$return_type = $function_handler(
$statements_source,
$function_id,
$stmt->getArgs(),
$context,
$code_location
);

if ($return_type) {
return $return_type;
}
}

foreach (self::$handlers[strtolower($function_id)] ?? [] as $function_handler) {
$event = new FunctionReturnTypeProviderEvent(
$statements_source,
Expand Down

0 comments on commit 2e24a16

Please sign in to comment.