Add return type hinting

This commit is contained in:
Corey McKrill 2023-10-24 14:00:00 -07:00
parent 0d70609dbb
commit 5681892773
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
4 changed files with 63 additions and 44 deletions

View File

@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
@ -20,7 +21,7 @@ class File {
*
* @var string
*/
protected $source;
protected $source = '';
/**
* The 0-based increment of the file, if it has been rotated. Derived from the filename. Can only be 0-9.
@ -32,16 +33,16 @@ class File {
/**
* The date the file was created, as a Unix timestamp, derived from the filename.
*
* @var int
* @var int|false
*/
protected $created;
protected $created = false;
/**
* The hash property of the file, derived from the filename.
*
* @var string
*/
protected $hash;
protected $hash = '';
/**
* Class File
@ -65,7 +66,7 @@ class File {
*
* @return void
*/
protected function parse_filename() {
protected function parse_filename(): void {
$info = pathinfo( $this->path );
$filename = $info['filename'];
$segments = explode( '-', $filename );
@ -82,8 +83,12 @@ class File {
$rotation_marker = strrpos( $this->source, '.', -1 );
if ( false !== $rotation_marker ) {
$this->rotation = substr( $this->source, -1 );
$this->source = substr( $this->source, 0, $rotation_marker );
$rotation = substr( $this->source, -1 );
if ( is_numeric( $rotation ) ) {
$this->rotation = intval( $rotation );
}
$this->source = substr( $this->source, 0, $rotation_marker );
}
}
@ -92,7 +97,7 @@ class File {
*
* @return string
*/
public function get_basename() {
public function get_basename(): string {
return basename( $this->path );
}
@ -101,16 +106,16 @@ class File {
*
* @return string
*/
public function get_source() {
public function get_source(): string {
return $this->source;
}
/**
* Get the file's rotation property.
*
* @return int
* @return int|null
*/
public function get_rotation() {
public function get_rotation(): ?int {
return $this->rotation;
}
@ -119,14 +124,14 @@ class File {
*
* @return string
*/
public function get_hash() {
public function get_hash(): string {
return $this->hash;
}
/**
* Get the file's created property.
*
* @return int
* @return int|false
*/
public function get_created_timestamp() {
return $this->created;
@ -151,15 +156,15 @@ class File {
return false;
}
return filesize( $this->path );
return @filesize( $this->path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
/**
* Delete the file from the filesystem.
*
* @return bool
* @return bool True on success, false on failure.
*/
public function delete() {
public function delete(): bool {
return @unlink( $this->path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
}

View File

@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
@ -153,10 +154,17 @@ class FileController {
/**
* Get a list of sources for existing log files.
*
* @return array
* @return array|WP_Error
*/
public function get_file_sources() {
$files = glob( $this->log_directory . '*.log' );
$files = glob( $this->log_directory . '*.log' );
if ( false === $files ) {
return new WP_Error(
'wc_log_directory_error',
__( 'Could not access the log file directory.', 'woocommerce' )
);
}
$all_sources = array_map(
function( $path ) {
$file = new File( $path );
@ -175,7 +183,7 @@ class FileController {
*
* @return int
*/
public function delete_files( $files ) {
public function delete_files( $files ): int {
$deleted = 0;
foreach ( $files as $basename ) {

View File

@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
@ -55,7 +56,7 @@ class ListTable extends WP_List_Table {
*
* @return void
*/
public function no_items() {
public function no_items(): void {
esc_html_e( 'No log files found.', 'woocommerce' );
}
@ -64,7 +65,7 @@ class ListTable extends WP_List_Table {
*
* @return array
*/
protected function get_bulk_actions() {
protected function get_bulk_actions(): array {
return array(
'delete' => __( 'Delete permanently', 'woocommerce' ),
);
@ -75,8 +76,12 @@ class ListTable extends WP_List_Table {
*
* @return array
*/
protected function get_sources_list() {
protected function get_sources_list(): array {
$sources = $this->file_controller->get_file_sources();
if ( is_wp_error( $sources ) ) {
return array();
}
sort( $sources );
return $sources;
@ -89,7 +94,7 @@ class ListTable extends WP_List_Table {
*
* @return void
*/
protected function extra_tablenav( $which ) {
protected function extra_tablenav( $which ): void {
$all_sources = $this->get_sources_list();
$current_source = filter_input( INPUT_GET, 'source', FILTER_SANITIZE_STRING ) ?? '';
@ -126,7 +131,7 @@ class ListTable extends WP_List_Table {
*
* @return void
*/
public function prepare_column_headers() {
public function prepare_column_headers(): void {
$this->_column_headers = array(
$this->get_columns(),
get_hidden_columns( $this->screen ),
@ -140,7 +145,7 @@ class ListTable extends WP_List_Table {
*
* @return void
*/
public function prepare_items() {
public function prepare_items(): void {
$per_page = $this->get_items_per_page(
self::PER_PAGE_USER_OPTION_KEY,
$this->file_controller::DEFAULTS_GET_FILES['per_page']
@ -181,7 +186,7 @@ class ListTable extends WP_List_Table {
*
* @return array
*/
public function get_columns() {
public function get_columns(): array {
$columns = array(
'cb' => '<input type="checkbox" />',
'source' => __( 'Source', 'woocommerce' ),
@ -198,7 +203,7 @@ class ListTable extends WP_List_Table {
*
* @return array
*/
protected function get_sortable_columns() {
protected function get_sortable_columns(): array {
$sortable = array(
'source' => array( 'source' ),
'created' => array( 'created' ),
@ -216,7 +221,7 @@ class ListTable extends WP_List_Table {
*
* @return string
*/
public function column_cb( $item ) {
public function column_cb( $item ): string {
ob_start();
?>
<input
@ -248,7 +253,7 @@ class ListTable extends WP_List_Table {
*
* @return string
*/
public function column_source( $item ) {
public function column_source( $item ): string {
$log_file = sanitize_title( $item->get_basename() );
$single_file_url = add_query_arg(
array(
@ -280,7 +285,7 @@ class ListTable extends WP_List_Table {
*
* @return string
*/
public function column_created( $item ) {
public function column_created( $item ): string {
$timestamp = $item->get_created_timestamp();
return gmdate( 'Y-m-d', $timestamp );
@ -293,7 +298,7 @@ class ListTable extends WP_List_Table {
*
* @return string
*/
public function column_modified( $item ) {
public function column_modified( $item ): string {
$timestamp = $item->get_modified_timestamp();
return gmdate( 'Y-m-d H:i:s', $timestamp );
@ -306,7 +311,7 @@ class ListTable extends WP_List_Table {
*
* @return string
*/
public function column_size( $item ) {
public function column_size( $item ): string {
$size = $item->get_file_size();
return size_format( $size );

View File

@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\Admin\Logging;
@ -37,7 +38,7 @@ class PageController {
*/
final public function init(
FileController $file_controller
) {
): void {
$this->file_controller = $file_controller;
$this->init_hooks();
@ -48,7 +49,7 @@ class PageController {
*
* @return void
*/
private function init_hooks() {
private function init_hooks(): void {
self::add_action( 'load-woocommerce_page_wc-status', array( $this, 'setup_screen_options' ) );
self::add_action( 'load-woocommerce_page_wc-status', array( $this, 'handle_list_table_bulk_actions' ) );
}
@ -58,7 +59,7 @@ class PageController {
*
* @return string
*/
public function get_logs_tab_url() {
public function get_logs_tab_url(): string {
return add_query_arg(
array(
'page' => 'wc-status',
@ -73,7 +74,7 @@ class PageController {
*
* @return string
*/
public function get_default_handler() {
public function get_default_handler(): string {
$handler = Constants::get_constant( 'WC_LOG_HANDLER' );
if ( is_null( $handler ) || ! class_exists( $handler ) ) {
@ -88,7 +89,7 @@ class PageController {
*
* @return void
*/
public function render() {
public function render(): void {
$handler = $this->get_default_handler();
switch ( $handler ) {
@ -112,7 +113,7 @@ class PageController {
*
* @return void
*/
private function render_filev2( array $args = array() ) {
private function render_filev2( array $args = array() ): void {
$view = $args['view'] ?? '';
switch ( $view ) {
@ -131,7 +132,7 @@ class PageController {
*
* @return void
*/
private function render_file_list_page() {
private function render_file_list_page(): void {
$defaults = $this->get_query_param_defaults();
$params = $this->get_query_params();
@ -162,7 +163,7 @@ class PageController {
*
* @return string[]
*/
public function get_query_param_defaults() {
public function get_query_param_defaults(): array {
return array(
'order' => $this->file_controller::DEFAULTS_GET_FILES['order'],
'orderby' => $this->file_controller::DEFAULTS_GET_FILES['orderby'],
@ -176,7 +177,7 @@ class PageController {
*
* @return array
*/
public function get_query_params() {
public function get_query_params(): array {
$defaults = $this->get_query_param_defaults();
$params = filter_input_array(
INPUT_GET,
@ -216,7 +217,7 @@ class PageController {
*
* @return ListTable
*/
private function get_list_table() {
private function get_list_table(): ListTable {
if ( $this->list_table instanceof ListTable ) {
return $this->list_table;
}
@ -231,7 +232,7 @@ class PageController {
*
* @return void
*/
private function setup_screen_options() {
private function setup_screen_options(): void {
$params = $this->get_query_params();
if ( 'list_files' === $params['view'] ) {
@ -253,7 +254,7 @@ class PageController {
*
* @return void
*/
private function handle_list_table_bulk_actions() {
private function handle_list_table_bulk_actions(): void {
$action = $this->get_list_table()->current_action();
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized