From 20cf3c9b61584fadc7a1abe331e9141512b0821b Mon Sep 17 00:00:00 2001 From: adrew Date: Sun, 25 Dec 2022 11:07:41 +0300 Subject: [PATCH] Add PsalmApi::$methods->getStorage and PsalmApi::$properties->getStorage --- src/Toolkit/Methods.php | 40 ++++++++++++++++++++++++++++++++++++++ src/Toolkit/Plugin.php | 2 ++ src/Toolkit/Properties.php | 39 +++++++++++++++++++++++++++++++++++++ src/Toolkit/PsalmApi.php | 2 ++ 4 files changed, 83 insertions(+) create mode 100644 src/Toolkit/Methods.php create mode 100644 src/Toolkit/Properties.php diff --git a/src/Toolkit/Methods.php b/src/Toolkit/Methods.php new file mode 100644 index 0000000..8f8d15f --- /dev/null +++ b/src/Toolkit/Methods.php @@ -0,0 +1,40 @@ + + */ + 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 + */ + 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)), + ); + } +} diff --git a/src/Toolkit/Plugin.php b/src/Toolkit/Plugin.php index 2a40819..0da0cc2 100644 --- a/src/Toolkit/Plugin.php +++ b/src/Toolkit/Plugin.php @@ -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)) { diff --git a/src/Toolkit/Properties.php b/src/Toolkit/Properties.php new file mode 100644 index 0000000..6f58345 --- /dev/null +++ b/src/Toolkit/Properties.php @@ -0,0 +1,39 @@ + + */ + 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 + */ + 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)), + ); + } +} diff --git a/src/Toolkit/PsalmApi.php b/src/Toolkit/PsalmApi.php index 73e8f79..9b66187 100644 --- a/src/Toolkit/PsalmApi.php +++ b/src/Toolkit/PsalmApi.php @@ -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; }