add type hints to PHP methods

This commit is contained in:
Stefan Kalscheuer 2024-03-24 11:16:44 +01:00
parent ad245ebf43
commit 5f1e1a7879
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
8 changed files with 39 additions and 38 deletions

View File

@ -23,7 +23,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function dashboard_right_now() { public static function dashboard_right_now(): void {
$total_files = wp_count_posts( 'scliveticker_tick' ); $total_files = wp_count_posts( 'scliveticker_tick' );
echo '<tr>'; echo '<tr>';
@ -37,7 +37,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function register_settings_page() { public static function register_settings_page(): void {
add_submenu_page( add_submenu_page(
'edit.php?post_type=scliveticker_tick', 'edit.php?post_type=scliveticker_tick',
'Liveticker ' . __( 'Settings', 'stklcode-liveticker' ), 'Liveticker ' . __( 'Settings', 'stklcode-liveticker' ),
@ -56,7 +56,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function register_settings() { public static function register_settings(): void {
register_setting( register_setting(
'scliveticker_settings', 'scliveticker_settings',
self::OPTION, self::OPTION,
@ -132,7 +132,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_general_section() { public static function settings_general_section(): void {
} }
/** /**
@ -140,7 +140,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_uninstall_section() { public static function settings_uninstall_section(): void {
} }
/** /**
@ -148,7 +148,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_enable_ajax_field() { public static function settings_enable_ajax_field(): void {
$checked = self::$options['enable_ajax']; $checked = self::$options['enable_ajax'];
echo '<input id="' . esc_attr( self::OPTION ) . '-enable-ajax" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_ajax]" value="1" ' . checked( $checked, 1, false ) . '> '; echo '<input id="' . esc_attr( self::OPTION ) . '-enable-ajax" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_ajax]" value="1" ' . checked( $checked, 1, false ) . '> ';
@ -161,7 +161,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_poll_interval_field() { public static function settings_poll_interval_field(): void {
$poll_interval = self::$options['poll_interval']; $poll_interval = self::$options['poll_interval'];
echo '<input id="' . esc_attr( self::OPTION ) . '-poll-interval" type="number" name="' . esc_attr( self::OPTION ) . '[poll_interval]" value="' . esc_attr( $poll_interval ) . '"> '; echo '<input id="' . esc_attr( self::OPTION ) . '-poll-interval" type="number" name="' . esc_attr( self::OPTION ) . '[poll_interval]" value="' . esc_attr( $poll_interval ) . '"> ';
@ -175,7 +175,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_enable_css_field() { public static function settings_enable_css_field(): void {
$checked = self::$options['enable_css']; $checked = self::$options['enable_css'];
echo '<input id="' . esc_attr( self::OPTION ) . '-enable-css" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_css]" value="1" ' . checked( $checked, 1, false ) . ' /> '; echo '<input id="' . esc_attr( self::OPTION ) . '-enable-css" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_css]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
@ -188,7 +188,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_show_feed_field() { public static function settings_show_feed_field(): void {
$checked = self::$options['show_feed']; $checked = self::$options['show_feed'];
echo '<input id="' . esc_attr( self::OPTION ) . '-show-feed" type="checkbox" name="' . esc_attr( self::OPTION ) . '[show_feed]" value="1" ' . checked( $checked, 1, false ) . ' /> '; echo '<input id="' . esc_attr( self::OPTION ) . '-show-feed" type="checkbox" name="' . esc_attr( self::OPTION ) . '[show_feed]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
@ -203,7 +203,7 @@ class Admin extends SCLiveticker {
* *
* @since 1.2 * @since 1.2
*/ */
public static function settings_enable_shortcode_field() { public static function settings_enable_shortcode_field(): void {
$checked = self::$options['enable_shortcode']; $checked = self::$options['enable_shortcode'];
echo '<input id="' . esc_attr( self::OPTION ) . '-enable-shortcode" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_shortcode]" value="1" ' . checked( $checked, 1, false ) . ' /> '; echo '<input id="' . esc_attr( self::OPTION ) . '-enable-shortcode" type="checkbox" name="' . esc_attr( self::OPTION ) . '[enable_shortcode]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
@ -218,7 +218,7 @@ class Admin extends SCLiveticker {
* *
* @since 1.2 * @since 1.2
*/ */
public static function settings_embedded_script_field() { public static function settings_embedded_script_field(): void {
$checked = self::$options['embedded_script']; $checked = self::$options['embedded_script'];
echo '<input id="' . esc_attr( self::OPTION ) . '-embedded-script" type="checkbox" name="' . esc_attr( self::OPTION ) . '[embedded_script]" value="1" ' . checked( $checked, 1, false ) . ' /> '; echo '<input id="' . esc_attr( self::OPTION ) . '-embedded-script" type="checkbox" name="' . esc_attr( self::OPTION ) . '[embedded_script]" value="1" ' . checked( $checked, 1, false ) . ' /> ';
@ -231,7 +231,7 @@ class Admin extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function settings_page() { public static function settings_page(): void {
include SCLIVETICKER_DIR . 'views/settings-page.php'; include SCLIVETICKER_DIR . 'views/settings-page.php';
} }
@ -242,7 +242,7 @@ class Admin extends SCLiveticker {
* *
* @return array Parsed arguments. * @return array Parsed arguments.
*/ */
public static function validate_settings( $input ) { public static function validate_settings( array $input ): array {
$defaults = self::default_options(); $defaults = self::default_options();
$result['enable_ajax'] = isset( $input['enable_ajax'] ) ? intval( $input['enable_ajax'] ) : 0; $result['enable_ajax'] = isset( $input['enable_ajax'] ) ? intval( $input['enable_ajax'] ) : 0;
@ -261,7 +261,7 @@ class Admin extends SCLiveticker {
* @return void * @return void
* @since 1.1 * @since 1.1
*/ */
public static function register_block() { public static function register_block(): void {
wp_register_script( wp_register_script(
'scliveticker-editor', 'scliveticker-editor',
SCLIVETICKER_BASE . 'scripts/block.min.js', SCLIVETICKER_BASE . 'scripts/block.min.js',

View File

@ -15,6 +15,7 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
use DateTime; use DateTime;
use WP_REST_Request;
/** /**
* Liveticker. * Liveticker.
@ -27,7 +28,7 @@ class Api {
* *
* @return void * @return void
*/ */
public static function init() { public static function init(): void {
// Add rendered modification date to WP_Post object. // Add rendered modification date to WP_Post object.
register_rest_field( register_rest_field(
'scliveticker_tick', 'scliveticker_tick',
@ -48,11 +49,11 @@ class Api {
* Filter tick queries by ticker slug and date. * Filter tick queries by ticker slug and date.
* *
* @param array $args Query vars. * @param array $args Query vars.
* @param \WP_REST_Request $request The REST request. * @param WP_REST_Request $request The REST request.
* *
* @return array Filtered query values. * @return array Filtered query values.
*/ */
public static function tick_query_filter( $args, $request ) { public static function tick_query_filter( array $args, WP_REST_Request $request ): array {
// Extract arguments. // Extract arguments.
$ticker_slug = $request->get_param( 'ticker' ); $ticker_slug = $request->get_param( 'ticker' );
$limit = intval( $request->get_param( 'limit' ) ); $limit = intval( $request->get_param( 'limit' ) );

View File

@ -62,7 +62,7 @@ class SCLiveticker {
* *
* @return void * @return void
*/ */
public static function init() { public static function init(): void {
// Skip on autosave. // Skip on autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return; return;
@ -114,7 +114,7 @@ class SCLiveticker {
* *
* @return void * @return void
*/ */
public static function register_types() { public static function register_types(): void {
// Add new taxonomy, make it hierarchical (like categories). // Add new taxonomy, make it hierarchical (like categories).
$labels = array( $labels = array(
'name' => _x( 'Ticker', 'taxonomy general name', 'stklcode-liveticker' ), 'name' => _x( 'Ticker', 'taxonomy general name', 'stklcode-liveticker' ),
@ -182,7 +182,7 @@ class SCLiveticker {
* *
* @return string * @return string
*/ */
public static function shortcode_ticker_show( $atts ) { public static function shortcode_ticker_show( array $atts ): string {
// Indicate presence of shortcode (to enqueue styles/scripts later). // Indicate presence of shortcode (to enqueue styles/scripts later).
self::$shortcode_present = true; self::$shortcode_present = true;
@ -259,7 +259,7 @@ class SCLiveticker {
* @return void * @return void
* @since 1.1 Combined former methods "enqueue_styles" and "enqueue_scripts". * @since 1.1 Combined former methods "enqueue_styles" and "enqueue_scripts".
*/ */
public static function enqueue_resources() { public static function enqueue_resources(): void {
// Only add if shortcode is present. // Only add if shortcode is present.
if ( self::$shortcode_present || self::$widget_present || self::block_present() ) { if ( self::$shortcode_present || self::$widget_present || self::block_present() ) {
wp_enqueue_script( wp_enqueue_script(
@ -301,7 +301,7 @@ class SCLiveticker {
* *
* @return void * @return void
*/ */
public static function ajax_update() { public static function ajax_update(): void {
// Verify AJAX nonce. // Verify AJAX nonce.
check_ajax_referer( 'scliveticker_update-ticks' ); check_ajax_referer( 'scliveticker_update-ticks' );
@ -394,7 +394,7 @@ class SCLiveticker {
* *
* @return void * @return void
*/ */
public static function mark_widget_present() { public static function mark_widget_present(): void {
self::$widget_present = true; self::$widget_present = true;
} }
@ -405,7 +405,7 @@ class SCLiveticker {
* *
* @return void * @return void
*/ */
protected static function update_options( $options = null ) { protected static function update_options( ?array $options = null ): void {
self::$options = wp_parse_args( self::$options = wp_parse_args(
get_option( self::OPTION ), get_option( self::OPTION ),
self::default_options() self::default_options()
@ -417,7 +417,7 @@ class SCLiveticker {
* *
* @return array The options array. * @return array The options array.
*/ */
protected static function default_options() { protected static function default_options(): array {
return array( return array(
'enable_ajax' => 1, 'enable_ajax' => 1,
'poll_interval' => 60, 'poll_interval' => 60,
@ -439,7 +439,7 @@ class SCLiveticker {
* *
* @return string HTML code of tick. * @return string HTML code of tick.
*/ */
private static function tick_html( $time, $title, $content, $id ) { private static function tick_html( string $time, string $title, string $content, int $id ): string {
if ( self::$options['enable_shortcode'] ) { if ( self::$options['enable_shortcode'] ) {
$content = do_shortcode( $content ); $content = do_shortcode( $content );
} }
@ -460,7 +460,7 @@ class SCLiveticker {
* *
* @return string HTML code of widget tick. * @return string HTML code of widget tick.
*/ */
public static function tick_html_widget( $time, $title, $highlight, $id = 0 ) { public static function tick_html_widget( string $time, string $title, bool $highlight, int $id = 0 ): string {
$out = '<li'; $out = '<li';
if ( $highlight ) { if ( $highlight ) {
$out .= ' class="sclt-widget-new"'; $out .= ' class="sclt-widget-new"';
@ -480,7 +480,7 @@ class SCLiveticker {
* @return boolean True, if Gutenberg block is present. * @return boolean True, if Gutenberg block is present.
* @since 1.1 * @since 1.1
*/ */
private static function block_present() { private static function block_present(): bool {
return function_exists( 'has_block' ) && // We are in WP 5.x environment. return function_exists( 'has_block' ) && // We are in WP 5.x environment.
has_block( 'scliveticker/ticker' ); // Specific block is present. has_block( 'scliveticker/ticker' ); // Specific block is present.
} }

View File

@ -28,7 +28,7 @@ class System extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function activate() { public static function activate(): void {
// Load current options. // Load current options.
self::update_options(); self::update_options();
@ -49,7 +49,7 @@ class System extends SCLiveticker {
* *
* @return void * @return void
*/ */
public static function uninstall() { public static function uninstall(): void {
// Delete all ticks. // Delete all ticks.
$ticks = new WP_Query( array( 'post_type' => 'scliveticker_tick' ) ); $ticks = new WP_Query( array( 'post_type' => 'scliveticker_tick' ) );
foreach ( $ticks->get_posts() as $tick ) { foreach ( $ticks->get_posts() as $tick ) {

View File

@ -31,7 +31,7 @@ class Widget extends WP_Widget {
/** /**
* Register the widget. * Register the widget.
*/ */
public static function register() { public static function register(): void {
register_widget( __CLASS__ ); register_widget( __CLASS__ );
} }
@ -164,7 +164,7 @@ class Widget extends WP_Widget {
* *
* @return array Complete instance configuration. * @return array Complete instance configuration.
*/ */
private static function fill_options_with_defaults( $instance ) { private static function fill_options_with_defaults( array $instance ): array {
$default = array( $default = array(
'title' => '', 'title' => '',
'category' => '', 'category' => '',

View File

@ -68,7 +68,7 @@ spl_autoload_register( 'scliveticker_autoload' );
* *
* @return void * @return void
*/ */
function scliveticker_autoload( $class_name ) { function scliveticker_autoload( string $class_name ): void {
$plugin_classes = array( $plugin_classes = array(
'SCLiveticker\\SCLiveticker', 'SCLiveticker\\SCLiveticker',
'SCLiveticker\\Admin', 'SCLiveticker\\Admin',

View File

@ -28,7 +28,7 @@ require_once "{$_tests_dir}/includes/functions.php";
/** /**
* Manually load the plugin being tested. * Manually load the plugin being tested.
*/ */
function _manually_load_plugin() { function _manually_load_plugin(): void {
require dirname( dirname( __FILE__ ) ) . '/stklcode-liveticker.php'; require dirname( dirname( __FILE__ ) ) . '/stklcode-liveticker.php';
} }

View File

@ -24,7 +24,7 @@ class Test_API extends WP_UnitTestCase {
* *
* @return void * @return void
*/ */
public function set_up() { public function set_up(): void {
parent::set_up(); parent::set_up();
global $wp_rest_server; global $wp_rest_server;
$wp_rest_server = new WP_REST_Server(); $wp_rest_server = new WP_REST_Server();
@ -36,7 +36,7 @@ class Test_API extends WP_UnitTestCase {
* *
* @return void * @return void
*/ */
public function test_register_route() { public function test_register_route(): void {
global $wp_rest_server; global $wp_rest_server;
$routes = $wp_rest_server->get_routes(); $routes = $wp_rest_server->get_routes();
@ -52,7 +52,7 @@ class Test_API extends WP_UnitTestCase {
* *
* @return void * @return void
*/ */
public function test_get_ticks() { public function test_get_ticks(): void {
global $wp_rest_server; global $wp_rest_server;
$request = new WP_REST_Request( 'GET', '/wp/v2/scliveticker_tick' ); $request = new WP_REST_Request( 'GET', '/wp/v2/scliveticker_tick' );