From 9aae69a693122d004db8c59187b0060098244f6e Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Sun, 2 May 2021 19:33:29 +0100 Subject: [PATCH] refactor: add strict typing --- src/admin/class-admin.php | 6 ++-- src/admin/class-wysiwyg.php | 2 +- src/admin/layout/class-engine.php | 2 +- src/admin/layout/class-settings.php | 4 +-- src/footnotes.php | 11 ++++--- src/includes/class-activator.php | 2 ++ src/includes/class-config.php | 2 ++ src/includes/class-convert.php | 4 ++- src/includes/class-core.php | 2 ++ src/includes/class-deactivator.php | 2 ++ src/includes/class-i18n.php | 2 ++ src/includes/class-loader.php | 2 ++ src/includes/class-settings.php | 2 ++ src/includes/class-template.php | 4 ++- src/public/class-general.php | 27 +++++++++------- src/public/class-parser.php | 31 ++++++++++--------- src/public/widget/class-base.php | 10 +++--- .../widget/class-reference-container.php | 15 +++++---- src/uninstall.php | 2 ++ 19 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src/admin/class-admin.php b/src/admin/class-admin.php index 5c19646..49ec24f 100644 --- a/src/admin/class-admin.php +++ b/src/admin/class-admin.php @@ -115,12 +115,12 @@ class Admin { array(), ( PRODUCTION_ENV ) ? $this->version : filemtime( plugin_dir_path( - __FILE__ + __FILE__ ) . 'css/settings.css' ), 'all' ); - + } /** @@ -136,7 +136,7 @@ class Admin { array(), ( PRODUCTION_ENV ) ? $this->version : filemtime( plugin_dir_path( - __FILE__ + __FILE__ ) . 'js/wysiwyg-editor.js' ), false diff --git a/src/admin/class-wysiwyg.php b/src/admin/class-wysiwyg.php index bc74448..55069dc 100644 --- a/src/admin/class-wysiwyg.php +++ b/src/admin/class-wysiwyg.php @@ -12,7 +12,7 @@ * @since 2.8.0 Rename file from `wysiwyg.php` to `class-footnotes-wysiwyg.php`, * move from `class/` sub-directory to `admin/`. */ - + declare(strict_types=1); namespace footnotes\admin; diff --git a/src/admin/layout/class-engine.php b/src/admin/layout/class-engine.php index 1a960c2..c03d6e1 100644 --- a/src/admin/layout/class-engine.php +++ b/src/admin/layout/class-engine.php @@ -583,7 +583,7 @@ abstract class Engine { * @since 1.5.0 * @todo Refactor HTML generation. */ - protected function add_num_box( string $p_str_setting_name, int $p_in_min, int $p_int_max, bool $p_bool_deci = false ): string { + protected function add_num_box( string $p_str_setting_name, int $p_in_min, int $p_int_max, bool $p_bool_deci = false ): string { // Collect data for given settings field. $l_arr_data = $this->load_setting( $p_str_setting_name ); diff --git a/src/admin/layout/class-settings.php b/src/admin/layout/class-settings.php index 4b5359b..53ec42d 100644 --- a/src/admin/layout/class-settings.php +++ b/src/admin/layout/class-settings.php @@ -1231,8 +1231,8 @@ class Settings extends Engine { * @todo Review in light of admin/public split. */ public function help(): void { - $footnotes = new General\General($this->plugin_name, "foo"); - + $footnotes = new General\General( $this->plugin_name, 'foo' ); + // Load footnotes starting and end tag. $l_arr_footnote_starting_tag = $this->load_setting( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START ); $l_arr_footnote_ending_tag = $this->load_setting( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END ); diff --git a/src/footnotes.php b/src/footnotes.php index e929969..0718185 100755 --- a/src/footnotes.php +++ b/src/footnotes.php @@ -25,6 +25,8 @@ * License URI: https://www.gnu.org/licenses/gpl-3.0.html */ +declare(strict_types=1); + namespace footnotes; // If this file is called directly, abort. @@ -90,12 +92,13 @@ function deactivate_footnotes(): void { /* * TODO: currently these throw an error: - * Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be - * a valid callback, function "deactivate_footnotes" not found or invalid + * Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be + * a valid callback, function "deactivate_footnotes" not found or invalid * function name in /srv/www/wordpress-one/public_html/wp-includes/class-wp-hook.php:292 + * + * register_activation_hook( __FILE__, 'activate_footnotes' ); + * register_deactivation_hook( __FILE__, 'deactivate_footnotes' ); */ -//register_activation_hook( __FILE__, 'activate_footnotes' ); -//register_deactivation_hook( __FILE__, 'deactivate_footnotes' ); /** * The core plugin class that defines internationalization, admin-specific and diff --git a/src/includes/class-activator.php b/src/includes/class-activator.php index c11e313..63a7d1a 100644 --- a/src/includes/class-activator.php +++ b/src/includes/class-activator.php @@ -25,6 +25,8 @@ * @since 2.8.0 */ +declare(strict_types=1); + namespace footnotes\includes; /** diff --git a/src/includes/class-config.php b/src/includes/class-config.php index 72620c9..8f93d29 100644 --- a/src/includes/class-config.php +++ b/src/includes/class-config.php @@ -29,6 +29,8 @@ * @deprecated */ +declare(strict_types=1); + namespace footnotes\includes; /** diff --git a/src/includes/class-convert.php b/src/includes/class-convert.php index 3e70b87..0d142c8 100644 --- a/src/includes/class-convert.php +++ b/src/includes/class-convert.php @@ -8,6 +8,8 @@ * rename `class/` sub-directory to `includes/`. */ +declare(strict_types=1); + namespace footnotes\includes; /** @@ -40,7 +42,7 @@ class Convert { return self::to_arabic_leading( $p_int_index ); case 'arabic_plain': default: - return $p_int_index; + return (string) $p_int_index; } } diff --git a/src/includes/class-core.php b/src/includes/class-core.php index 7a64fe2..69a02a9 100644 --- a/src/includes/class-core.php +++ b/src/includes/class-core.php @@ -27,6 +27,8 @@ * Renamed parent `class/` directory to `includes/`. */ +declare(strict_types=1); + namespace footnotes\includes; use footnotes\general as General; diff --git a/src/includes/class-deactivator.php b/src/includes/class-deactivator.php index 8c2aa9a..0f13e6f 100644 --- a/src/includes/class-deactivator.php +++ b/src/includes/class-deactivator.php @@ -6,6 +6,8 @@ * @since 2.8.0 */ +declare(strict_types=1); + namespace footnotes\includes; /** diff --git a/src/includes/class-i18n.php b/src/includes/class-i18n.php index 4d7a2fd..626596b 100644 --- a/src/includes/class-i18n.php +++ b/src/includes/class-i18n.php @@ -8,6 +8,8 @@ * rename `class/` sub-directory to `includes/`. */ +declare(strict_types=1); + namespace footnotes\includes; require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-config.php'; diff --git a/src/includes/class-loader.php b/src/includes/class-loader.php index 62915b6..fe93898 100644 --- a/src/includes/class-loader.php +++ b/src/includes/class-loader.php @@ -7,6 +7,8 @@ * @since 2.8.0 */ +declare(strict_types=1); + namespace footnotes\includes; /** diff --git a/src/includes/class-settings.php b/src/includes/class-settings.php index 8d31708..7a279fd 100644 --- a/src/includes/class-settings.php +++ b/src/includes/class-settings.php @@ -8,6 +8,8 @@ * Renamed parent `class/` directory to `includes/`. */ +declare(strict_types=1); + namespace footnotes\includes; /** diff --git a/src/includes/class-template.php b/src/includes/class-template.php index 86f8edb..a05d810 100644 --- a/src/includes/class-template.php +++ b/src/includes/class-template.php @@ -11,6 +11,8 @@ * rename `class/` sub-directory to `includes/`. */ +declare(strict_types=1); + namespace footnotes\includes; /** @@ -120,7 +122,7 @@ class Template { } // Iterate through each placeholder and replace it with its value. foreach ( $p_arr_placeholders as $l_str_placeholder => $l_str_value ) { - $this->a_str_replaced_content = str_replace( '[[' . $l_str_placeholder . ']]', $l_str_value, $this->a_str_replaced_content ); + $this->a_str_replaced_content = str_replace( '[[' . $l_str_placeholder . ']]', (string) $l_str_value, $this->a_str_replaced_content ); } // Success. return true; diff --git a/src/public/class-general.php b/src/public/class-general.php index 6228099..fd1d8aa 100644 --- a/src/public/class-general.php +++ b/src/public/class-general.php @@ -6,6 +6,8 @@ * @since 2.8.0 */ +declare(strict_types=1); + namespace footnotes\general; use footnotes\includes as Includes; @@ -29,7 +31,7 @@ class General { * @access private * @var string $plugin_name The ID of this plugin. */ - private $plugin_name; + private string $plugin_name; /** * The version of this plugin. @@ -39,7 +41,7 @@ class General { * @access private * @var string $version The current version of this plugin. */ - private $version; + private string $version; /** * The reference container widget. @@ -48,17 +50,18 @@ class General { * * @var Widget\Reference_Container $reference_container_widget The reference container widget */ - private $reference_container_widget; + private Widget\Reference_Container $reference_container_widget; /** * The footnote parser. * * @since 1.5.0 * @since 2.8.0 Moved from {@see Footnotes} to {@see Includes\Public}. + * @todo Review null init. * * @var Parser $task The Plugin task. */ - public $a_obj_task = null; + public ?Parser $a_obj_task = null; /** * Flag for using tooltips. @@ -107,7 +110,7 @@ class General { * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ - public function __construct( $plugin_name, $version ) { + public function __construct( string $plugin_name, string $version ) { $this->plugin_name = $plugin_name; $this->version = $version; @@ -132,7 +135,7 @@ class General { * * @since 2.8.0 */ - private function load_dependencies() { + private function load_dependencies(): void { // TODO: neaten up and document once placements and names are settled. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-config.php'; require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-settings.php'; @@ -156,7 +159,7 @@ class General { * @since 2.5.5 Change stylesheet schema. * @since 2.8.0 Moved from {@see Footnotes} to {@see Includes\Public}. */ - public function enqueue_styles() { + public function enqueue_styles(): void { if ( PRODUCTION_ENV ) { // Set tooltip mode for use in stylesheet name. if ( self::$a_bool_tooltips_enabled ) { @@ -204,20 +207,20 @@ class General { array(), ( PRODUCTION_ENV ) ? $this->version : filemtime( plugin_dir_path( - __FILE__ + __FILE__ ) . "css/footnotes-{$l_str_tooltip_mode_short}brpl{$l_str_layout_mode}.min.css" ), 'all' ); } else { - foreach (array('amp-tooltips', 'common', 'layout-entry-content', 'layout-main-content', 'layout-reference-container', 'tooltips', 'tooltips-alternative') as $val) { + foreach ( array( 'amp-tooltips', 'common', 'layout-entry-content', 'layout-main-content', 'layout-reference-container', 'tooltips', 'tooltips-alternative' ) as $val ) { wp_enqueue_style( "footnotes-$val", plugin_dir_url( __FILE__ ) . "css/dev-$val.css", array(), filemtime( plugin_dir_path( - __FILE__ + __FILE__ ) . "css/dev-$val.css" ), 'all' @@ -235,7 +238,7 @@ class General { * @since 2.5.6 Add jQuery dependency. * @since 2.8.0 Moved from {@see Footnotes} to {@see Includes\Public}. */ - public function enqueue_scripts() { + public function enqueue_scripts(): void { /* * Enqueues the jQuery library registered by WordPress. * @@ -284,7 +287,7 @@ class General { * @since 1.5.0 * @since 2.8.0 Moved from {@see Footnotes} to {@see Includes\Public}. */ - public function register_widgets() { + public function register_widgets(): void { register_widget( $this->reference_container_widget ); } } diff --git a/src/public/class-parser.php b/src/public/class-parser.php index 4b55203..194a4e7 100644 --- a/src/public/class-parser.php +++ b/src/public/class-parser.php @@ -9,6 +9,8 @@ * move from `class/` sub-directory to `public/`. */ +declare(strict_types=1); + namespace footnotes\general; use footnotes\includes as Includes; @@ -284,7 +286,7 @@ class Parser { * @since 2.1.0 Remove @see 'the_post' support. * @todo Move to {@see General}. */ - public function register_hooks() { + public function register_hooks(): void { // Get values from settings. $l_int_the_title_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_THE_TITLE_PRIORITY_LEVEL ); $l_int_the_content_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_THE_CONTENT_PRIORITY_LEVEL ); @@ -431,7 +433,7 @@ class Parser { * @since 1.5.0 * @todo Refactor to enqueue stylesheets properly in {@see General}. */ - public function footnotes_output_head() { + public function footnotes_output_head(): void { // Insert start tag without switching out of PHP. echo "\r\n