From 013ea67280f0d7510a101bc9cc804c459fa656fa Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 18 Aug 2024 11:21:16 -0500 Subject: [PATCH] Phpstan fixes --- classes/PodsAPI.php | 71 ++++++++----------- classes/PodsData.php | 8 ++- classes/PodsRESTHandlers.php | 2 +- classes/PodsUI.php | 13 +++- includes/access.php | 5 +- includes/general.php | 6 +- phpstan.neon | 3 + .../wpunit/Pods/PodsRESTHandlersTest.php | 2 +- 8 files changed, 57 insertions(+), 53 deletions(-) diff --git a/classes/PodsAPI.php b/classes/PodsAPI.php index 7ed3b367f3..38605a7052 100644 --- a/classes/PodsAPI.php +++ b/classes/PodsAPI.php @@ -1749,12 +1749,8 @@ public function save_pod( $params, $sanitized = false, $db = true ) { $pod = null; } - if ( $fail_on_load ) { - if ( is_wp_error( $pod ) ) { - return $pod; - } elseif ( empty( $pod ) ) { - return pods_error( __( 'Pod not found', 'pods' ), $this ); - } + if ( $fail_on_load && ! $pod instanceof Pod ) { + return pods_error( __( 'Pod not found', 'pods' ), $this ); } } } @@ -3285,7 +3281,7 @@ public function save_field( $params, $table_operation = true, $sanitized = false if ( $load_params ) { $field_obj = $this->load_field( $load_params ); - if ( $fail_on_load && ( empty( $field_obj ) || is_wp_error( $field_obj ) ) ) { + if ( $fail_on_load && ! $field_obj instanceof Field ) { return $field_obj; } } @@ -4830,11 +4826,12 @@ public function save_pod_item( $params ) { * * Use for globally setting field change tracking. * - * @param bool + * @param bool $track_changed_fields Whether to track changed fields or not. + * @param object $params The parameters passed to save_pod_item. * * @since 2.3.19 */ - $track_changed_fields = apply_filters( "pods_api_save_pod_item_track_changed_fields_{$pod_name}", (boolean) $params->track_changed_fields, $params ); + $track_changed_fields = (bool) apply_filters( "pods_api_save_pod_item_track_changed_fields_{$pod_name}", (bool) $params->track_changed_fields, $params ); $changed_fields = array(); @@ -5083,10 +5080,10 @@ public function save_pod_item( $params ) { * * @since 3.0 * - * @param bool $is_visible Whether the field is visible from conditional logic. - * @param Field $field The field object. - * @param array $field_values The field values referenced. - * @param object $params The save_pod_item parameters. + * @param bool $is_visible Whether the field is visible from conditional logic. + * @param Field|Value_Field $field The field object. + * @param array $field_values The field values referenced. + * @param object $params The save_pod_item parameters. */ $is_visible = (bool) apply_filters( 'pods_api_save_pod_item_conditional_logic_field_is_visible', @@ -6192,9 +6189,9 @@ public static function handle_changed_fields( $pod, $id, $mode = 'set' ) { return []; } - $changed_pods_cache = pods_static_cache_get( 'changed_pods_cache', __CLASS__ ) ?: []; - $old_fields_cache = pods_static_cache_get( 'old_fields_cache', __CLASS__ ) ?: []; - $changed_fields_cache = pods_static_cache_get( 'changed_fields_cache', __CLASS__ ) ?: []; + $changed_pods_cache = (array) ( pods_static_cache_get( 'changed_pods_cache', __CLASS__ ) ?: [] ); + $old_fields_cache = (array) ( pods_static_cache_get( 'old_fields_cache', __CLASS__ ) ?: [] ); + $changed_fields_cache = (array) ( pods_static_cache_get( 'changed_fields_cache', __CLASS__ ) ?: [] ); $cache_key = $pod . '|' . $id; @@ -6202,7 +6199,7 @@ public static function handle_changed_fields( $pod, $id, $mode = 'set' ) { 'depth' => 1, ); - if ( in_array( $mode, array( 'set', 'reset' ), true ) ) { + if ( in_array( $mode, [ 'set', 'reset' ], true ) ) { if ( isset( $changed_fields_cache[ $cache_key ] ) ) { unset( $changed_fields_cache[ $cache_key ] ); } @@ -9785,7 +9782,7 @@ public function lookup_related_items( $field_id, $pod_id, $ids, $field = null, $ $related = get_comments( $comment_args ); - if ( ! is_wp_error( $related ) ) { + if ( $related ) { $related_ids = $related; } } elseif ( @@ -10456,13 +10453,13 @@ public function get_table_info( $object_type, $object, $name = null, $pod = null /** * Allow filtering the table information for an object. * - * @param array $info The table information. - * @param string $object_type The object type. - * @param string $object The object name. - * @param string $name The pod name. - * @param array|Pod $pod The pod config (if found). - * @param array|Field $field The field config (if found). - * @param self $obj The PodsAPI object. + * @param array $info The table information. + * @param string $object_type The object type. + * @param string $object The object name. + * @param string|null $name The pod name. + * @param array|Pod|null $pod The pod config (if found). + * @param array|Field|null $field The field config (if found). + * @param self $obj The PodsAPI object. */ return apply_filters( 'pods_api_get_table_info', $info, $object_type, $object, $name, $pod, $field, $this ); } else { @@ -10570,13 +10567,13 @@ public function get_table_info( $object_type, $object, $name = null, $pod = null * * Use to change "default" post status from publish to any other status or statuses. * - * @param array $post_status List of post statuses. Default is 'publish' or field setting (if available). - * @param string $post_type Post type of current object. - * @param array $info Array of information about the object. - * @param string $object Type of object. - * @param string $name Name of pod to load. - * @param array $pod Array with Pod information. Result of PodsAPI::load_pod(). - * @param array $field Array with field information. + * @param array $post_status List of post statuses. Default is 'publish' or field setting (if available). + * @param string $post_type Post type of current object. + * @param array $info Array of information about the object. + * @param string $object Type of object. + * @param string|null $name Name of pod to load. + * @param array|Pod|null $pod The pod config (if found). + * @param array|Field|null $field The field config (if found). * * @since unknown */ @@ -10993,10 +10990,6 @@ public function import( $import_data, $numeric_mode = false, $format = null ) { */ global $wpdb; - if ( null === $format && null !== $this->format ) { - $format = $this->format; - } - if ( 'csv' === $format && ! is_array( $import_data ) ) { $data = pods_migrate( 'sv', ',' )->parse( $import_data ); @@ -11016,8 +11009,6 @@ public function import( $import_data, $numeric_mode = false, $format = null ) { if ( ! empty( $this->pod_data ) ) { $pod = $this->pod_data; - } elseif ( ! empty( $this->pod ) ) { - $pod = $this->load_pod( [ 'name' => $this->pod ], false ); } if ( false === $pod ) { @@ -11183,8 +11174,6 @@ public function export( $pod = null, $params = null ) { if ( empty( $pod ) ) { if ( ! empty( $this->pod_data ) ) { $pod = $this->pod_data; - } elseif ( ! empty( $this->pod ) ) { - $pod = $this->load_pod( [ 'name' => $this->pod ], false ); } } @@ -11683,7 +11672,7 @@ public function get_pods_object_from_wp_post( $post ) { $post = get_post( $post ); } - if ( ! $post || is_wp_error( $post ) ) { + if ( ! $post instanceof WP_Post ) { return false; } diff --git a/classes/PodsData.php b/classes/PodsData.php index 1c5e95f6ae..0da2014498 100644 --- a/classes/PodsData.php +++ b/classes/PodsData.php @@ -757,6 +757,10 @@ public function select( $params ) { } }//end if + if ( is_object( $results ) ) { + $results = get_object_vars( $results ); + } + /** * Filter results of Pods Query * @@ -766,7 +770,7 @@ public function select( $params ) { * * @since unknown */ - $results = apply_filters( 'pods_data_select', $results, $params, $instance ); + $results = (array) apply_filters( 'pods_data_select', $results, $params, $instance ); // Clean up data we don't want to work with. if ( @@ -806,7 +810,7 @@ public function select( $params ) { * Filters whether the total_found should be calculated right away or not. * * @param boolean $auto_calculate_total_found Whether to auto calculate total_found. - * @param array $params Select parameters. + * @param object $params Select parameters. * @param PodsData $instance The current PodsData instance. * * @since 2.7.11 diff --git a/classes/PodsRESTHandlers.php b/classes/PodsRESTHandlers.php index e53bc2d755..0439b8b7ff 100755 --- a/classes/PodsRESTHandlers.php +++ b/classes/PodsRESTHandlers.php @@ -129,7 +129,7 @@ public static function get_handler( $object, $field_name, $request, $object_type * @param array $field_data The field data * @param object|Pods $pod The Pods object for Pod relationship is from. * @param int $id Current item ID - * @param object|WP_REST_Request Current request object. + * @param object|WP_REST_Request $request Current request object. */ $output_type = apply_filters( 'pods_rest_api_output_type_for_relationship_response', $output_type, $field_name, $field_data, $pod, $id, $request ); diff --git a/classes/PodsUI.php b/classes/PodsUI.php index 9203cbc9ed..79d2b5953b 100644 --- a/classes/PodsUI.php +++ b/classes/PodsUI.php @@ -56,6 +56,13 @@ class PodsUI { */ public $id = 0; + /** + * The inserted item ID. + * + * @var int + */ + public $insert_id = 0; + /** * The prefix used for all URL parameters used by PodsUI. * @@ -2401,7 +2408,7 @@ public function get_params( $params = null, $action = null ) { * * @param array $find_params Parameters used with Pods::find() * @param string $action Current action - * @param PodsUI $this PodsUI instance + * @param PodsUI $obj PodsUI instance * * @since 2.6.8 */ @@ -2637,9 +2644,9 @@ public function manage( $reorder = false ) { * @since 2.6.8 * * @param array $custom_container_classes List of custom classes to use. - * @param PodsUI $this PodsUI instance. + * @param PodsUI $obj PodsUI instance. */ - $custom_container_classes = apply_filters( 'pods_ui_manage_custom_container_classes', array() ); + $custom_container_classes = apply_filters( 'pods_ui_manage_custom_container_classes', array(), $obj ); if ( is_admin() ) { array_unshift( $custom_container_classes, 'wrap' ); diff --git a/includes/access.php b/includes/access.php index 10340b187e..76383ae544 100644 --- a/includes/access.php +++ b/includes/access.php @@ -176,7 +176,7 @@ function pods_user_can_access_object( array $args, ?int $user_id, string $access // Check if the user exists. $user = get_userdata( $user_id ); - if ( ! $user || is_wp_error( $user ) ) { + if ( ! $user instanceof WP_User ) { // If the user does not exist and it was not anonymous, do not allow access to an invalid user. if ( 0 < $user_id ) { return false; @@ -767,7 +767,8 @@ function pods_is_type_public( array $args, string $context = 'shortcode' ): bool 'pods_is_type_public', $is_public, $info, - $context + $context, + $pod ); } diff --git a/includes/general.php b/includes/general.php index de3b6caf33..b2b5925d81 100644 --- a/includes/general.php +++ b/includes/general.php @@ -693,7 +693,7 @@ function pods_is_user_admin( int $user_id, $capabilities = null ): bool { // Check if the user exists. $user = get_userdata( $user_id ); - if ( ! $user || is_wp_error( $user ) ) { + if ( ! $user instanceof WP_User ) { return false; } @@ -4055,7 +4055,7 @@ function pods_meta_hook_list( $object_type = 'post', $object = null ) { * @param string $object_type The object type. * @param string|null $object The object name. */ - return (array) apply_filters( 'pods_meta_hook_list', $hooks, $object_type ); + return (array) apply_filters( 'pods_meta_hook_list', $hooks, $object_type, $object ); } /** @@ -4977,7 +4977,7 @@ function pods_is_types_only( $check_constant_only = false, $content_type = null * @param bool $is_types_only Whether Pods is being used for content types only. * @param null|string $content_type The content type context we are in. */ - return (bool) apply_filters( 'pods_is_types_only', $is_types_only ); + return (bool) apply_filters( 'pods_is_types_only', $is_types_only, $content_type ); } /** diff --git a/phpstan.neon b/phpstan.neon index c3ad4d0c69..f2670ff5c6 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,3 +13,6 @@ parameters: - ui/front - init.php - vendor/vendor-prefixed + excludePaths: + - src/Integrations + - tests diff --git a/tests/codeception/wpunit/Pods/PodsRESTHandlersTest.php b/tests/codeception/wpunit/Pods/PodsRESTHandlersTest.php index acbceec3cb..8a888e2075 100644 --- a/tests/codeception/wpunit/Pods/PodsRESTHandlersTest.php +++ b/tests/codeception/wpunit/Pods/PodsRESTHandlersTest.php @@ -174,7 +174,7 @@ protected function save_test_fields( $pod_id ) { protected function save_test_item( $pod_name ) { $api = pods_api(); - $api->save_pod_item( [ + return $api->save_pod_item( [ 'pod' => $pod_name, 'post_title' => 'Test item for ' . $pod_name, 'post_status' => 'publish',