phpcs cleanup

This commit is contained in:
Corey McKrill 2023-10-18 16:46:28 -07:00
parent 7be565bc47
commit d872ec5742
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
5 changed files with 71 additions and 36 deletions

View File

@ -2,24 +2,37 @@
namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2; namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
/**
* File class.
*
* An object representation of a single log file.
*/
class File { class File {
/** /**
* @var string The absolute path of the file. * The absolute path of the file.
*
* @var string
*/ */
protected $path; protected $path;
/** /**
* @var string The source property of the file, derived from the filename. * The source property of the file, derived from the filename.
*
* @var string
*/ */
protected $source; protected $source;
/** /**
* @var int The date the file was created, as a Unix timestamp, derived from the filename. * The date the file was created, as a Unix timestamp, derived from the filename.
*
* @var int
*/ */
protected $created; protected $created;
/** /**
* @var string The key property of the file, derived from the filename. * The key property of the file, derived from the filename.
*
* @var string
*/ */
protected $key; protected $key;
@ -125,6 +138,6 @@ class File {
* @return bool * @return bool
*/ */
public function delete() { public function delete() {
return @unlink( $this->path ); return @unlink( $this->path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
} }
} }

View File

@ -10,7 +10,9 @@ use WP_Error;
*/ */
class FileController { class FileController {
/** /**
* @const array Default values for arguments for the get_files method. * Default values for arguments for the get_files method.
*
* @const array
*/ */
public const DEFAULTS_GET_FILES = array( public const DEFAULTS_GET_FILES = array(
'offset' => 0, 'offset' => 0,
@ -21,7 +23,9 @@ class FileController {
); );
/** /**
* @var string The absolute path to the log directory. * The absolute path to the log directory.
*
* @var string
*/ */
private $log_directory; private $log_directory;
@ -51,7 +55,7 @@ class FileController {
public function get_files( array $args = array(), bool $count_only = false ) { public function get_files( array $args = array(), bool $count_only = false ) {
$args = wp_parse_args( $args, self::DEFAULTS_GET_FILES ); $args = wp_parse_args( $args, self::DEFAULTS_GET_FILES );
$pattern = $args['source'] . '*' . '.log'; $pattern = $args['source'] . '*.log';
$files = glob( $this->log_directory . $pattern ); $files = glob( $this->log_directory . $pattern );
if ( false === $files ) { if ( false === $files ) {
@ -90,7 +94,7 @@ class FileController {
$comparison = $set[0] <=> $set[1]; $comparison = $set[0] <=> $set[1];
} }
if ( $comparison !== 0 ) { if ( 0 !== $comparison ) {
break; break;
} }
} }
@ -101,7 +105,7 @@ class FileController {
switch ( $args['orderby'] ) { switch ( $args['orderby'] ) {
case 'created': case 'created':
$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$sort_sets = array( $sort_sets = array(
array( $a->get_created_timestamp(), $b->get_created_timestamp() ), array( $a->get_created_timestamp(), $b->get_created_timestamp() ),
array( $a->get_source(), $b->get_source() ), array( $a->get_source(), $b->get_source() ),
); );
@ -111,7 +115,7 @@ class FileController {
break; break;
case 'modified': case 'modified':
$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$sort_sets = array( $sort_sets = array(
array( $a->get_modified_timestamp(), $b->get_modified_timestamp() ), array( $a->get_modified_timestamp(), $b->get_modified_timestamp() ),
array( $a->get_source(), $b->get_source() ), array( $a->get_source(), $b->get_source() ),
); );
@ -121,7 +125,7 @@ class FileController {
break; break;
case 'source': case 'source':
$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$sort_sets = array( $sort_sets = array(
array( $a->get_source(), $b->get_source() ), array( $a->get_source(), $b->get_source() ),
array( $a->get_created_timestamp(), $b->get_created_timestamp() ), array( $a->get_created_timestamp(), $b->get_created_timestamp() ),
); );
@ -131,7 +135,7 @@ class FileController {
break; break;
case 'size': case 'size':
$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$sort_sets = array( $sort_sets = array(
array( $a->get_file_size(), $b->get_file_size() ), array( $a->get_file_size(), $b->get_file_size() ),
array( $a->get_source(), $b->get_source() ), array( $a->get_source(), $b->get_source() ),
); );

View File

@ -11,22 +11,30 @@ use WP_List_Table;
*/ */
class ListTable extends WP_List_Table { class ListTable extends WP_List_Table {
/** /**
* The user option key for saving the preferred number of files displayed per page.
* @const string * @const string
*/ */
public const PER_PAGE_USER_OPTION_KEY = 'woocommerce_logging_file_list_per_page'; public const PER_PAGE_USER_OPTION_KEY = 'woocommerce_logging_file_list_per_page';
/** /**
* Instance of FileController.
*
* @var FileController * @var FileController
*/ */
private $file_controller; private $file_controller;
/** /**
* Instance of PageController.
*
* @var PageController * @var PageController
*/ */
private $page_controller; private $page_controller;
/** /**
* ListTable class. * ListTable class.
*
* @param FileController $file_controller Instance of FileController.
* @param PageController $page_controller Instance of PageController.
*/ */
public function __construct( FileController $file_controller, PageController $page_controller ) { public function __construct( FileController $file_controller, PageController $page_controller ) {
$this->file_controller = $file_controller; $this->file_controller = $file_controller;
@ -76,7 +84,7 @@ class ListTable extends WP_List_Table {
/** /**
* Displays extra controls between bulk actions and pagination. * Displays extra controls between bulk actions and pagination.
* *
* @param string $which * @param string $which The location of the tablenav being rendered. 'top' or 'bottom'.
* *
* @return void * @return void
*/ */
@ -91,7 +99,7 @@ class ListTable extends WP_List_Table {
<select name="source" id="filter-by-source"> <select name="source" id="filter-by-source">
<option<?php selected( $current_source, '' ); ?> value=""><?php esc_html_e( 'All sources', 'woocommerce' ); ?></option> <option<?php selected( $current_source, '' ); ?> value=""><?php esc_html_e( 'All sources', 'woocommerce' ); ?></option>
<?php foreach ( $all_sources as $source ) : ?> <?php foreach ( $all_sources as $source ) : ?>
<option<?php selected( $current_source, $source ); ?> value="<?php echo esc_attr( $source ) ?>"> <option<?php selected( $current_source, $source ); ?> value="<?php echo esc_attr( $source ); ?>">
<?php echo esc_html( $source ); ?> <?php echo esc_html( $source ); ?>
</option> </option>
<?php endforeach; ?> <?php endforeach; ?>
@ -137,7 +145,7 @@ class ListTable extends WP_List_Table {
$this->file_controller::DEFAULTS_GET_FILES['per_page'] $this->file_controller::DEFAULTS_GET_FILES['per_page']
); );
$defaults = array( $defaults = array(
'per_page' => $per_page, 'per_page' => $per_page,
'offset' => ( $this->get_pagenum() - 1 ) * $per_page, 'offset' => ( $this->get_pagenum() - 1 ) * $per_page,
); );
@ -203,7 +211,7 @@ class ListTable extends WP_List_Table {
/** /**
* Render the checkbox column. * Render the checkbox column.
* *
* @param File $item * @param File $item The current log file being rendered.
* *
* @return string * @return string
*/ */
@ -220,8 +228,9 @@ class ListTable extends WP_List_Table {
<span class="screen-reader-text"> <span class="screen-reader-text">
<?php <?php
printf( printf(
// translators: 1. a date, 2. a slug-style name for a file.
esc_html__( 'Select the %1$s log file for %2$s', 'woocommerce' ), esc_html__( 'Select the %1$s log file for %2$s', 'woocommerce' ),
esc_html( date( get_option( 'date_format' ), $item->get_created_timestamp() ) ), esc_html( gmdate( get_option( 'date_format' ), $item->get_created_timestamp() ) ),
esc_html( $item->get_source() ) esc_html( $item->get_source() )
); );
?> ?>
@ -234,7 +243,7 @@ class ListTable extends WP_List_Table {
/** /**
* Render the source column. * Render the source column.
* *
* @param File $item * @param File $item The current log file being rendered.
* *
* @return string * @return string
*/ */
@ -258,33 +267,33 @@ class ListTable extends WP_List_Table {
/** /**
* Render the created column. * Render the created column.
* *
* @param File $item * @param File $item The current log file being rendered.
* *
* @return string * @return string
*/ */
public function column_created( $item ) { public function column_created( $item ) {
$timestamp = $item->get_created_timestamp(); $timestamp = $item->get_created_timestamp();
return date( 'Y-m-d', $timestamp ); return gmdate( 'Y-m-d', $timestamp );
} }
/** /**
* Render the modified column. * Render the modified column.
* *
* @param File $item * @param File $item The current log file being rendered.
* *
* @return string * @return string
*/ */
public function column_modified( $item ) { public function column_modified( $item ) {
$timestamp = $item->get_modified_timestamp(); $timestamp = $item->get_modified_timestamp();
return date( 'Y-m-d H:i:s', $timestamp ); return gmdate( 'Y-m-d H:i:s', $timestamp );
} }
/** /**
* Render the size column. * Render the size column.
* *
* @param File $item * @param File $item The current log file being rendered.
* *
* @return string * @return string
*/ */

View File

@ -4,4 +4,7 @@ namespace Automattic\WooCommerce\Internal\Admin\Logging;
use WC_Log_Handler_File; use WC_Log_Handler_File;
/**
* LogHandlerFileV2 class.
*/
class LogHandlerFileV2 extends WC_Log_Handler_File {} class LogHandlerFileV2 extends WC_Log_Handler_File {}

View File

@ -15,11 +15,15 @@ class PageController {
use AccessiblePrivateMethods; use AccessiblePrivateMethods;
/** /**
* Instance of FileController.
*
* @var FileController * @var FileController
*/ */
private $file_controller; private $file_controller;
/** /**
* Instance of ListTable.
*
* @var ListTable * @var ListTable
*/ */
private $list_table; private $list_table;
@ -27,7 +31,7 @@ class PageController {
/** /**
* Initialize dependencies. * Initialize dependencies.
* *
* @param FileController $file_controller * @param FileController $file_controller Instance of FileController.
* *
* @return void * @return void
*/ */
@ -177,7 +181,7 @@ class PageController {
$params = filter_input_array( $params = filter_input_array(
INPUT_GET, INPUT_GET,
array( array(
'view' => array( 'view' => array(
'filter' => FILTER_VALIDATE_REGEXP, 'filter' => FILTER_VALIDATE_REGEXP,
'options' => array( 'options' => array(
'regexp' => '/^(list_files|single_file)$/', 'regexp' => '/^(list_files|single_file)$/',
@ -185,20 +189,20 @@ class PageController {
), ),
), ),
'orderby' => array( 'orderby' => array(
'filter' => FILTER_VALIDATE_REGEXP, 'filter' => FILTER_VALIDATE_REGEXP,
'options' => array( 'options' => array(
'regexp' => '/^(created|modified|source|size)$/', 'regexp' => '/^(created|modified|source|size)$/',
'default' => $defaults['orderby'] 'default' => $defaults['orderby'],
), ),
), ),
'order' => array( 'order' => array(
'filter' => FILTER_VALIDATE_REGEXP, 'filter' => FILTER_VALIDATE_REGEXP,
'options' => array( 'options' => array(
'regexp' => '/^(asc|desc)$/i', 'regexp' => '/^(asc|desc)$/i',
'default' => $defaults['order'] 'default' => $defaults['order'],
), ),
), ),
'source' => FILTER_SANITIZE_STRING, 'source' => FILTER_SANITIZE_STRING,
), ),
false false
); );
@ -252,6 +256,8 @@ class PageController {
private function handle_list_table_bulk_actions() { private function handle_list_table_bulk_actions() {
$action = $this->get_list_table()->current_action(); $action = $this->get_list_table()->current_action();
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : $this->get_logs_tab_url();
if ( $action ) { if ( $action ) {
check_admin_referer( 'bulk-log-files' ); check_admin_referer( 'bulk-log-files' );
@ -264,7 +270,7 @@ class PageController {
switch ( $action ) { switch ( $action ) {
case 'delete': case 'delete':
$deleted = $this->file_controller->delete_files( $files ); $deleted = $this->file_controller->delete_files( $files );
$sendback = add_query_arg( 'deleted', $deleted, $sendback ); $sendback = add_query_arg( 'deleted', $deleted, $sendback );
break; break;
} }
@ -275,7 +281,7 @@ class PageController {
exit; exit;
} elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
$removable_args = array( '_wp_http_referer', '_wpnonce', 'action', 'action2', 'filter_action' ); $removable_args = array( '_wp_http_referer', '_wpnonce', 'action', 'action2', 'filter_action' );
wp_safe_redirect( remove_query_arg( $removable_args, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); wp_safe_redirect( remove_query_arg( $removable_args, $request_uri ) );
exit; exit;
} }
@ -292,7 +298,7 @@ class PageController {
printf( printf(
// translators: %s is a number of files. // translators: %s is a number of files.
esc_html( _n( '%s log file deleted.', '%s log files deleted.', $deleted, 'woocommerce' ) ), esc_html( _n( '%s log file deleted.', '%s log files deleted.', $deleted, 'woocommerce' ) ),
number_format_i18n( $deleted ) esc_html( number_format_i18n( $deleted ) )
); );
?> ?>
</p> </p>
@ -302,6 +308,6 @@ class PageController {
); );
} }
$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ); $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $request_uri );
} }
} }