From f361946091e397cf29e794b76103c3cbb40a51a4 Mon Sep 17 00:00:00 2001 From: Mark Kelnar <749603+markkelnar@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:31:10 -0500 Subject: [PATCH 1/3] phpstand level 7 --- composer.lock | 36 ++++++++++++++++++------------------ phpstan.neon.dist | 2 +- src/Cache/Results.php | 2 +- src/Document.php | 6 +++--- src/Document/MaxAge.php | 6 ++++-- src/Utils.php | 7 ++++--- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/composer.lock b/composer.lock index 6aaf7cc6..df648e7a 100644 --- a/composer.lock +++ b/composer.lock @@ -1644,16 +1644,16 @@ }, { "name": "illuminate/collections", - "version": "v10.16.1", + "version": "v10.17.0", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", - "reference": "07ec467c42b90ec6e7e2faec381e3457dcfd5571" + "reference": "bc030c443fc43776070b96d66257c3b29153f30a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/collections/zipball/07ec467c42b90ec6e7e2faec381e3457dcfd5571", - "reference": "07ec467c42b90ec6e7e2faec381e3457dcfd5571", + "url": "https://api.github.com/repos/illuminate/collections/zipball/bc030c443fc43776070b96d66257c3b29153f30a", + "reference": "bc030c443fc43776070b96d66257c3b29153f30a", "shasum": "" }, "require": { @@ -1695,11 +1695,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2023-07-24T14:56:19+00:00" + "time": "2023-07-27T15:19:34+00:00" }, { "name": "illuminate/conditionable", - "version": "v10.16.1", + "version": "v10.17.0", "source": { "type": "git", "url": "https://github.com/illuminate/conditionable.git", @@ -1745,16 +1745,16 @@ }, { "name": "illuminate/contracts", - "version": "v10.16.1", + "version": "v10.17.0", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", - "reference": "ec47d1aa1a1b1a679d8553836b417343881b8215" + "reference": "eb1a7e72e159136a832f2c0467de5570bdc208ae" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/contracts/zipball/ec47d1aa1a1b1a679d8553836b417343881b8215", - "reference": "ec47d1aa1a1b1a679d8553836b417343881b8215", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/eb1a7e72e159136a832f2c0467de5570bdc208ae", + "reference": "eb1a7e72e159136a832f2c0467de5570bdc208ae", "shasum": "" }, "require": { @@ -1789,11 +1789,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2023-06-27T14:35:49+00:00" + "time": "2023-07-26T21:27:34+00:00" }, { "name": "illuminate/macroable", - "version": "v10.16.1", + "version": "v10.17.0", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -1839,16 +1839,16 @@ }, { "name": "illuminate/support", - "version": "v10.16.1", + "version": "v10.17.0", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", - "reference": "40b87e80d150339bc186256576e11ea3be3d0ff5" + "reference": "7549dd081cc45c742b7d97064b64cf67fab4c7b6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/support/zipball/40b87e80d150339bc186256576e11ea3be3d0ff5", - "reference": "40b87e80d150339bc186256576e11ea3be3d0ff5", + "url": "https://api.github.com/repos/illuminate/support/zipball/7549dd081cc45c742b7d97064b64cf67fab4c7b6", + "reference": "7549dd081cc45c742b7d97064b64cf67fab4c7b6", "shasum": "" }, "require": { @@ -1860,7 +1860,7 @@ "illuminate/conditionable": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", - "nesbot/carbon": "^2.62.1", + "nesbot/carbon": "^2.67", "php": "^8.1", "voku/portable-ascii": "^2.0" }, @@ -1906,7 +1906,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2023-07-25T19:33:34+00:00" + "time": "2023-07-31T15:02:41+00:00" }, { "name": "ivome/graphql-relay-php", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 26605199..16672ff5 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 6 + level: 7 inferPrivatePropertyTypeFromConstructor: true checkMissingIterableValueType: false stubFiles: diff --git a/src/Cache/Results.php b/src/Cache/Results.php index c5bb8021..27b2e850 100644 --- a/src/Cache/Results.php +++ b/src/Cache/Results.php @@ -90,7 +90,7 @@ public function add_cache_key_to_response_extensions( if ( is_array( $response ) ) { $response['extensions']['graphqlSmartCache']['graphqlObjectCache'] = $message; - } if ( is_object( $response ) ) { + } if ( is_object( $response ) && property_exists( $response, 'extensions' ) ) { $response->extensions['graphqlSmartCache']['graphqlObjectCache'] = $message; } } diff --git a/src/Document.php b/src/Document.php index a47501cc..d0c01f59 100644 --- a/src/Document.php +++ b/src/Document.php @@ -183,7 +183,7 @@ public function graphql_mutation_insert( $post_object, $filtered_input, $input, // Remove the existing/old alias terms before update $terms = wp_get_post_terms( $post_object['postObjectId'], self::ALIAS_TAXONOMY_NAME ); - if ( $terms ) { + if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { wp_remove_object_terms( $post_object['postObjectId'], $term->term_id, self::ALIAS_TAXONOMY_NAME ); wp_delete_term( $term->term_id, self::ALIAS_TAXONOMY_NAME ); @@ -353,7 +353,7 @@ public function after_updated_cb( $post_ID, $post_after, $post_before ) { // If the old query string hash is assigned to this post, delete it $terms = wp_get_post_terms( $post_ID, self::ALIAS_TAXONOMY_NAME ); - if ( $terms ) { + if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { if ( $old_query_id === $term->name ) { wp_remove_object_terms( $post_ID, $term->term_id, self::ALIAS_TAXONOMY_NAME ); @@ -469,7 +469,7 @@ public function delete_post_cb( $post_id ) { */ public function delete_term( $post_id ) { $terms = wp_get_object_terms( $post_id, self::ALIAS_TAXONOMY_NAME ); - if ( $terms ) { + if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { wp_delete_term( $term->term_id, self::ALIAS_TAXONOMY_NAME ); } diff --git a/src/Document/MaxAge.php b/src/Document/MaxAge.php index 8bfb647e..33eeb845 100644 --- a/src/Document/MaxAge.php +++ b/src/Document/MaxAge.php @@ -152,8 +152,10 @@ public function get( $post_id ) { if ( is_wp_error( $item ) ) { return $item; } - - return property_exists( $item[0], 'name' ) ? $item[0]->name : null; + if ( ! $item || ! property_exists( $item[0], 'name' ) ) { + return null; + } + return $item[0]->name; } /** diff --git a/src/Utils.php b/src/Utils.php index 5bda6028..eee4a32c 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -14,7 +14,7 @@ class Utils { * @param string|array $type * @param string $taxonomy * - * @return \WP_Post|bool false when not exist + * @return \WP_Post|false false when not exist */ public static function getPostByTermName( $query_id, $type, $taxonomy ) { $wp_query = new \WP_Query( @@ -32,13 +32,14 @@ public static function getPostByTermName( $query_id, $type, $taxonomy ) { ], ] ); - $posts = $wp_query->get_posts(); + // returns an array of post objects. + $posts = $wp_query->get_posts(); if ( empty( $posts ) ) { return false; } $post = array_pop( $posts ); - if ( ! $post->ID ) { + if ( ! ( $post instanceof \WP_Post ) || ! $post->ID ) { return false; } From 6b9b29351cea113c70aaf8bc8e82c6460f8f9510 Mon Sep 17 00:00:00 2001 From: Mark Kelnar <749603+markkelnar@users.noreply.github.com> Date: Wed, 2 Aug 2023 10:05:48 -0500 Subject: [PATCH 2/3] phpstan level 7 changes --- composer.lock | 12 +++++------ src/Admin/Editor.php | 41 ++++++++++++++++++++++++++------------ src/AdminErrors.php | 18 ++++++++++------- src/Cache/Collection.php | 4 ++++ src/Cache/Invalidation.php | 16 ++++++++++++--- src/Cache/Query.php | 11 ++++++++-- 6 files changed, 71 insertions(+), 31 deletions(-) diff --git a/composer.lock b/composer.lock index df648e7a..ab01a5d9 100644 --- a/composer.lock +++ b/composer.lock @@ -4293,16 +4293,16 @@ }, { "name": "sebastian/global-state", - "version": "5.0.5", + "version": "5.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2" + "reference": "bde739e7565280bda77be70044ac1047bc007e34" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/0ca8db5a5fc9c8646244e629625ac486fa286bf2", - "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bde739e7565280bda77be70044ac1047bc007e34", + "reference": "bde739e7565280bda77be70044ac1047bc007e34", "shasum": "" }, "require": { @@ -4345,7 +4345,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.5" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.6" }, "funding": [ { @@ -4353,7 +4353,7 @@ "type": "github" } ], - "time": "2022-02-14T08:28:10+00:00" + "time": "2023-08-02T09:26:13+00:00" }, { "name": "sebastian/lines-of-code", diff --git a/src/Admin/Editor.php b/src/Admin/Editor.php index 4129360f..9234a0b4 100644 --- a/src/Admin/Editor.php +++ b/src/Admin/Editor.php @@ -170,18 +170,21 @@ public static function grant_input_box_cb( $post ) { checked( $value, Grant::USE_DEFAULT, false ) ); $html .= '
'; + + /** @var array[] */ + $allowed_html = [ + 'input' => [ + 'type' => true, + 'id' => true, + 'name' => true, + 'value' => true, + 'checked' => true, + ], + 'br' => true, + ]; echo wp_kses( $html, - [ - 'input' => [ - 'type' => true, - 'id' => true, - 'name' => true, - 'value' => true, - 'checked' => true, - ], - 'br' => true, - ] + $allowed_html ); } @@ -196,9 +199,21 @@ public static function maxage_input_box_cb( $post ) { $max_age = new MaxAge(); $value = $max_age->get( $post->ID ); - $value = absint( $value ) ? $value : 0; - $html = sprintf( '', $value ); - $html .= '
'; + + if ( is_wp_error( $value ) ) { + AdminErrors::add_message( + sprintf( + __( 'Invalid max age %s.', 'wp-graphql-smart-cache' ), + $value->get_error_message() + ) + ); + $value = 0; + } else { + $value = absint( $value ) ? $value : 0; + } + + $html = sprintf( '', $value ); + $html .= '
'; echo wp_kses( $html, [ diff --git a/src/AdminErrors.php b/src/AdminErrors.php index f66bc234..48a2fa28 100644 --- a/src/AdminErrors.php +++ b/src/AdminErrors.php @@ -45,15 +45,19 @@ public function display_validation_messages() { foreach ( $error_messages as $message ) { $html = sprintf( '

%s

', $message ); + + /** @var array[] */ + $allowed_html = [ + 'div' => [ + 'id' => true, + 'class' => true, + ], + 'p' => true, + ]; + echo wp_kses( $html, - [ - 'div' => [ - 'id' => true, - 'class' => true, - ], - 'p' => true, - ] + $allowed_html ); } diff --git a/src/Cache/Collection.php b/src/Cache/Collection.php index b508a520..b278a6ef 100644 --- a/src/Cache/Collection.php +++ b/src/Cache/Collection.php @@ -75,6 +75,10 @@ public function save_query_mapping_cb( $request_key = $this->build_key( $query_id, $query, $variables, $operation ); + if ( false === $request_key ) { + return; + } + // get the runtime nodes from the query analyzer $runtime_nodes = $request->get_query_analyzer()->get_runtime_nodes() ?: []; $list_types = $request->get_query_analyzer()->get_list_types() ?: []; diff --git a/src/Cache/Invalidation.php b/src/Cache/Invalidation.php index 7fa7dc6d..ce7ab059 100644 --- a/src/Cache/Invalidation.php +++ b/src/Cache/Invalidation.php @@ -449,6 +449,10 @@ public function on_saved_term_cb( $term_id, $tt_id, $taxonomy, $update, $args = $tax_object = get_taxonomy( $taxonomy ); + if ( false === $tax_object ) { + return; + } + // Delete the cached results associated with this post/key $this->purge_nodes( 'term', $term->term_id, 'term_saved' ); @@ -479,6 +483,10 @@ public function on_deleted_term_relationship_cb( $object_id, $tt_id, $taxonomy ) $tax_object = get_taxonomy( $taxonomy ); + if ( false === $tax_object ) { + return; + } + // Delete the cached results associated with this post/key $this->purge_nodes( 'term', $term->term_id, 'term_relationship_deleted' ); $type_name = strtolower( $tax_object->graphql_single_name ); @@ -584,7 +592,7 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post // Purge the terms associated with the node $terms = wp_get_object_terms( $post->ID, \WPGraphQL::get_allowed_taxonomies() ); - if ( ! empty( $terms ) ) { + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { array_map( function ( $term ) use ( $post ) { if ( ! $term instanceof WP_Term ) { @@ -608,7 +616,7 @@ function ( $term ) use ( $post ) { if ( 'DELETE' === $action_type ) { $terms = wp_get_object_terms( $post->ID, \WPGraphQL::get_allowed_taxonomies() ); - if ( ! empty( $terms ) ) { + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { array_map( function ( $term ) use ( $post ) { if ( ! $term instanceof WP_Term ) { @@ -813,7 +821,9 @@ public function on_update_nav_menu_cb( $menu_id ) { $menu = get_term_by( 'id', absint( $menu_id ), 'nav_menu' ); // menus have a term:id relay global ID, as they use the term loader - $this->purge_nodes( 'term', $menu->term_id, 'updated_nav_menu' ); + if ( $menu instanceof WP_Term ) { + $this->purge_nodes( 'term', $menu->term_id, 'updated_nav_menu' ); + } } /** diff --git a/src/Cache/Query.php b/src/Cache/Query.php index 9a3b5968..ec5cfdca 100644 --- a/src/Cache/Query.php +++ b/src/Cache/Query.php @@ -9,6 +9,7 @@ use WPGraphQL\SmartCache\Document; use WPGraphQL\SmartCache\Storage\Transient; use WPGraphQL\SmartCache\Storage\WpCache; +use WPGraphQL\SmartCache\Storage\Ephemeral; class Query { @@ -17,7 +18,7 @@ class Query { /** * The storage object for the actual system of choice transient, database, object, memory, etc * - * @var object + * @var WpCache|Transient|Ephemeral **/ public static $storage = null; @@ -69,7 +70,13 @@ public function build_key( $query_id, $query, $variables = null, $operation = nu 'user' => $user->ID, ]; - return hash( 'sha256', wp_json_encode( $parts ) ); + $parts_string = wp_json_encode( $parts ); + + if ( false === $parts_string ) { + return false; + } + + return hash( 'sha256', $parts_string ); } /** From d6c0308cc7aceef5a63f176951c71a6028e40266 Mon Sep 17 00:00:00 2001 From: Mark Kelnar <749603+markkelnar@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:47:53 -0500 Subject: [PATCH 3/3] phpstan changes for level 7 --- composer.lock | 18 +++++++++--------- src/Admin/Editor.php | 8 ++++++-- src/Cache/Invalidation.php | 13 ++++++++++--- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/composer.lock b/composer.lock index ab01a5d9..20ae4a3c 100644 --- a/composer.lock +++ b/composer.lock @@ -1644,16 +1644,16 @@ }, { "name": "illuminate/collections", - "version": "v10.17.0", + "version": "v10.17.1", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", - "reference": "bc030c443fc43776070b96d66257c3b29153f30a" + "reference": "66ff5aab0dd10659aff0efe3ff101819db192dfe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/collections/zipball/bc030c443fc43776070b96d66257c3b29153f30a", - "reference": "bc030c443fc43776070b96d66257c3b29153f30a", + "url": "https://api.github.com/repos/illuminate/collections/zipball/66ff5aab0dd10659aff0efe3ff101819db192dfe", + "reference": "66ff5aab0dd10659aff0efe3ff101819db192dfe", "shasum": "" }, "require": { @@ -1695,11 +1695,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2023-07-27T15:19:34+00:00" + "time": "2023-08-02T14:57:32+00:00" }, { "name": "illuminate/conditionable", - "version": "v10.17.0", + "version": "v10.17.1", "source": { "type": "git", "url": "https://github.com/illuminate/conditionable.git", @@ -1745,7 +1745,7 @@ }, { "name": "illuminate/contracts", - "version": "v10.17.0", + "version": "v10.17.1", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", @@ -1793,7 +1793,7 @@ }, { "name": "illuminate/macroable", - "version": "v10.17.0", + "version": "v10.17.1", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -1839,7 +1839,7 @@ }, { "name": "illuminate/support", - "version": "v10.17.0", + "version": "v10.17.1", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", diff --git a/src/Admin/Editor.php b/src/Admin/Editor.php index de38ad7f..48e9438c 100644 --- a/src/Admin/Editor.php +++ b/src/Admin/Editor.php @@ -50,7 +50,9 @@ public function validate_before_save_cb( $data, $post ) { $existing_post = get_post( $post['ID'] ); // Overwrite new/invalid query with previous working query, or empty - $data['post_content'] = $existing_post->post_content; + if ( $existing_post ) { + $data['post_content'] = $existing_post->post_content; + } AdminErrors::add_message( $e->getMessage() ); } @@ -286,7 +288,9 @@ public function make_excerpt_column_sortable_in_admin_cb( $columns ) { * @return array */ public function wp_editor_settings( $settings, $editor_id ) { - if ( 'content' === $editor_id && Document::TYPE_NAME === get_current_screen()->post_type ) { + $screen = get_current_screen(); + + if ( $screen && 'content' === $editor_id && Document::TYPE_NAME === $screen->post_type ) { $settings['tinymce'] = false; $settings['quicktags'] = false; $settings['media_buttons'] = false; diff --git a/src/Cache/Invalidation.php b/src/Cache/Invalidation.php index ce7ab059..29105948 100644 --- a/src/Cache/Invalidation.php +++ b/src/Cache/Invalidation.php @@ -128,7 +128,7 @@ public function init() { /** * Return a list of ignored meta keys * - * @return array|null + * @return array */ public static function get_ignored_meta_keys() { if ( null !== self::$ignored_meta_keys ) { @@ -542,6 +542,10 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post $post_type_object = get_post_type_object( $post->post_type ); + if ( ! $post_type_object instanceof \WP_Post_Type ) { + return; + } + // If the post type is not public and not publicly queryable // don't track it if ( false === $post_type_object->public && false === $post_type_object->publicly_queryable ) { @@ -578,8 +582,7 @@ public function on_transition_post_status_cb( $new_status, $old_status, WP_Post $action_type = 'CREATE'; } - $post_type_object = get_post_type_object( $post->post_type ); - $type_name = $post_type_object instanceof \WP_Post_Type ? strtolower( $post_type_object->graphql_single_name ) : $post_type_object; + $type_name = strtolower( $post_type_object->graphql_single_name ); // if we create a post // we need to purge lists of the type @@ -731,6 +734,10 @@ public function on_postmeta_change_cb( $meta_id, $post_id, $meta_key, $meta_valu $post_type_object = get_post_type_object( $post->post_type ); + if ( ! $post_type_object instanceof \WP_Post_Type ) { + return; + } + // If the post type is not public and not publicly queryable // don't track it if ( false === $post_type_object->public && false === $post_type_object->publicly_queryable ) {