Skip to content

Commit

Permalink
Merge pull request #241 from markkelnar/phpstan-level-5
Browse files Browse the repository at this point in the history
Phpstan level 5,6
  • Loading branch information
markkelnar authored Aug 2, 2023
2 parents 9776247 + c83fe52 commit 4e00e3a
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 30 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 4
level: 6
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
stubFiles:
Expand Down
47 changes: 44 additions & 3 deletions src/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

class Editor {

/**
* @return void
*/
public function admin_init() {
add_filter( 'wp_insert_post_data', [ $this, 'validate_before_save_cb' ], 10, 2 );
add_action( sprintf( 'save_post_%s', Document::TYPE_NAME ), [ $this, 'save_document_cb' ], 10, 2 );
Expand All @@ -34,6 +37,10 @@ public function admin_init() {

/**
* If existing post is edited, verify query string in content is valid graphql
*
* @param array $data An array of slashed, sanitized, and processed post data.
* @param array $post An array of sanitized (and slashed) but otherwise unmodified post data.
* @return array
*/
public function validate_before_save_cb( $data, $post ) {
try {
Expand All @@ -50,6 +57,12 @@ public function validate_before_save_cb( $data, $post ) {
return $data;
}

/**
* On save, validate the form data.
*
* @param int $post_id post id
* @return void|bool
*/
public function is_valid_form( $post_id ) {
if ( empty( $_POST ) ) {
return;
Expand Down Expand Up @@ -97,8 +110,12 @@ public function is_valid_form( $post_id ) {
}

/**
* When a post is saved, sanitize and store the data.
*/
* When a post is saved, sanitize and store the data.
*
* @param int $post_id Post ID.
* @param \WP_Post $post Post object.
* @return void
*/
public function save_document_cb( $post_id, $post ) {
if ( ! $this->is_valid_form( $post_id ) ) {
AdminErrors::add_message( 'Something is wrong with the form data' );
Expand All @@ -119,14 +136,17 @@ public function save_document_cb( $post_id, $post ) {
$data = sanitize_text_field( wp_unslash( $_POST['graphql_query_maxage'] ) );
$max_age->save( $post_id, $data );
} catch ( SyntaxError $e ) {
AdminErrors::add_message( 'Did not save invalid graphql query string. ' . $post['post_content'] );
AdminErrors::add_message( 'Did not save invalid graphql query string. ' . $post->post_content );
} catch ( RequestError $e ) {
AdminErrors::add_message( $e->getMessage() );
}
}

/**
* Draw the input field for the post edit
*
* @param \WP_Post $post Post object.
* @return void
*/
public static function grant_input_box_cb( $post ) {
wp_nonce_field( 'graphql_query_grant', 'savedquery_grant_noncename' );
Expand Down Expand Up @@ -167,6 +187,9 @@ public static function grant_input_box_cb( $post ) {

/**
* Draw the input field for the post edit
*
* @param \WP_Post $post Post object.
* @return void
*/
public static function maxage_input_box_cb( $post ) {
wp_nonce_field( 'graphql_query_maxage', 'savedquery_maxage_noncename' );
Expand Down Expand Up @@ -211,24 +234,42 @@ public function translate_excerpt_text_cb( $string ) {

/**
* Enable excerpt as the description.
*
* @param string[] $columns An associative array of column headings.
* @return array
*/
public function add_description_column_to_admin_cb( $columns ) {
// Use 'description' as the text the user sees
$columns['excerpt'] = __( 'Description', 'wp-graphql-smart-cache' );
return $columns;
}

/**
* @param string $column The name of the column to display.
* @param int $post_id The current post ID.
* @return void
*/
public function fill_excerpt_content_cb( $column, $post_id ) {
if ( 'excerpt' === $column ) {
echo esc_html( get_the_excerpt( $post_id ) );
}
}

/**
* @param array $columns An array of sortable columns.
* @return array
*/
public function make_excerpt_column_sortable_in_admin_cb( $columns ) {
$columns['excerpt'] = true;
return $columns;
}

/**
* @param array $settings Array of editor arguments.
* @param string $editor_id Unique editor identifier, e.g. 'content'. Accepts 'classic-block'
* when called from block editor's Classic block.
* @return array
*/
public function wp_editor_settings( $settings, $editor_id ) {
if ( 'content' === $editor_id && Document::TYPE_NAME === get_current_screen()->post_type ) {
$settings['tinymce'] = false;
Expand Down
20 changes: 18 additions & 2 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class Settings {

// set this to true to see these in wp-admin
/**
* Set this to true to see these in wp-admin
*
* @return bool
*/
public static function show_in_admin() {
$display_admin = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'editor_display', false, 'graphql_persisted_queries_section' ) : false;
return ( 'on' === $display_admin );
Expand Down Expand Up @@ -60,16 +64,28 @@ public static function purge_logging_enabled() {
return ( 'on' === $option );
}

// Date/Time of the last time purge all happened through admin.
/**
* Date/Time of the last time purge all happened through admin.
*
* @return string|false
*/
public static function caching_purge_timestamp() {
return function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'purge_all_timestamp', false, 'graphql_cache_section' ) : false;
}

/**
* The graphql url endpoint with leading slash.
*
* @return string
*/
public static function graphql_endpoint() {
$path = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'graphql_endpoint', 'graphql', 'graphql_general_settings' ) : 'graphql';
return '/' . $path;
}

/**
* @return void
*/
public function init() {
// Add to the wp-graphql admin settings page
add_action(
Expand Down
10 changes: 10 additions & 0 deletions src/AdminErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ class AdminErrors {
const TRANSIENT_NAME = 'graphql_save_graphql_query_validation_error_messages';
const MESSAGE_TTL_SECONDS = 60;

/**
* @return void
*/
public function init() {
add_action( 'admin_notices', [ $this, 'display_validation_messages' ] );
}

/**
* @param string $message
* @return void
*/
public static function add_message( $message ) {
set_transient( self::TRANSIENT_NAME, [ $message ], self::MESSAGE_TTL_SECONDS );
}

/**
* @return void
*/
public function display_validation_messages() {
$screen = get_current_screen();
if ( $screen && Document::TYPE_NAME !== $screen->post_type ) {
Expand Down
6 changes: 5 additions & 1 deletion src/Cache/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

class Collection extends Query {

// initialize the cache collection
/**
* Initialize the cache collection
*
* @return void
*/
public function init() {
add_action( 'graphql_return_response', [ $this, 'save_query_mapping_cb' ], 10, 8 );
parent::init();
Expand Down
38 changes: 36 additions & 2 deletions src/Cache/Invalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ class Invalidation {
* Instantiate the Cache Invalidation class
*
* @param Collection $collection
* @return void
*/
public function __construct( Collection $collection ) {
$this->collection = $collection;
}

/**
* Initialize the actions to listen for
*
* @return void
*/
public function init() {
// @phpcs:ignore
Expand Down Expand Up @@ -200,6 +203,8 @@ public function should_track_meta( $meta_key, $meta_value, $object ) {
*
* @param string $key An identifiers for data stored in memory.
* @param string $event The event that caused the purge
*
* @return void
*/
public function purge( $key, $event = 'undefined event' ) {

Expand All @@ -223,6 +228,8 @@ public function purge( $key, $event = 'undefined event' ) {
* @param string $id_prefix The type name specific to the id to form the "global ID" that is unique among all types
* @param mixed|string|int $id The node entity identifier
* @param string $event The event that caused the purge
*
* @return void
*/
public function purge_nodes( $id_prefix, $id, $event = 'unknown event' ) {
if ( ! method_exists( Relay::class, 'toGlobalId' ) ) {
Expand Down Expand Up @@ -328,6 +335,8 @@ public function on_deleted_post_cb( $post_id, WP_Post $post ) {
* @param int $term_id Term ID.
* @param int $tt_id Taxonomy term ID.
* @param string $taxonomy Taxonomy name.
*
* @return void
*/
public function on_created_term_cb( $term_id, $tt_id, $taxonomy ) {
$tax_object = get_taxonomy( $taxonomy );
Expand Down Expand Up @@ -364,6 +373,8 @@ public function is_taxonomy_tracked( $taxonomy ) {
* @param int $tt_id Taxonomy term ID.
* @param string $taxonomy Taxonomy name.
* @param mixed $deleted_term Deleted term object.
*
* @return void
*/
public function on_deleted_term_cb( $term_id, $tt_id, $taxonomy, $deleted_term ) {
$tax_object = get_taxonomy( $taxonomy );
Expand All @@ -387,6 +398,8 @@ public function on_deleted_term_cb( $term_id, $tt_id, $taxonomy, $deleted_term )
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Serialized if non-scalar.
*
* @return void
*/
public function on_updated_term_meta_cb( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( empty( $term = get_term( $object_id ) ) || ! $term instanceof WP_Term ) {
Expand Down Expand Up @@ -503,6 +516,8 @@ public function on_added_term_relationship_cb( $object_id, $tt_id, $taxonomy ) {
* @param string $new_status The new status of the post
* @param string $old_status The old status of the post
* @param WP_Post $post The post being updated
*
* @return void
*/
public function on_transition_post_status_cb( $new_status, $old_status, WP_Post $post ) {

Expand Down Expand Up @@ -612,6 +627,8 @@ function ( $term ) use ( $post ) {
*
* @param int $user_id User ID.
* @param WP_User $old_user_data Object containing user's data prior to update.
*
* @return void
*/
public function on_user_profile_update_cb( $user_id, $old_user_data ) {
// Delete the cached results associated with this key
Expand All @@ -625,6 +642,8 @@ public function on_user_profile_update_cb( $user_id, $old_user_data ) {
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value. Serialized if non-scalar.
*
* @return void
*/
public function on_user_meta_change_cb( $meta_id, $object_id, $meta_key, $_meta_value ) {
$user = get_user_by( 'id', $object_id );
Expand All @@ -642,10 +661,10 @@ public function on_user_meta_change_cb( $meta_id, $object_id, $meta_key, $_meta_
}

/**
*
* @param int $deleted_id ID of the deleted user.
* @param int|null $reassign_id ID of the user to reassign posts and links to.
* Default null, for no reassignment.
* @return void
*/
public function on_user_deleted_cb( $deleted_id, $reassign_id ) {
global $wpdb;
Expand Down Expand Up @@ -679,6 +698,8 @@ public function on_user_deleted_cb( $deleted_id, $reassign_id ) {
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string
* representation of the value if the value is an array, an object,
* or itself a PHP-serialized string.
*
* @return void
*/
public function on_postmeta_change_cb( $meta_id, $post_id, $meta_key, $meta_value ) {

Expand Down Expand Up @@ -856,6 +877,8 @@ public function on_updated_menu_meta_cb( $meta_id, $object_id, $meta_key, $meta_
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string
* representation of the value if the value is an array, an object,
* or itself a PHP-serialized string.
*
* @return void
*/
public function on_menu_item_change_cb( $meta_id, $post_id, $meta_key, $meta_value ) {

Expand Down Expand Up @@ -901,11 +924,18 @@ public function on_add_attachment_cb( $attachment_id ) {
* @param string $image Unused.
* @param string $mime_type Unused.
* @param int $post_id Post ID.
*
* @return void
*/
public function on_save_image_file_cb( $dummy, $filename, $image, $mime_type, $post_id ) {
$this->on_edit_attachment_cb( $post_id );
}

/**
* @param int $attachment_id Attachment ID.
*
* @return void
*/
public function on_edit_attachment_cb( $attachment_id ) {
$attachment = get_post( $attachment_id );

Expand All @@ -919,7 +949,7 @@ public function on_edit_attachment_cb( $attachment_id ) {
/**
* Handle purging when attachment is deleted
*
* @param $attachment_id
* @param int $attachment_id Attachment ID.
*
* @return void
*/
Expand All @@ -939,6 +969,8 @@ public function on_delete_attachment( $attachment_id ) {
* @param int|string $new_status The new comment status.
* @param int|string $old_status The old comment status.
* @param WP_Comment $comment Comment object.
*
* @return void
*/
public function on_comment_transition_cb( $new_status, $old_status, $comment ) {
// Only evict cache if transitioning to or from 'approved'
Expand All @@ -953,6 +985,8 @@ public function on_comment_transition_cb( $new_status, $old_status, $comment ) {
*
* @param int $comment_id The comment ID.
* @param WP_Comment $comment Comment object.
*
* @return void
*/
public function on_insert_comment_cb( $comment_id, $comment ) {
if ( property_exists( $comment, 'comment_approved' ) && '1' === $comment->comment_approved ) {
Expand Down
11 changes: 9 additions & 2 deletions src/Cache/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ class Query {

const GROUP_NAME = 'gql_cache';

// The storage object for the actual system of choice transient, database, object, memory, etc
/**
* The storage object for the actual system of choice transient, database, object, memory, etc
*
* @var object
**/
public static $storage = null;

/**
* @return void
*/
public function init() {
if ( ! self::$storage ) {
if ( null === self::$storage ) {
self::$storage = apply_filters(
'graphql_cache_storage_object', //phpcs:ignore
wp_using_ext_object_cache() ? new WpCache( self::GROUP_NAME ) : new Transient( self::GROUP_NAME )
Expand Down
Loading

0 comments on commit 4e00e3a

Please sign in to comment.