From c1421d197838ae8e487b99c4c1f12ce589677520 Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Sun, 2 May 2021 19:19:46 +0100 Subject: [PATCH] refactor: add return types --- src/admin/class-admin.php | 21 +++++---- src/admin/class-wysiwyg.php | 10 +++-- src/admin/layout/class-engine.php | 70 +++++++++++++++-------------- src/admin/layout/class-init.php | 18 ++++---- src/admin/layout/class-settings.php | 67 ++++++++++++++------------- src/includes/class-settings.php | 27 ++++++----- src/public/class-general.php | 16 ++++++- 7 files changed, 130 insertions(+), 99 deletions(-) diff --git a/src/admin/class-admin.php b/src/admin/class-admin.php index b41fe39..5c19646 100644 --- a/src/admin/class-admin.php +++ b/src/admin/class-admin.php @@ -11,6 +11,8 @@ * @since 2.8.0 */ +declare(strict_types=1); + namespace footnotes\admin; use footnotes\includes as Includes; @@ -34,7 +36,7 @@ class Admin { * @see Includes\Footnotes::$plugin_name * @var string $plugin_name The ID of this plugin. */ - private $plugin_name; + private string $plugin_name; /** * The version of this plugin. @@ -44,7 +46,7 @@ class Admin { * @see Includes\Footnotes::$version * @var string $version The current version of this plugin. */ - private $version; + private string $version; /** * The WYSIWYG editor integration object. @@ -52,7 +54,7 @@ class Admin { * @since 2.8.0 * @var WYSIWYG $wysiwyg The WYSIWYG editor integration object. */ - public $wysiwyg; + public WYSIWYG $wysiwyg; /** * Initialize the class and set its properties. @@ -62,7 +64,7 @@ class Admin { * * @since 2.8.0 */ - public function __construct( $plugin_name, $version ) { + public function __construct( string $plugin_name, string $version ) { $this->plugin_name = $plugin_name; $this->version = $version; @@ -84,7 +86,7 @@ class Admin { * * @since 2.8.0 */ - private function load_dependencies() { + private function load_dependencies(): void { /** * The class responsible for WYSIWYG editor integration. */ @@ -105,7 +107,7 @@ class Admin { * * @since 2.8.0 */ - public function enqueue_styles() { + public function enqueue_styles(): void { wp_enqueue_style( $this->plugin_name, @@ -113,11 +115,12 @@ class Admin { array(), ( PRODUCTION_ENV ) ? $this->version : filemtime( plugin_dir_path( - dirname( __FILE__ ) + __FILE__ ) . 'css/settings.css' ), 'all' ); + } /** @@ -125,7 +128,7 @@ class Admin { * * @since 2.8.0 */ - public function enqueue_scripts() { + public function enqueue_scripts(): void { wp_enqueue_script( $this->plugin_name, @@ -133,7 +136,7 @@ class Admin { array(), ( PRODUCTION_ENV ) ? $this->version : filemtime( plugin_dir_path( - dirname( __FILE__ ) + __FILE__ ) . 'js/wysiwyg-editor.js' ), false diff --git a/src/admin/class-wysiwyg.php b/src/admin/class-wysiwyg.php index 03557d7..bc74448 100644 --- a/src/admin/class-wysiwyg.php +++ b/src/admin/class-wysiwyg.php @@ -12,6 +12,8 @@ * @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; @@ -34,7 +36,7 @@ class WYSIWYG { * @since 1.5.0 * @todo Should this be `static`? */ - public static function new_visual_editor_button( $p_arr_buttons ) { + public static function new_visual_editor_button( array $p_arr_buttons ): array { $p_arr_buttons[] = 'footnotes'; return $p_arr_buttons; } @@ -44,7 +46,7 @@ class WYSIWYG { * * @since 1.5.0 */ - public static function new_plain_text_editor_button() { + public static function new_plain_text_editor_button(): void { $l_obj_template = new Includes\Template( \footnotes\includes\Template::C_STR_DASHBOARD, 'editor-button' ); // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped echo $l_obj_template->get_content(); @@ -60,7 +62,7 @@ class WYSIWYG { * @since 1.5.0 * @todo Should this be `static`? */ - public static function include_scripts( $p_arr_plugins ) { + public static function include_scripts( array $p_arr_plugins ): array { $p_arr_plugins['footnotes'] = plugins_url( '/../admin/js/wysiwyg-editor' . ( ( PRODUCTION_ENV ) ? '.min' : '' ) . '.js', __FILE__ ); return $p_arr_plugins; } @@ -71,7 +73,7 @@ class WYSIWYG { * * @since 1.5.0 */ - public static function ajax_callback() { + public static function ajax_callback(): void { // Get start and end tag for the footnotes short code. $l_str_starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START ); $l_str_ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END ); diff --git a/src/admin/layout/class-engine.php b/src/admin/layout/class-engine.php index 5bc5389..1a960c2 100644 --- a/src/admin/layout/class-engine.php +++ b/src/admin/layout/class-engine.php @@ -13,6 +13,8 @@ * rename `dashboard/` sub-directory to `layout/`. */ +declare(strict_types=1); + namespace footnotes\admin\layout; use footnotes\includes as Includes; @@ -37,7 +39,7 @@ abstract class Engine { * * @since 2.8.0 */ - protected $plugin_name; + protected string $plugin_name; /** * Stores the Hook connection string for the child sub-page. @@ -47,7 +49,7 @@ abstract class Engine { * * @since 1.5.0 */ - protected $a_str_sub_page_hook; + protected ?string $a_str_sub_page_hook; /** * Stores all Sections for the child sub-page. @@ -57,7 +59,7 @@ abstract class Engine { * * @since 1.5.0 */ - protected $a_arr_sections = array(); + protected array $a_arr_sections = array(); /** * Returns a Priority index. Lower numbers have a higher priority. @@ -67,7 +69,7 @@ abstract class Engine { * * @since 1.5.0 */ - abstract public function get_priority(); + abstract public function get_priority(): int; /** * Returns the unique slug of the child sub-page. @@ -78,7 +80,7 @@ abstract class Engine { * * @since 1.5.0 */ - abstract protected function get_sub_page_slug(); + abstract protected function get_sub_page_slug(): string; /** * Returns the title of the child sub-page. @@ -89,7 +91,7 @@ abstract class Engine { * * @since 1.5.0 */ - abstract protected function get_sub_page_title(); + abstract protected function get_sub_page_title(): string; /** * Returns an array of all registered sections for a sub-page. @@ -100,7 +102,7 @@ abstract class Engine { * * @since 1.5.0 */ - abstract protected function get_sections(); + abstract protected function get_sections(): array; /** * Returns an array of all registered meta boxes. @@ -111,7 +113,7 @@ abstract class Engine { * * @since 1.5.0 */ - abstract protected function get_meta_boxes(); + abstract protected function get_meta_boxes(): array; /** * Returns an array describing a sub-page section. @@ -134,7 +136,7 @@ abstract class Engine { * @since 1.5.0 * @todo Refactor sections into their own class? */ - protected function add_section( $p_str_id, $p_str_title, $p_int_settings_container_index, $p_bool_has_submit_button = true ) { + protected function add_section( string $p_str_id, string $p_str_title, int $p_int_settings_container_index, bool $p_bool_has_submit_button = true ): array { return array( 'id' => $this->plugin_name . '-' . $p_str_id, 'title' => $p_str_title, @@ -164,7 +166,7 @@ abstract class Engine { * @todo Refactor meta boxes into their own class? * @todo Pass actual functions rather than strings? */ - protected function add_meta_box( $p_str_section_id, $p_str_id, $p_str_title, $p_str_callback_function_name ) { + protected function add_meta_box( string $p_str_section_id, string $p_str_id, string $p_str_title, string $p_str_callback_function_name ): array { return array( 'parent' => $this->plugin_name . '-' . $p_str_section_id, 'id' => $p_str_id, @@ -178,7 +180,7 @@ abstract class Engine { * * @since 1.5.0 */ - public function register_sub_page() { + public function register_sub_page(): void { global $submenu; if ( array_key_exists( plugin_basename( Init::C_STR_MAIN_MENU_SLUG ), $submenu ) ) { @@ -206,7 +208,7 @@ abstract class Engine { * * @since 1.5.0 */ - public function register_sections() { + public function register_sections(): void { foreach ( $this->get_sections() as $l_arr_section ) { // Append tab to the tab-array. $this->a_arr_sections[ $l_arr_section['id'] ] = $l_arr_section; @@ -230,7 +232,7 @@ abstract class Engine { * * @since 1.5.0 */ - private function register_meta_boxes( $p_str_parent_id ) { + private function register_meta_boxes( string $p_str_parent_id ): void { // Iterate through each meta box. foreach ( $this->get_meta_boxes() as $l_arr_meta_box ) { if ( $p_str_parent_id !== $l_arr_meta_box['parent'] ) { @@ -254,7 +256,7 @@ abstract class Engine { * @since 1.5.0 * @todo Move to {@see Includes\Admin}. */ - private function append_scripts() { + private function append_scripts(): void { wp_enqueue_script( 'postbox' ); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); @@ -267,7 +269,7 @@ abstract class Engine { * @since 1.5.0 * @todo Review nonce verification. */ - public function display_content() { + public function display_content(): void { $this->append_scripts(); // Get the current section. @@ -337,7 +339,7 @@ abstract class Engine { * @since 1.5.0 * @todo Review nonce verification. */ - private function save_settings() { + private function save_settings(): bool { $l_arr_new_settings = array(); // TODO: add nonce verification. @@ -361,7 +363,7 @@ abstract class Engine { * @since 1.5.0 * @todo Required? Should be `abstract`? */ - public function description() { + public function description(): void { // Default no description will be displayed. } @@ -382,7 +384,7 @@ abstract class Engine { * @since 2.5.11 Broken due to accidental removal of `esc_attr()` call. * @since 2.6.1 Restore `esc_attr()` call. */ - protected function load_setting( $p_str_setting_key_name ) { + protected function load_setting( string $p_str_setting_key_name ): array { // Get current section. reset( $this->a_arr_sections ); $p_arr_return = array(); @@ -393,7 +395,7 @@ abstract class Engine { } /** - * Returns a simple text inside HTML `` element. + * Returns a simple text inside a 'span' element. * * @access protected * @param string $p_str_text Message to be surrounded with `` tags. @@ -402,12 +404,12 @@ abstract class Engine { * @since 1.5.0 * @todo Refactor HTML generation. */ - protected function add_text( $p_str_text ) { + protected function add_text( string $p_str_text ): string { return sprintf( '%s', $p_str_text ); } /** - * Returns the HTML tag for an ``/`` element. + * Constructs the HTML for a text 'input' element. * * @access protected * @param string $p_str_setting_name Setting key. @@ -449,7 +451,7 @@ abstract class Engine { * @since 1.5.0 * @todo Refactor HTML generation. */ - protected function add_text_box( $p_str_setting_name, $p_str_max_length = 999, $p_bool_readonly = false, $p_bool_hidden = false ) { + protected function add_text_box( string $p_str_setting_name, int $p_str_max_length = 999, bool $p_bool_readonly = false, bool $p_bool_hidden = false ): string { $l_str_style = ''; // Collect data for given settings field. $l_arr_data = $this->load_setting( $p_str_setting_name ); @@ -468,7 +470,7 @@ abstract class Engine { } /** - * Constructs the HTML for a checkbox `` element. + * Constructs the HTML for a checkbox 'input' element. * * @access protected * @param string $p_str_setting_name Setting key. @@ -477,7 +479,7 @@ abstract class Engine { * @since 1.5.0 * @todo Refactor HTML generation. */ - protected function add_checkbox( $p_str_setting_name ) { + protected function add_checkbox( string $p_str_setting_name ): string { // Collect data for given settings field. $l_arr_data = $this->load_setting( $p_str_setting_name ); return sprintf( @@ -489,7 +491,7 @@ abstract class Engine { } /** - * Constructs the HTML for a `