From 9a8b3da2aed1ee43af352046dce710f80046f23f Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 3 Mar 2022 21:39:27 +0100 Subject: [PATCH] Simplify PHP class imports --- lib/AbstractPicoPlugin.php | 22 ++++++------- lib/Pico.php | 64 ++++++++++++++++++------------------- lib/PicoPluginInterface.php | 2 +- lib/PicoTwigExtension.php | 24 +++++++------- plugins/DummyPlugin.php | 5 +-- 5 files changed, 59 insertions(+), 58 deletions(-) diff --git a/lib/AbstractPicoPlugin.php b/lib/AbstractPicoPlugin.php index 195d63b..250d757 100644 --- a/lib/AbstractPicoPlugin.php +++ b/lib/AbstractPicoPlugin.php @@ -124,7 +124,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface // otherwise the plugin needs to be enabled manually try { $this->setEnabled(true, false, true); - } catch (\RuntimeException $e) { + } catch (RuntimeException $e) { $this->enabled = false; } } @@ -226,15 +226,15 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface * * @param bool $recursive enable required plugins automatically * - * @throws \RuntimeException thrown when a dependency fails + * @throws RuntimeException thrown when a dependency fails */ protected function checkDependencies($recursive) { foreach ($this->getDependencies() as $pluginName) { try { $plugin = $this->getPico()->getPlugin($pluginName); - } catch (\RuntimeException $e) { - throw new \RuntimeException( + } catch (RuntimeException $e) { + throw new RuntimeException( "Unable to enable plugin '" . get_called_class() . "': " . "Required plugin '" . $pluginName . "' not found" ); @@ -246,13 +246,13 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface if (!$plugin->isStatusChanged()) { $plugin->setEnabled(true, true, true); } else { - throw new \RuntimeException( + throw new RuntimeException( "Unable to enable plugin '" . get_called_class() . "': " . "Required plugin '" . $pluginName . "' was disabled manually" ); } } else { - throw new \RuntimeException( + throw new RuntimeException( "Unable to enable plugin '" . get_called_class() . "': " . "Required plugin '" . $pluginName . "' is disabled" ); @@ -276,7 +276,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface * * @param bool $recursive disabled dependant plugins automatically * - * @throws \RuntimeException thrown when a dependency fails + * @throws RuntimeException thrown when a dependency fails */ protected function checkDependants($recursive) { @@ -288,7 +288,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface if (!$plugin->isStatusChanged()) { $plugin->setEnabled(false, true, true); } else { - throw new \RuntimeException( + throw new RuntimeException( "Unable to disable plugin '" . get_called_class() . "': " . "Required by manually enabled plugin '" . $pluginName . "'" ); @@ -298,7 +298,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface } else { $dependantsList = 'plugin' . ((count($dependants) > 1) ? 's' : '') . ' ' . "'" . implode("', '", array_keys($dependants)) . "'"; - throw new \RuntimeException( + throw new RuntimeException( "Unable to disable plugin '" . get_called_class() . "': " . "Required by " . $dependantsList ); @@ -337,7 +337,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface * newer API versions, what requires some special (version specific) * precaution and is therefore usually not the case. * - * @throws \RuntimeException thrown when the plugin's and Pico's API aren't + * @throws RuntimeException thrown when the plugin's and Pico's API aren't * compatible */ protected function checkCompatibility() @@ -350,7 +350,7 @@ abstract class AbstractPicoPlugin implements PicoPluginInterface $this->nativePlugin = ($pluginApiVersion === $picoApiVersion); if (!$this->nativePlugin && ($pluginApiVersion > $picoApiVersion)) { - throw new \RuntimeException( + throw new RuntimeException( "Unable to enable plugin '" . get_called_class() . "': The plugin's API (version " . $pluginApiVersion . ") isn't compatible with Pico's API (version " . $picoApiVersion . ")" ); diff --git a/lib/Pico.php b/lib/Pico.php index 7211be5..e2f7eec 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -18,6 +18,7 @@ declare(strict_types=1); +use Symfony\Component\Yaml\Exception\ParseException as YamlParseException; use Symfony\Component\Yaml\Parser as YamlParser; use Twig\Environment as TwigEnvironment; use Twig\Extension\DebugExtension as TwigDebugExtension; @@ -260,7 +261,7 @@ class Pico * Parsedown Extra instance used for markdown parsing * * @see Pico::getParsedown() - * @var \Parsedown|null + * @var Parsedown|null */ protected $parsedown; @@ -422,13 +423,13 @@ class Pico * * @return string rendered Pico contents * - * @throws \Exception thrown when a irrecoverable error occurs + * @throws Exception thrown when a irrecoverable error occurs */ public function run(): string { // check lock if ($this->locked) { - throw new \LogicException('You cannot run the same Pico instance multiple times'); + throw new LogicException('You cannot run the same Pico instance multiple times'); } // lock Pico @@ -445,7 +446,7 @@ class Pico // check content dir if (!is_dir($this->getConfig('content_dir'))) { - throw new \RuntimeException('Invalid content directory "' . $this->getConfig('content_dir') . '"'); + throw new RuntimeException('Invalid content directory "' . $this->getConfig('content_dir') . '"'); } // load theme @@ -553,7 +554,7 @@ class Pico * @see Pico::getPlugin() * @see Pico::getPlugins() * - * @throws \RuntimeException thrown when a plugin couldn't be loaded + * @throws RuntimeException thrown when a plugin couldn't be loaded */ protected function loadPlugins(): void { @@ -564,7 +565,7 @@ class Pico } if (!isset($this->plugins['PicoDeprecated']) && (count($this->plugins) !== count($this->nativePlugins))) { - throw new \RuntimeException( + throw new RuntimeException( "Plugins using an older API than version " . static::API_VERSION . " found, " . "but PicoDeprecated isn't loaded" ); @@ -585,7 +586,7 @@ class Pico * * @return string[] installer names of the loaded plugins * - * @throws \RuntimeException thrown when a plugin couldn't be loaded + * @throws RuntimeException thrown when a plugin couldn't be loaded */ protected function loadComposerPlugins(array $pluginBlacklist = []): array { @@ -613,7 +614,7 @@ class Pico } if (!($plugin instanceof PicoPluginInterface)) { - throw new \RuntimeException( + throw new RuntimeException( "Unable to load plugin '" . $className . "' via 'vendor/pico-plugin.php': " . "Plugins installed by composer must implement 'PicoPluginInterface'" ); @@ -653,7 +654,7 @@ class Pico * * @param string[] $pluginBlacklist class names of plugins not to load * - * @throws \RuntimeException thrown when a plugin couldn't be loaded + * @throws RuntimeException thrown when a plugin couldn't be loaded */ protected function loadLocalPlugins(array $pluginBlacklist = []): void { @@ -679,7 +680,7 @@ class Pico $pluginFile = $file . '/' . $className . '.php'; if (!is_file($this->getPluginsDir() . $pluginFile)) { - throw new \RuntimeException( + throw new RuntimeException( "Unable to load plugin '" . $className . "' from '" . $pluginFile . "': File not found" ); } @@ -687,7 +688,7 @@ class Pico $className = preg_replace('/^[0-9]+-/', '', substr($file, 0, -4)); $pluginFile = $file; } else { - throw new \RuntimeException("Unable to load plugin from '" . $file . "': Not a valid plugin file"); + throw new RuntimeException("Unable to load plugin from '" . $file . "': Not a valid plugin file"); } if (isset($this->plugins[$className]) || isset($pluginBlacklist[$className])) { @@ -709,7 +710,7 @@ class Pico } } } else { - throw new \RuntimeException( + throw new RuntimeException( "Unable to load plugin '" . $className . "' from '" . $pluginFile . "': Plugin class not found" ); } @@ -746,7 +747,7 @@ class Pico * * @return PicoPluginInterface instance of the loaded plugin * - * @throws \RuntimeException thrown when the plugin couldn't be loaded + * @throws RuntimeException thrown when the plugin couldn't be loaded */ public function loadPlugin($plugin): PicoPluginInterface { @@ -755,14 +756,14 @@ class Pico if (class_exists($className)) { $plugin = new $className($this); } else { - throw new \RuntimeException("Unable to load plugin '" . $className . "': Class not found"); + throw new RuntimeException("Unable to load plugin '" . $className . "': Class not found"); } } $className = get_class($plugin); if (!($plugin instanceof PicoPluginInterface)) { - throw new \RuntimeException( + throw new RuntimeException( "Unable to load plugin '" . $className . "': " . "Manually loaded plugins must implement 'PicoPluginInterface'" ); @@ -878,7 +879,7 @@ class Pico * * @return object instance of the plugin * - * @throws \RuntimeException thrown when the plugin wasn't found + * @throws RuntimeException thrown when the plugin wasn't found */ public function getPlugin(string $pluginName): object { @@ -886,7 +887,7 @@ class Pico return $this->plugins[$pluginName]; } - throw new \RuntimeException("Missing plugin '" . $pluginName . "'"); + throw new RuntimeException("Missing plugin '" . $pluginName . "'"); } /** @@ -1051,12 +1052,12 @@ class Pico * * @param array $config array with config variables * - * @throws \LogicException thrown if Pico already started processing + * @throws LogicException thrown if Pico already started processing */ public function setConfig(array $config): void { if ($this->locked) { - throw new \LogicException("You cannot modify Pico's config after processing has started"); + throw new LogicException("You cannot modify Pico's config after processing has started"); } $this->config = $config; @@ -1096,7 +1097,7 @@ class Pico protected function loadTheme(): void { if (!is_dir($this->getThemesDir() . $this->getTheme())) { - throw new \RuntimeException( + throw new RuntimeException( 'Couldn\'t load theme "' . $this->theme . '": No such theme directory' ); } @@ -1163,7 +1164,7 @@ class Pico // check for theme compatibility if (!isset($this->plugins['PicoDeprecated']) && ($this->themeApiVersion < static::API_VERSION)) { - throw new \RuntimeException( + throw new RuntimeException( 'Current theme "' . $this->theme . '" uses API version ' . $this->themeApiVersion . ', but Pico ' . 'provides API version ' . static::API_VERSION . ' and PicoDeprecated isn\'t loaded' ); @@ -1490,8 +1491,7 @@ class Pico * * @return array parsed meta data * - * @throws \Symfony\Component\Yaml\Exception\ParseException thrown when the - * meta data is invalid + * @throws YamlParseException thrown when the meta data is invalid */ public function parseFileMeta(string $rawContent, array $headers): array { @@ -1576,15 +1576,15 @@ class Pico * This method triggers the `onParsedownRegistered` event when the * Parsedown markdown parser wasn't initiated yet. * - * @return \Parsedown Parsedown markdown parser + * @return Parsedown Parsedown markdown parser */ - public function getParsedown(): \Parsedown + public function getParsedown(): Parsedown { if ($this->parsedown === null) { if ($this->config['content_config']['extra']) { - $this->parsedown = new \ParsedownExtra(); + $this->parsedown = new ParsedownExtra(); } else { - $this->parsedown = new \Parsedown(); + $this->parsedown = new Parsedown(); } $this->parsedown->setBreaksEnabled((bool) $this->config['content_config']['breaks']); @@ -1797,7 +1797,7 @@ class Pico $headers = $this->getMetaHeaders(); try { $meta = $this->parseFileMeta($rawContent, $headers); - } catch (\Symfony\Component\Yaml\Exception\ParseException $e) { + } catch (YamlParseException $e) { $meta = $this->parseFileMeta('', $headers); $meta['YAML_ParseError'] = $e->getMessage(); } @@ -2334,14 +2334,14 @@ class Pico * * @return string URL * - * @throws \InvalidArgumentException thrown when invalid arguments got passed + * @throws InvalidArgumentException thrown when invalid arguments got passed */ public function getPageUrl(string $page, $queryData = null, bool $dropIndex = true): string { if (is_array($queryData)) { $queryData = http_build_query($queryData, '', '&'); } elseif (($queryData !== null) && !is_string($queryData)) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( 'Argument 2 passed to ' . __METHOD__ . ' must be of the type array or string, ' . (is_object($queryData) ? get_class($queryData) : gettype($queryData)) . ' given' ); @@ -2735,7 +2735,7 @@ class Pico * * @return string normalized path * - * @throws \UnexpectedValueException thrown when a absolute path is passed + * @throws UnexpectedValueException thrown when a absolute path is passed * although absolute paths aren't allowed */ public function getNormalizedPath(string $path, bool $allowAbsolutePath = false, bool $endSlash = true): string @@ -2754,7 +2754,7 @@ class Pico } if ($absolutePath && !$allowAbsolutePath) { - throw new \UnexpectedValueException( + throw new UnexpectedValueException( 'Argument 1 passed to ' . __METHOD__ . ' must be a relative path, absolute path "' . $path . '" given' ); } diff --git a/lib/PicoPluginInterface.php b/lib/PicoPluginInterface.php index 4825fd5..ba21eb5 100644 --- a/lib/PicoPluginInterface.php +++ b/lib/PicoPluginInterface.php @@ -56,7 +56,7 @@ interface PicoPluginInterface * @param bool $auto enable or disable to fulfill a dependency. This * parameter is optional and defaults to FALSE. * - * @throws \RuntimeException thrown when a dependency fails + * @throws RuntimeException thrown when a dependency fails */ public function setEnabled($enabled, $recursive = true, $auto = false); diff --git a/lib/PicoTwigExtension.php b/lib/PicoTwigExtension.php index 960d7b6..f85dc86 100644 --- a/lib/PicoTwigExtension.php +++ b/lib/PicoTwigExtension.php @@ -133,8 +133,8 @@ class PicoTwigExtension extends AbstractTwigExtension * This method is registered as the Twig `map` filter. You can use this * filter to e.g. get all page titles (`{{ pages|map("title") }}`). * - * @param array|\Traversable $var variable to map - * @param mixed $mapKeyPath key to map; either a scalar or a + * @param array|Traversable $var variable to map + * @param mixed $mapKeyPath key to map; either a scalar or a * array interpreted as key path (i.e. ['foo', 'bar'] will return all * $item['foo']['bar'] values) * @@ -144,7 +144,7 @@ class PicoTwigExtension extends AbstractTwigExtension */ public function mapFilter($var, $mapKeyPath): array { - if (!is_array($var) && (!is_object($var) || !($var instanceof \Traversable))) { + if (!is_array($var) && (!is_object($var) || !($var instanceof Traversable))) { throw new TwigRuntimeError(sprintf( 'The map filter only works with arrays or "Traversable", got "%s"', is_object($var) ? get_class($var) : gettype($var) @@ -170,11 +170,11 @@ class PicoTwigExtension extends AbstractTwigExtension * always sorted in ascending order, apply Twigs `reverse` filter to * achieve a descending order. * - * @param array|\Traversable $var variable to sort - * @param mixed $sortKeyPath key to use for sorting; either + * @param array|Traversable $var variable to sort + * @param mixed $sortKeyPath key to use for sorting; either * a scalar or a array interpreted as key path (i.e. ['foo', 'bar'] * will sort $var by $item['foo']['bar']) - * @param string $fallback specify what to do with items + * @param string $fallback specify what to do with items * which don't contain the specified sort key; use "bottom" (default) * to move these items to the end of the sorted array, "top" to rank * them first, "keep" to keep the original order, or "remove" to remove @@ -186,7 +186,7 @@ class PicoTwigExtension extends AbstractTwigExtension */ public function sortByFilter($var, $sortKeyPath, string $fallback = 'bottom'): array { - if (is_object($var) && ($var instanceof \Traversable)) { + if (is_object($var) && ($var instanceof Traversable)) { $var = iterator_to_array($var, true); } elseif (!is_array($var)) { throw new TwigRuntimeError(sprintf( @@ -247,8 +247,8 @@ class PicoTwigExtension extends AbstractTwigExtension * Returns the value of a variable item specified by a scalar key or a * arbitrary deep sub-key using a key path * - * @param array|\Traversable|\ArrayAccess|object $var base variable - * @param mixed $keyPath scalar key or a + * @param array|Traversable|ArrayAccess|object $var base variable + * @param mixed $keyPath scalar key or a * array interpreted as key path (when passing e.g. ['foo', 'bar'], the * method will return $var['foo']['bar']) specifying the value * @@ -265,9 +265,9 @@ class PicoTwigExtension extends AbstractTwigExtension foreach ($keyPath as $key) { if (is_object($var)) { - if ($var instanceof \ArrayAccess) { + if ($var instanceof ArrayAccess) { // use ArrayAccess, see below - } elseif ($var instanceof \Traversable) { + } elseif ($var instanceof Traversable) { $var = iterator_to_array($var); } elseif (isset($var->{$key})) { $var = $var->{$key}; @@ -276,7 +276,7 @@ class PicoTwigExtension extends AbstractTwigExtension try { $var = call_user_func([ $var, 'get' . ucfirst($key) ]); continue; - } catch (\BadMethodCallException $e) { + } catch (BadMethodCallException $e) { return null; } } else { diff --git a/plugins/DummyPlugin.php b/plugins/DummyPlugin.php index 031efbd..27547bd 100644 --- a/plugins/DummyPlugin.php +++ b/plugins/DummyPlugin.php @@ -12,6 +12,7 @@ use picocms\Pico\AbstractPicoPlugin; use picocms\Pico\Pico; +use Symfony\Component\Yaml\Parser as YamlParser; use Twig\Environment as TwigEnvironment; /** @@ -475,9 +476,9 @@ class DummyPlugin extends AbstractPicoPlugin * * @see Pico::getYamlParser() * - * @param \Symfony\Component\Yaml\Parser &$yamlParser YAML parser instance + * @param YamlParser &$yamlParser YAML parser instance */ - public function onYamlParserRegistered(\Symfony\Component\Yaml\Parser &$yamlParser) + public function onYamlParserRegistered(YamlParser &$yamlParser) { // your code }