Psalm
Get started
Documentation
Paid support
Plugins
Blog
GitHub
<?php use ReflectionException; use ReflectionMethod; use RuntimeException; use function get_class; use function sprintf; final class MethodExtractor { /** @return mixed */ public function extract(object $source, string $data) { try { $reflectionMethod = new ReflectionMethod(get_class($source), $data); } catch (ReflectionException $e) { throw new RuntimeException(sprintf('Method "%s" does not exist.', $data)); } if (!$reflectionMethod->isPublic()) { throw new RuntimeException(sprintf('Method "%s" is not public.', $data)); } if ($reflectionMethod->isStatic()) { return $source::$data(); } return $source->$data(); } }
Snippet created on February 20 2021 at 20:52 UTC
Settings
Get link