From 070132d48b10f49eaed0218978441689666bf94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 17 May 2020 10:40:51 +0200 Subject: [PATCH 1/5] [exmaple] Fill list of HTTP status codes This is theoretical! --- src/Page.php | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/Page.php b/src/Page.php index e78897b..2d8468d 100755 --- a/src/Page.php +++ b/src/Page.php @@ -115,37 +115,27 @@ class Page extends Model { /** * Get the number of pages for each group of status codes, e.g. 1xx, 2xx, 3xx * - * @return mixed[] Assoc. array of status code to number of pages, e.g. '2' => 183 + * @return array Array of status code to number of pages. */ public static function get_http_status_codes_summary() { global $wpdb; - $query = 'SELECT LEFT(http_status_code, 1) AS status, COUNT(*) AS count'; + $query = 'SELECT LEFT(http_status_code, 1) AS code, COUNT(*) AS count'; $query .= ' FROM ' . self::table_name(); - $query .= ' GROUP BY LEFT(http_status_code, 1)'; - $query .= ' ORDER BY status'; + $query .= ' GROUP BY code'; - $rows = $wpdb->get_results( - $query, - ARRAY_A - ); + $rows = $wpdb->get_results( $query, \ARRAY_A ); - $http_codes = [ - '1' => 0, - '2' => 0, - '3' => 0, - '4' => 0, - '5' => 0, - '6' => 0, - '7' => 0, - '8' => 0, - ]; - - foreach ( $rows as $row ) { - $http_codes[ $row['status'] ] = $row['count']; - } + $http_status_codes = array_fill( 1, 8, 0 ); + + array_walk( + $http_status_codes, + function ( $count, $code ) use ( $rows ) { + return $rows[ $code ] ?? $count; + } + ); - return $http_codes; + return $http_status_codes; } /** From a1ecefde3d9c008761ab5c25259bf5299302e9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 17 May 2020 10:44:14 +0200 Subject: [PATCH 2/5] wpdb returns strings --- src/Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Page.php b/src/Page.php index 2d8468d..125c0f1 100755 --- a/src/Page.php +++ b/src/Page.php @@ -131,7 +131,7 @@ public static function get_http_status_codes_summary() { array_walk( $http_status_codes, function ( $count, $code ) use ( $rows ) { - return $rows[ $code ] ?? $count; + return $rows[ (string) $code ] ?? $count; } ); From 376d528c6e763fd00f28e144e26a388d65f135db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 17 May 2020 10:49:52 +0200 Subject: [PATCH 3/5] Add missing array_column --- src/Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Page.php b/src/Page.php index 125c0f1..6c510cf 100755 --- a/src/Page.php +++ b/src/Page.php @@ -124,7 +124,7 @@ public static function get_http_status_codes_summary() { $query .= ' FROM ' . self::table_name(); $query .= ' GROUP BY code'; - $rows = $wpdb->get_results( $query, \ARRAY_A ); + $rows = array_column( $wpdb->get_results( $query, \ARRAY_A ), 'count', 'code' ); $http_status_codes = array_fill( 1, 8, 0 ); From d8312bfe249b66526bd01038ad22143ec5ce8dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 17 May 2020 11:19:48 +0200 Subject: [PATCH 4/5] Make HTTP status code counting much simpler --- src/Page.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Page.php b/src/Page.php index 6c510cf..0762b8f 100755 --- a/src/Page.php +++ b/src/Page.php @@ -126,16 +126,9 @@ public static function get_http_status_codes_summary() { $rows = array_column( $wpdb->get_results( $query, \ARRAY_A ), 'count', 'code' ); - $http_status_codes = array_fill( 1, 8, 0 ); + $http_status_codes = array_fill_keys( ['1', '2', '3', '4', '5', '6', '7', '8'], 0 ); - array_walk( - $http_status_codes, - function ( $count, $code ) use ( $rows ) { - return $rows[ (string) $code ] ?? $count; - } - ); - - return $http_status_codes; + return array_merge( $http_status_codes, $rows ); } /** From 16867ee2fea17a6210661e8d75c8f30d2da629c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 17 May 2020 11:21:25 +0200 Subject: [PATCH 5/5] Revert string indices --- src/Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Page.php b/src/Page.php index 0762b8f..386f770 100755 --- a/src/Page.php +++ b/src/Page.php @@ -115,7 +115,7 @@ class Page extends Model { /** * Get the number of pages for each group of status codes, e.g. 1xx, 2xx, 3xx * - * @return array Array of status code to number of pages. + * @return array Array of status code to number of pages. */ public static function get_http_status_codes_summary() { global $wpdb;