From 371a61f11e00d48c2defd0d5c539bfbcd0086c90 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 25 Mar 2018 04:34:40 +0300 Subject: [PATCH] Code cleanup and refactoring #5 --- flextype/Config.php | 4 +-- flextype/Events.php | 5 +-- flextype/Filters.php | 71 ++++++++++++++++++++--------------------- flextype/Flextype.php | 17 +++++----- flextype/I18n.php | 2 +- flextype/Markdown.php | 2 +- flextype/Plugins.php | 1 - flextype/Shortcodes.php | 4 +-- 8 files changed, 52 insertions(+), 54 deletions(-) diff --git a/flextype/Config.php b/flextype/Config.php index bc59abaf..c1cbdef4 100755 --- a/flextype/Config.php +++ b/flextype/Config.php @@ -65,7 +65,7 @@ class Config * @param string $key Key * @param mixed $value Value */ - public static function set($key, $value) + public static function set($key, $value) : void { Arr::set(static::$config, $key, $value); } @@ -89,7 +89,7 @@ class Config * @access public * @return array */ - public static function getConfig() + public static function getConfig() : array { return static::$config; } diff --git a/flextype/Events.php b/flextype/Events.php index 6457660b..a4d0ffa4 100644 --- a/flextype/Events.php +++ b/flextype/Events.php @@ -9,7 +9,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - + namespace Flextype; use Arr; @@ -43,8 +43,9 @@ class Events * @param mixed $added_function Added function * @param integer $priority Priority. Default is 10 * @param array $args Arguments + * @return void */ - public static function addListener(string $event_name, $added_function, int $priority = 10, array $args = null) + public static function addListener(string $event_name, $added_function, int $priority = 10, array $args = null) : void { // Hooks a function on to a specific event. static::$events[] = array( diff --git a/flextype/Filters.php b/flextype/Filters.php index 3f05e863..6a80a2e8 100755 --- a/flextype/Filters.php +++ b/flextype/Filters.php @@ -28,7 +28,6 @@ class Filters */ protected static $filters = []; - /** * Protected constructor since this is a static class. * @@ -39,6 +38,41 @@ class Filters // Nothing here } + /** + * Add filter + * + * @access public + * @param string $filter_name The name of the filter to hook the $function_to_add to. + * @param string $function_to_add The name of the function to be called when the filter is applied. + * @param integer $priority Function to add priority - default is 10. + * @param integer $accepted_args The number of arguments the function accept default is 1. + * @return bool + */ + public static function addListener($filter_name, $function_to_add, $priority = 10, $accepted_args = 1) : bool + { + // Redefine arguments + $filter_name = (string) $filter_name; + $function_to_add = $function_to_add; + $priority = (int) $priority; + $accepted_args = (int) $accepted_args; + + // Check that we don't already have the same filter at the same priority. Thanks to WP :) + if (isset(static::$filters[$filter_name]["$priority"])) { + foreach (static::$filters[$filter_name]["$priority"] as $filter) { + if ($filter['function'] == $function_to_add) { + return true; + } + } + } + + static::$filters[$filter_name]["$priority"][] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); + + // Sort + ksort(static::$filters[$filter_name]["$priority"]); + + return true; + } + /** * Dispatch filters * @@ -77,39 +111,4 @@ class Filters return $value; } - - /** - * Add filter - * - * @access public - * @param string $filter_name The name of the filter to hook the $function_to_add to. - * @param string $function_to_add The name of the function to be called when the filter is applied. - * @param integer $priority Function to add priority - default is 10. - * @param integer $accepted_args The number of arguments the function accept default is 1. - * @return boolean - */ - public static function addListener($filter_name, $function_to_add, $priority = 10, $accepted_args = 1) - { - // Redefine arguments - $filter_name = (string) $filter_name; - $function_to_add = $function_to_add; - $priority = (int) $priority; - $accepted_args = (int) $accepted_args; - - // Check that we don't already have the same filter at the same priority. Thanks to WP :) - if (isset(static::$filters[$filter_name]["$priority"])) { - foreach (static::$filters[$filter_name]["$priority"] as $filter) { - if ($filter['function'] == $function_to_add) { - return true; - } - } - } - - static::$filters[$filter_name]["$priority"][] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); - - // Sort - ksort(static::$filters[$filter_name]["$priority"]); - - return true; - } } diff --git a/flextype/Flextype.php b/flextype/Flextype.php index 76838c33..c3ea1438 100755 --- a/flextype/Flextype.php +++ b/flextype/Flextype.php @@ -12,8 +12,7 @@ namespace Flextype; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Finder\Finder; +use Symfony\Component\{Filesystem\Filesystem, Finder\Finder}; use Url; use Session; @@ -30,7 +29,7 @@ class Flextype /** * Filesystem object * - * @var object + * @var Filesystem * @access public */ public static $filesystem = null; @@ -38,7 +37,7 @@ class Flextype /** * Finder object * - * @var object + * @var Finder * @access public */ public static $finder = null; @@ -75,7 +74,7 @@ class Flextype * * @access protected */ - protected static function app() + protected static function app() : void { // Init Finder static::$finder = new Finder(); @@ -140,9 +139,9 @@ class Flextype * Returns filesystem object * * @access public - * @return object + * @return Filesystem */ - public static function filesystem() + public static function filesystem() : Filesystem { return static::$filesystem; } @@ -151,9 +150,9 @@ class Flextype * Returns finder object * * @access public - * @return object + * @return Finder */ - public static function finder() + public static function finder() : Finder { return static::$finder; } diff --git a/flextype/I18n.php b/flextype/I18n.php index bc29e985..9f73cfc0 100644 --- a/flextype/I18n.php +++ b/flextype/I18n.php @@ -17,7 +17,7 @@ use Symfony\Component\Yaml\Yaml; class I18n { /** - * An instance of the Cache class + * An instance of the I18n class * * @var object */ diff --git a/flextype/Markdown.php b/flextype/Markdown.php index ad4a6c27..e57bc45d 100644 --- a/flextype/Markdown.php +++ b/flextype/Markdown.php @@ -20,7 +20,7 @@ class Markdown * Parsedown Extra Object * * @var object - * @access protected + * @access protected */ protected static $markdown; diff --git a/flextype/Plugins.php b/flextype/Plugins.php index 34e89c6e..66e74c9f 100755 --- a/flextype/Plugins.php +++ b/flextype/Plugins.php @@ -27,7 +27,6 @@ class Plugins * Init Plugins * * @access public - * @return mixed */ protected function __construct() { diff --git a/flextype/Shortcodes.php b/flextype/Shortcodes.php index 1d12ddb5..089b70a6 100644 --- a/flextype/Shortcodes.php +++ b/flextype/Shortcodes.php @@ -52,7 +52,7 @@ class Shortcodes * @access public * @return object */ - public static function driver() + public static function driver() : ShortcodeFacade { return static::$driver; } @@ -62,7 +62,7 @@ class Shortcodes * * @access protected */ - protected static function registerDefaultShortcodes() + protected static function registerDefaultShortcodes() : void { static::driver()->addHandler('site_url', function() { return Url::getBase();