Skip to content

Commit 4ddff76

Browse files
committed
Fixed Psalm suppressed errors.
1 parent 98b17bd commit 4ddff76

File tree

9 files changed

+25
-51
lines changed

9 files changed

+25
-51
lines changed

plugins/wpgraphql-logging/src/Admin/Settings/ConfigurationHelper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,15 @@ public function get_option_value( string $option_key, mixed $default_value = nul
164164
/**
165165
* Hook into WordPress to clear cache when settings are updated.
166166
* This should be called during plugin initialization.
167-
*
168-
* @psalm-suppress PossiblyInvalidArgument
169167
*/
170168
public static function init_cache_hooks(): void {
171169
$instance = self::get_instance();
172170
$option_key = $instance->get_option_key();
173171

174172
// Clear cache when the option is updated.
175-
add_action( "update_option_{$option_key}", [ $instance, 'clear_cache' ] );
176-
add_action( "add_option_{$option_key}", [ $instance, 'clear_cache' ] );
177-
add_action( "delete_option_{$option_key}", [ $instance, 'clear_cache' ] );
173+
add_action( "update_option_{$option_key}", [ $instance, 'clear_cache' ], 10, 0 );
174+
add_action( "add_option_{$option_key}", [ $instance, 'clear_cache' ], 10, 0 );
175+
add_action( "delete_option_{$option_key}", [ $instance, 'clear_cache' ], 10, 0 );
178176
}
179177

180178
/**

plugins/wpgraphql-logging/src/Admin/Settings/Fields/Field/AbstractSettingsField.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,15 @@ public function should_render_for_tab( string $tab_key ): bool {
5959
* @param array<string, mixed> $args The field arguments.
6060
*/
6161
public function add_settings_field( string $section, string $page, array $args ): void {
62-
/** @psalm-suppress InvalidArgument */
6362
add_settings_field(
6463
$this->get_id(),
6564
$this->title,
6665
[ $this, 'render_field_callback' ],
6766
$page,
6867
$section,
69-
array_merge(
70-
$args,
71-
[
72-
'class' => $this->css_class,
73-
'description' => $this->description,
74-
]
75-
)
68+
[
69+
'class' => $this->css_class,
70+
]
7671
);
7772
}
7873

plugins/wpgraphql-logging/src/Admin/Settings/Fields/Field/SelectField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function sanitize_field( $value ): mixed {
101101
* @return string The sanitized value.
102102
*/
103103
protected function sanitize_single_value( $value ): string {
104-
$sanitized_value = sanitize_text_field( (string) $value );
104+
$sanitized_value = sanitize_text_field( $value );
105105
return array_key_exists( $sanitized_value, $this->options ) ? $sanitized_value : '';
106106
}
107107

plugins/wpgraphql-logging/src/Admin/Settings/SettingsFormManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function sanitize_settings( ?array $new_input ): array {
8585
return $old_input;
8686
}
8787

88-
$tab_to_sanitize = (string) $tab[0];
88+
$tab_to_sanitize = $tab[0];
8989
if ( ! is_array( $new_input[ $tab_to_sanitize ] ) ) {
9090
return $old_input;
9191
}

plugins/wpgraphql-logging/src/Admin/SettingsPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function get_current_tab( array $tabs = [] ): string {
166166
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for tab navigation only, no form processing
167167
$tab = sanitize_text_field( $_GET['tab'] );
168168

169-
if ( ! is_string( $tab ) || '' === $tab ) {
169+
if ( '' === $tab ) {
170170
return $this->get_default_tab();
171171
}
172172

plugins/wpgraphql-logging/src/Admin/View/List/ListTable.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ public function __construct(
5656
* Prepare items for display.
5757
*
5858
* @phpcs:disable WordPress.Security.NonceVerification.Recommended
59-
*
60-
* @psalm-suppress PossiblyInvalidCast
6159
*/
6260
public function prepare_items(): void {
6361
if ( array_key_exists( 'orderby', $_REQUEST ) || array_key_exists( 'order', $_REQUEST ) ) {
@@ -78,9 +76,9 @@ public function prepare_items(): void {
7876

7977
$per_page = $this->get_items_per_page( 'logs_per_page', self::DEFAULT_PER_PAGE );
8078
$current_page = $this->get_pagenum();
81-
/** @psalm-suppress InvalidArgument */
82-
$where = $this->process_where( $_REQUEST );
83-
$total_items = $this->log_service->count_entities_by_where( $where );
79+
$request = $_REQUEST ?? [];
80+
$where = $this->process_where( $request );
81+
$total_items = $this->log_service->count_entities_by_where( $where );
8482

8583
$this->set_pagination_args(
8684
[
@@ -94,12 +92,12 @@ public function prepare_items(): void {
9492
'offset' => ( $current_page - 1 ) * $per_page,
9593
];
9694

97-
if ( array_key_exists( 'orderby', $_REQUEST ) ) {
98-
$args['orderby'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
95+
if ( isset( $_REQUEST['orderby'] ) && is_string( $_REQUEST['orderby'] ) ) {
96+
$args['orderby'] = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
9997
}
10098

101-
if ( array_key_exists( 'order', $_REQUEST ) ) {
102-
$args['order'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
99+
if ( isset( $_REQUEST['order'] ) && is_string( $_REQUEST['order'] ) ) {
100+
$args['order'] = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
103101
}
104102
$args['where'] = $where;
105103
$this->items = $this->log_service->find_entities_by_where( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
@@ -424,7 +422,7 @@ protected function format_code(string $code): string {
424422
/**
425423
* Process the where clauses for filtering.
426424
*
427-
* @param array<string, mixed> $request The request data.
425+
* @param array<int|string, mixed> $request The request data.
428426
*
429427
* @return array<string, string> The where clauses.
430428
*/

plugins/wpgraphql-logging/src/Admin/ViewLogsPage.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function setup(): void {
6565

6666
/**
6767
* Registers the settings page for the view logs.
68-
*
6968
*/
7069
public function register_settings_page(): void {
7170

@@ -92,7 +91,7 @@ public function register_settings_page(): void {
9291
add_action( 'load-' . $this->page_hook, [ $this, 'process_page_actions_before_rendering' ], 10, 0 );
9392

9493
// Enqueue scripts for the admin page.
95-
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ]);
94+
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
9695
}
9796

9897
/**
@@ -152,8 +151,9 @@ public function enqueue_admin_scripts( string $hook_suffix ): void {
152151
* Renders the admin page for the logs.
153152
*/
154153
public function render_admin_page(): void {
155-
/** @psalm-suppress PossiblyInvalidArgument */
156-
$action = sanitize_text_field( $_REQUEST['action'] ?? 'link' ); // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
154+
$action = isset( $_REQUEST['action'] ) && is_string( $_REQUEST['action'] )
155+
? sanitize_text_field( $_REQUEST['action'] )
156+
: 'link'; // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
157157

158158
switch ( $action ) {
159159
case 'view':

plugins/wpgraphql-logging/src/Logger/Handlers/WordPressDatabaseHandler.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class WordPressDatabaseHandler extends AbstractProcessingHandler {
2828
protected function write( LogRecord $record ): void {
2929
try {
3030
$log_service = LogStoreService::get_log_service();
31+
$level = $record->level;
3132
$log_service->create_log_entity( // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
3233
$record->channel,
33-
$record->level->value,
34-
$this->get_record_name( $record ),
34+
$level->value,
35+
$level->name,
3536
$record->message,
3637
$record->context ?? [],
3738
$record->extra ?? []
@@ -43,22 +44,4 @@ protected function write( LogRecord $record ): void {
4344
}
4445
}
4546
}
46-
47-
/**
48-
* Gets the name of the log record.
49-
*
50-
* @param \Monolog\LogRecord $record The log record.
51-
*
52-
* @return string The name of the log record.
53-
*/
54-
protected function get_record_name( LogRecord $record ): string {
55-
56-
/**
57-
* @psalm-suppress InvalidCast
58-
*/
59-
$name = (string) $record->level->getName(); // @phpstan-ignore-line
60-
$default = 'INFO';
61-
62-
return $name ?: $default; // @phpstan-ignore-line
63-
}
6447
}

plugins/wpgraphql-logging/tests/wpunit/Logger/Handlers/WordPressDatabaseHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function test_write_method_saves_log_to_database(): void
102102
$this->assertNotNull($saved_row, 'A log entry should have been created in the database.');
103103
$this->assertEquals($log_data['channel'], $saved_row['channel']);
104104
$this->assertEquals($log_data['level']->value, $saved_row['level']);
105-
$this->assertEquals($log_data['level']->getName(), $saved_row['level_name']);
105+
$this->assertEquals($log_data['level']->name, $saved_row['level_name']);
106106
$this->assertEquals($log_data['message'], $saved_row['message']);
107107

108108
// Compare the JSON-decoded context and extra fields.

0 commit comments

Comments
 (0)