Skip to content

Commit

Permalink
refactor: add type hints to Admin and Cron
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Mar 23, 2024
1 parent 64cb846 commit 6576f6c
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 57 deletions.
16 changes: 8 additions & 8 deletions src/Admin/CommentsColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommentsColumns {
/**
* Registers the module hooks.
*/
public static function init() {
public static function init(): void {
if ( ! DashboardHelper::is_edit_spam_comments_page() ) {
return;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ public static function init() {
* @since 2.6.0
* @change 2.6.0
*/
public static function register_plugin_columns( $columns ) {
public static function register_plugin_columns( array $columns ): array {
return array_merge(
$columns,
[
Expand All @@ -77,7 +77,7 @@ public static function register_plugin_columns( $columns ) {
* @since 2.6.0
* @change 2.6.0
*/
public static function print_plugin_column( $column, $comment_id ) {
public static function print_plugin_column( string $column, int $comment_id ): void {
if ( 'antispam_bee_reason' !== $column ) {
return;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public static function print_plugin_column( $column, $comment_id ) {
* @since 2.6.3
* @change 2.6.3
*/
public static function register_sortable_columns( $columns ) {
public static function register_sortable_columns( array $columns ): array {
$columns['antispam_bee_reason'] = 'antispam_bee_reason';

return $columns;
Expand All @@ -120,7 +120,7 @@ public static function register_sortable_columns( $columns ) {
* @since 2.6.3
* @change 2.6.3
*/
public static function set_orderby_query( $query ) {
public static function set_orderby_query( WP_Comment_Query $query ): void {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '';

Expand All @@ -138,7 +138,7 @@ public static function set_orderby_query( $query ) {
*
* @global wpdb $wpdb
*/
public static function filter_columns() {
public static function filter_columns(): void {
global $wpdb;
?>
<label class="screen-reader-text"
Expand Down Expand Up @@ -182,7 +182,7 @@ public static function filter_columns() {
*
* @param WP_Comment_Query $query Current WordPress query.
*/
public static function filter_by_spam_reason( $query ) {
public static function filter_by_spam_reason( WP_Comment_Query $query ): void {
$spam_reason = isset( $_GET['comment_spam_reason'] ) ? sanitize_text_field( wp_unslash( $_GET['comment_spam_reason'] ) ) : '';
if ( empty( $spam_reason ) ) {
return;
Expand Down Expand Up @@ -221,7 +221,7 @@ public static function filter_by_spam_reason( $query ) {
* @since 2.6.1
* @change 2.6.1
*/
public static function print_column_styles() {
public static function print_column_styles(): void {
?>
<style>
.column-antispam_bee_reason {
Expand Down
12 changes: 6 additions & 6 deletions src/Admin/DashboardWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DashboardWidgets {
/**
* Initialize the dashboard widgets.
*/
public static function init() {
public static function init(): void {
if ( DashboardHelper::is_dashboard_page() ) {
add_action( 'antispam_bee_count', [ __CLASS__, 'the_spam_count' ] );
add_filter( 'dashboard_glance_items', [ __CLASS__, 'add_dashboard_count' ] );
Expand All @@ -34,7 +34,7 @@ public static function init() {
* @since 0.1
* @since 2.6.5
*/
public static function add_dashboard_count( $items = array() ) {
public static function add_dashboard_count( array $items = array() ): array {
if ( ! current_user_can( 'manage_options' ) || ! Statistics::is_active() ) {
return $items;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public static function add_dashboard_count( $items = array() ) {
* @since 0.1
* @since 2.4
*/
private static function get_spam_count() {
private static function get_spam_count(): string {
$count = intval( Settings::get_option( 'spam_count', '' ) );

return self::format_number( $count );
Expand All @@ -77,10 +77,10 @@ private static function get_spam_count() {
/**
* Format a number.
*
* @param float $number Number to format.
* @param float|int $number Number to format.
* @return string
*/
private static function format_number( $number ) {
private static function format_number( $number ): string {
return ( get_locale() === 'de_DE' ? number_format( $number, 0, '', '.' ) : number_format_i18n( $number ) );
}

Expand All @@ -90,7 +90,7 @@ private static function format_number( $number ) {
* @since 0.1
* @since 2.4
*/
public static function the_spam_count() {
public static function the_spam_count(): void {
echo esc_html( self::get_spam_count() );
}
}
2 changes: 1 addition & 1 deletion src/Admin/Fields/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Checkbox extends Field implements RenderElement {
/**
* Get HTML.
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

$label = ! empty( $this->get_label() ) ? sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Fields/CheckboxGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CheckboxGroup extends Field implements RenderElement {
/**
* Render HTML.
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

$options = isset( $this->option['options'] ) ? $this->option['options'] : [];
Expand Down
12 changes: 6 additions & 6 deletions src/Admin/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ abstract class Field {
/**
* Initializing field
*
* @param string $type Item type.
* @param array $option Field options.
* @param class-string<Controllable> $controllable The related controllable.
* @param string $type Item type.
* @param array $option Field options.
* @param string $controllable The related controllable (class name).
*/
public function __construct( $type, $option, $controllable ) {
public function __construct( string $type, array $option, string $controllable ) {
$this->type = $type;
$this->option = $option;
$this->controllable_option_name = $controllable::get_option_name( $this->option['option_name'] );
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function maybe_show_description() {
/**
* Get HTML for field.
*
* @return string Element HTML.
* @return void1
*/
abstract public function render();
abstract public function render(): void;
}
4 changes: 2 additions & 2 deletions src/Admin/Fields/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Inline extends Field implements RenderElement {
/**
* Get HTML for field.
*
* @return string Element HTML.
* @return void
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

if ( ! $this->option['input'] instanceof Field ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Select extends Field implements RenderElement {
/**
* Get HTML.
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

$name = $this->get_name();
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Fields/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function get_placeholder() {
/**
* Get HTML.
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

printf(
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Fields/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Textarea extends Field implements RenderElement {
/**
* Render HTML.
*/
public function render() {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

printf(
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/RenderElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface RenderElement {
/**
* Render function.
*
* @return string Rendered Element.
* @return void
*/
public function render();
public function render(): void;
}
26 changes: 13 additions & 13 deletions src/Admin/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

use AntispamBee\Admin\Fields\Checkbox;
use AntispamBee\Admin\Fields\CheckboxGroup;
use AntispamBee\Admin\Fields\Field;
use AntispamBee\Admin\Fields\Inline;
use AntispamBee\Admin\Fields\Select;
use AntispamBee\Admin\Fields\Text;
use AntispamBee\Admin\Fields\Textarea;
use AntispamBee\Interfaces\Controllable;
use Exception;

/**
* Sections for admin.
Expand Down Expand Up @@ -64,7 +64,7 @@ class Section {
* @param string $description Description of the tab.
* @param string|null $type Item type (e.g. comment, trackback).
*/
public function __construct( $slug, $title, $description = '', $type = null ) {
public function __construct( string $slug, string $title, string $description = '', string $type = null ) {
$this->slug = $slug;
$this->title = $title;
$this->description = $description;
Expand All @@ -77,7 +77,7 @@ public function __construct( $slug, $title, $description = '', $type = null ) {
* @param array|null $controllables List of controllable items to add.
* @return void
*/
public function add_controllables( $controllables ) {
public function add_controllables( ?array $controllables ): void {
if ( ! empty( $controllables ) ) {
$this->generate_fields( $controllables );
}
Expand All @@ -89,7 +89,7 @@ public function add_controllables( $controllables ) {
* @param Controllable[] $controllables List of controllable items to add.
* @return void
*/
private function generate_fields( $controllables ) {
private function generate_fields( array $controllables ): void {
foreach ( $controllables as $controllable ) {
$label = $controllable::get_label();
$description = $controllable::get_description();
Expand Down Expand Up @@ -127,11 +127,11 @@ private function generate_fields( $controllables ) {
/**
* Generate field for a controllable item's option.
*
* @param array $option Option name.
* @param Controllable $controllable Controllable item.
* @param array $option Option name.
* @param string $controllable Controllable item (class name).
* @return Checkbox|CheckboxGroup|Inline|Select|Text|Textarea|null
*/
private function generate_field( $option, $controllable ) {
private function generate_field( array $option, string $controllable ): ?Field {
switch ( $option['type'] ) {
case 'input':
return new Text( $this->type, $option, $controllable );
Expand All @@ -156,7 +156,7 @@ private function generate_field( $option, $controllable ) {
*
* @return string Name of the field.
*/
public function get_slug() {
public function get_slug(): string {
return $this->slug;
}

Expand All @@ -165,7 +165,7 @@ public function get_slug() {
*
* @return string Title of the field.
*/
public function get_title() {
public function get_title(): string {
return $this->title;
}

Expand All @@ -174,7 +174,7 @@ public function get_title() {
*
* @return string Title of the field.
*/
public function get_description() {
public function get_description(): string {
return $this->description;
}

Expand All @@ -183,7 +183,7 @@ public function get_description() {
*
* @return array
*/
public function get_rows() {
public function get_rows(): array {
return $this->rows;
}

Expand All @@ -202,7 +202,7 @@ public function get_callback() {
/**
* Renders the settings section.
*/
public function render() {
public function render(): void {
add_settings_section(
$this->get_slug(),
$this->get_title(),
Expand Down Expand Up @@ -231,7 +231,7 @@ function () use ( $row ) {
*
* @param array $row Row of fields.
*/
protected function render_row_fields( $row ) {
protected function render_row_fields( array $row ): void {
foreach ( $row['fields'] as $key => $field ) {
$field->render();

Expand Down
10 changes: 5 additions & 5 deletions src/Admin/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SettingsPage {
/**
* Add Hooks.
*/
public function init() {
public function init(): void {
add_action( 'admin_menu', [ $this, 'add_menu' ] );
add_action( 'admin_init', [ $this, 'setup_settings' ] );

Expand All @@ -69,7 +69,7 @@ public function init() {
/**
* Add settings page.
*/
public function add_menu() {
public function add_menu(): void {
add_options_page(
__( 'Antispam Bee', 'antispam-bee' ),
__( 'Antispam Bee', 'antispam-bee' ),
Expand All @@ -82,7 +82,7 @@ public function add_menu() {
/**
* Setup tabs content.
*/
public function setup_settings() {
public function setup_settings(): void {
// Todo: Add a way to build rows and fields with a fluent interface? (Nice-to-have).

/*
Expand Down Expand Up @@ -145,7 +145,7 @@ public function setup_settings() {
*
* @return void
*/
protected function populate_tabs() {
protected function populate_tabs(): void {
$type = $this->active_tab;

$data = [];
Expand Down Expand Up @@ -193,7 +193,7 @@ protected function populate_tabs() {
/**
* Settings page content.
*/
public function options_page() {
public function options_page(): void {
?>
<div class="wrap" id="ab_main">
<h1><?php esc_html_e( 'Antispam Bee', 'antispam-bee' ); ?></h1>
Expand Down
Loading

0 comments on commit 6576f6c

Please sign in to comment.