From 3c25e18ef4364581fa5eca1255dd35f4103e158a Mon Sep 17 00:00:00 2001 From: Vasyl Sokolyk Date: Sat, 4 Feb 2023 18:33:49 +0200 Subject: [PATCH] style: update phpdocs, psalm configs and code style --- phpstan-baseline.neon | 35 ----------------------- psalm-baseline.xml | 60 ++++++++++++++++++++-------------------- psalm.xml | 5 ++++ src/ArchivePostType.php | 37 ++++++++++++++++--------- src/CustomPostType.php | 50 ++++----------------------------- src/PostTypeMessages.php | 11 ++++++-- 6 files changed, 73 insertions(+), 125 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index faccc59..7cf8386 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,46 +15,11 @@ parameters: count: 1 path: src/CustomPostType.php - - - message: "#^Method JazzMan\\\\Post\\\\CustomPostType\\:\\:registerTaxonomy\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" - count: 1 - path: src/CustomPostType.php - - - - message: "#^PHPDoc tag @var above a method has no effect\\.$#" - count: 1 - path: src/CustomPostType.php - - - - message: "#^PHPDoc tag @var for variable \\$args has no value type specified in iterable type array\\.$#" - count: 2 - path: src/CustomPostType.php - - - - message: "#^PHPDoc tag @var for variable \\$default_term has no value type specified in iterable type array\\.$#" - count: 2 - path: src/CustomPostType.php - - - - message: "#^PHPDoc tag @var for variable \\$rewrite has no value type specified in iterable type array\\.$#" - count: 2 - path: src/CustomPostType.php - - message: "#^Parameter \\#1 \\$args of function wp_dropdown_categories expects array\\{show_option_all\\?\\: string, show_option_none\\?\\: string, option_none_value\\?\\: string, orderby\\?\\: string, pad_counts\\?\\: bool, show_count\\?\\: bool\\|int, echo\\?\\: bool\\|int, hierarchical\\?\\: bool\\|int, \\.\\.\\.\\}, array\\{hide_empty\\: 0, hierarchical\\: 1, show_count\\: 0, orderby\\: 'name', name\\: non\\-falsy\\-string, value_field\\: 'slug', taxonomy\\: string, selected\\: mixed, \\.\\.\\.\\} given\\.$#" count: 1 path: src/CustomPostType.php - - - message: "#^Method JazzMan\\\\Post\\\\PostTypeMessages\\:\\:bulkUpdatedMessages\\(\\) should return array\\\\> but returns array\\\\>\\.$#" - count: 1 - path: src/PostTypeMessages.php - - - - message: "#^Method JazzMan\\\\Post\\\\PostTypeMessages\\:\\:updatedMessages\\(\\) should return array\\\\> but returns array\\\\>\\.$#" - count: 1 - path: src/PostTypeMessages.php - - message: "#^Function app_get_post_type_labels\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 16d3938..4369726 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5,39 +5,39 @@ $postarr + [ + 'description' => 'Archive posts associated with each post type.', + 'public' => false, + 'show_in_nav_menus' => false, + 'show_in_admin_bar' => false, + 'exclude_from_search' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'can_export' => true, + 'delete_with_user' => false, + 'hierarchical' => false, + 'has_archive' => false, + 'menu_icon' => 'dashicons-media-text', + 'query_var' => 'hdptap_cpt_archive', + 'menu_position' => 26, + 'show_in_rest' => true, + 'labels' => $labels, + 'supports' => $supports, + ] false - - $post->post_name - + + $object->add_archive_page + $wpPostType->add_archive_page + $wpPostType->add_archive_page + - - @var array $args - @var array|bool $rewrite { - @var array|string $default_term { - @var bool $_builtin - @var bool $hierarchical - @var bool $public - @var bool $publicly_queryable - @var bool $show_admin_column - @var bool $show_in_menu - @var bool $show_in_nav_menus - @var bool $show_in_quick_edit - @var bool $show_in_rest - @var bool $show_tagcloud - @var bool $show_ui - @var bool $sort - @var bool|callable $meta_box_cb - @var bool|string $query_var - @var callable $meta_box_sanitize_cb - @var callable $update_count_callback - @var string $description - @var string $rest_base - @var string $rest_controller_class - @var string $rest_namespace - @var string[] $capabilities { - @var string[] $labels - + + $options + + + $col + diff --git a/psalm.xml b/psalm.xml index eef6ec6..fd6f6d9 100644 --- a/psalm.xml +++ b/psalm.xml @@ -29,5 +29,10 @@ + + + + + diff --git a/src/ArchivePostType.php b/src/ArchivePostType.php index a7b2a3b..f61e374 100644 --- a/src/ArchivePostType.php +++ b/src/ArchivePostType.php @@ -11,7 +11,7 @@ class ArchivePostType implements AutoloadInterface { private const ARCHIVE_POST_TYPE = 'hdptap_cpt_archive'; /** - * @var array + * @var array */ private static array $store = []; @@ -132,12 +132,15 @@ private static function createArchivePages(string $postType, \WP_Post_Type $wpPo private static function adminMenuCorrection(string $parentFile = ''): string { global $current_screen; + if (!$current_screen instanceof \WP_Screen) { + return $parentFile; + } + /** @var null|int $postId */ $postId = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT); // if this is a post edit screen for the archive page post type. if (!empty($postId) && 'post' === $current_screen->base && self::ARCHIVE_POST_TYPE === $current_screen->post_type) { - // get the plugin options. - + /** @var null|\WP_Post $post */ $post = get_post($postId); // if we have an archive post type returned. @@ -192,7 +195,7 @@ private static function archiveTitle(string $title, string $postType): string { $archivePost = self::getPostTypeArchive($postType); if ($archivePost instanceof \WP_Post) { - return apply_filters('the_title', $archivePost->post_title); + return (string) apply_filters('the_title', $archivePost->post_title); } return $title; @@ -214,7 +217,7 @@ private static function archiveDescription(string $description, \WP_Post_Type $w $archivePost = self::getPostTypeArchive($wpPostType->name); if ($archivePost instanceof \WP_Post) { - return apply_filters('the_content', $archivePost->post_content); + return (string) apply_filters('the_content', $archivePost->post_content); } return $description; @@ -223,14 +226,12 @@ private static function archiveDescription(string $description, \WP_Post_Type $w return $description; } - /** - * @return null|\WP_Post - */ - private static function getPostTypeArchive(string $postType) { + private static function getPostTypeArchive(string $postType): ?\WP_Post { global $wpdb; if (empty(self::$store[$postType])) { - $postId = $wpdb->get_var($wpdb->prepare(<<prepare(<<posts} @@ -239,13 +240,23 @@ private static function getPostTypeArchive(string $postType) { AND post_type = %s AND post_name = %s LIMIT 1 - SQL, 'hdptap_cpt_archive', $postType)); + SQL, 'hdptap_cpt_archive', $postType); + + if (empty($prepare)) { + return null; + } + + /** @var null|int|string $postId */ + $postId = $wpdb->get_var($prepare); if (null !== $postId) { - self::$store[$postType] = \WP_Post::get_instance((int) $postId); + /** @var \WP_Post $post */ + $post = \WP_Post::get_instance((int) $postId); + + self::$store[$postType] = $post; } } - return empty(self::$store[$postType]) ? null : self::$store[$postType]; + return !empty(self::$store[$postType]) ? self::$store[$postType] : null; } } diff --git a/src/CustomPostType.php b/src/CustomPostType.php index 4f935c6..ae137f7 100644 --- a/src/CustomPostType.php +++ b/src/CustomPostType.php @@ -42,9 +42,9 @@ public function setColumns(array $columns = []): void { */ static function (array $wp_columns = []) use ($columns): array { $newColumns = []; - $newColumns['cb'] = $wp_columns['cb']; - $newColumns['title'] = $wp_columns['title']; - $date = $wp_columns['date']; + $newColumns['cb'] = (string) $wp_columns['cb']; + $newColumns['title'] = (string) $wp_columns['title']; + $date = (string) $wp_columns['date']; unset($wp_columns['cb'], $wp_columns['title'], $wp_columns['date']); foreach (['cb', 'title', 'date'] as $col) { @@ -53,6 +53,7 @@ static function (array $wp_columns = []) use ($columns): array { } } + /** @var array $merge */ $merge = wp_parse_args($wp_columns, $columns); foreach ($merge as $key => $value) { @@ -114,48 +115,7 @@ static function (string $col = '', int $postId = 0) use ($callback): void { } /** - * @param array $options { - * - * @var string[] $labels - * @var string $description - * @var bool $public - * @var bool $publicly_queryable - * @var bool $hierarchical - * @var bool $show_ui - * @var bool $show_in_menu - * @var bool $show_in_nav_menus - * @var bool $show_in_rest - * @var string $rest_base - * @var string $rest_namespace - * @var string $rest_controller_class - * @var bool $show_tagcloud - * @var bool $show_in_quick_edit - * @var bool $show_admin_column - * @var bool|callable $meta_box_cb - * @var callable $meta_box_sanitize_cb - * @var string[] $capabilities { - * @var string $manage_terms - * @var string $edit_terms - * @var string $delete_terms - * @var string $assign_terms - * } - * @var array|bool $rewrite { - * @var string $slug - * @var bool $with_front - * @var bool $hierarchical - * @var int $ep_mask - * } - * @var bool|string $query_var - * @var callable $update_count_callback - * @var array|string $default_term { - * @var string $name - * @var string $slug - * @var string $description - * } - * @var bool $sort - * @var array $args - * @var bool $_builtin - * } + * @param array $options */ public function registerTaxonomy(string $taxonomy, array $options = []): void { $taxonomy = sanitize_key($taxonomy); diff --git a/src/PostTypeMessages.php b/src/PostTypeMessages.php index 25aa184..0dc27ce 100644 --- a/src/PostTypeMessages.php +++ b/src/PostTypeMessages.php @@ -7,6 +7,8 @@ class PostTypeMessages implements AutoloadInterface { public function load(): void { add_filter('post_updated_messages', /** + * @psalm-param array> $messages + * * @return array[] * * @psalm-return array> @@ -15,6 +17,9 @@ static function (array $messages = []): array { return self::updatedMessages($messages); }); add_filter('bulk_post_updated_messages', /** + * @psalm-param array> $messages + * @psalm-param array $counts + * * @return string[][] * * @psalm-return array> @@ -41,6 +46,7 @@ private static function bulkUpdatedMessages(array $messages = [], array $counts return $messages; } + /** @var string $post_type */ if (!empty($messages[$post_type])) { return $messages; } @@ -92,6 +98,7 @@ private static function updatedMessages(array $messages = []): array { return $messages; } + /** @var string $post_type */ if (!empty($messages[$post_type])) { return $messages; } @@ -157,8 +164,8 @@ private static function getPostTypeLabelForMessages() { return false; } - $singular = empty($labels['singular_name']) ? false : $labels['singular_name']; - $plural = empty($labels['all_items']) ? false : $labels['all_items']; + $singular = empty($labels['singular_name']) ? false : (string) $labels['singular_name']; + $plural = empty($labels['all_items']) ? false : (string) $labels['all_items']; if (empty($singular)) { return false;