Skip to content

Commit

Permalink
Add PsalmApi::$methods->getStorage and PsalmApi::$properties->getStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
klimick committed Dec 25, 2022
1 parent 12806b2 commit 20cf3c9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Toolkit/Methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Fp\PsalmToolkit\Toolkit;

use Fp\Functional\Option\Option;
use Psalm\Internal\MethodIdentifier;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\MethodStorage;
use Psalm\Type\Atomic\TNamedObject;

use function Fp\Collection\at;

final class Methods
{
/**
* @return Option<MethodStorage>
*/
public function getStorage(string|TNamedObject|ClassLikeStorage $object, string $method): Option
{
$storage = !($object instanceof ClassLikeStorage)
? PsalmApi::$classlikes->getStorage($object)
: Option::some($object);

return $storage->flatMap(fn(ClassLikeStorage $s) => at($s->methods, strtolower($method))->orElse(
fn() => self::getDeclaringStorage($s, strtolower($method)),
));
}

/**
* @return Option<MethodStorage>
*/
private function getDeclaringStorage(ClassLikeStorage $storage, string $method): Option
{
return at($storage->declaring_method_ids, strtolower($method))->flatMap(
fn(MethodIdentifier $id) => Option::try(fn() => PsalmApi::$codebase->methods->getStorage($id)),
);
}
}
2 changes: 2 additions & 0 deletions src/Toolkit/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement
PsalmApi::$classlikes = new Classlikes();
PsalmApi::$codebase = ProjectAnalyzer::getInstance()->getCodebase();
PsalmApi::$issue = new Issue();
PsalmApi::$methods = new Methods();
PsalmApi::$properties = new Properties();

$register = function(string $hook) use ($registration): void {
if (class_exists($hook)) {
Expand Down
39 changes: 39 additions & 0 deletions src/Toolkit/Properties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Fp\PsalmToolkit\Toolkit;

use Fp\Functional\Option\Option;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\PropertyStorage;
use Psalm\Type\Atomic\TNamedObject;

use function Fp\Collection\at;

final class Properties
{
/**
* @return Option<PropertyStorage>
*/
public function getStorage(string|TNamedObject|ClassLikeStorage $object, string $property): Option
{
$storage = !($object instanceof ClassLikeStorage)
? PsalmApi::$classlikes->getStorage($object)
: Option::some($object);

return $storage->flatMap(fn(ClassLikeStorage $s) => at($s->properties, $property)->orElse(
fn() => self::getAppearingStorage($s, $property),
));
}

/**
* @return Option<PropertyStorage>
*/
private function getAppearingStorage(ClassLikeStorage $storage, string $property): Option
{
return at($storage->appearing_property_ids, $property)->flatMap(
fn(string $propertyId) => Option::try(fn() => PsalmApi::$codebase->properties->getStorage($propertyId)),
);
}
}
2 changes: 2 additions & 0 deletions src/Toolkit/PsalmApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ final class PsalmApi
public static Codebase $codebase;
public static Classlikes $classlikes;
public static Issue $issue;
public static Methods $methods;
public static Properties $properties;
}

0 comments on commit 20cf3c9

Please sign in to comment.