Skip to content

Commit 549594f

Browse files
committed
feat: improve PHPStan
1 parent a26d8ed commit 549594f

5 files changed

Lines changed: 91 additions & 33 deletions

File tree

phpstan.neon.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
parameters:
2-
level: 5
2+
level: 7
33
paths:
44
- src/
55
editorUrl: 'vscode://file/%%file%%:%%line%%'
66
scanDirectories:
77
- wp-content/plugins/wordpress-seo
88
scanFiles:
99
- vendor/php-stubs/acf-pro-stubs/acf-pro-stubs.php
10-
- vendor/wpsyntex/polylang-stubs/polylang-stubs.php
10+
- vendor/wpsyntex/polylang-stubs/polylang-stubs.php

src/integrations/polylang.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function get_translated_page_id_cache_key(string $language_slug): string
2323
* @see https://github.com/polylang/polylang/blob/88ee8ed65af4f92c0225e4faa46d5f3e640173ba/frontend/frontend-static-pages.php#L122-L146
2424
*
2525
* @param string $url not used
26-
* @param object $language language in which we want the translation
26+
* @param PLL_Language $language language in which we want the translation
2727
* @param int $queried_object_id id of the queried object
2828
*/
2929
function set_is_posts_page($url, $language, $queried_object_id): string
@@ -35,6 +35,13 @@ function set_is_posts_page($url, $language, $queried_object_id): string
3535
$GLOBALS['wp_query']->is_posts_page = true;
3636
return $url;
3737
}
38+
/**
39+
* Undocumented function
40+
*
41+
* @param string $url
42+
* @param PLL_Language $language
43+
* @param int $queried_object_id
44+
*/
3845
function reset_is_posts_page($url, $language, $queried_object_id): string
3946
{
4047
if (!\is_home()) {
@@ -49,6 +56,10 @@ function reset_is_posts_page($url, $language, $queried_object_id): string
4956

5057
/**
5158
* Set translasted IDs
59+
*
60+
* @param int[] $page_ids
61+
*
62+
* @return int[]
5263
*/
5364
function set_translated_page_id(array $page_ids): array
5465
{
@@ -149,7 +160,11 @@ function get_default_language_page_id(int $page_id): int
149160
if (!empty(\pll_current_language())) {
150161
return $page_id;
151162
}
152-
$default_page_id = \pll_get_post($page_id, \pll_default_language());
163+
$default_language = \pll_default_language();
164+
if (!$default_language) {
165+
return $page_id;
166+
}
167+
$default_page_id = \pll_get_post($page_id, $default_language);
153168
return $default_page_id ? $default_page_id : $page_id;
154169
}
155170
\add_filter('pfcpt/post_type_from_id/page_id', __NAMESPACE__ . '\\get_default_language_page_id');
@@ -159,7 +174,7 @@ function get_default_language_page_id(int $page_id): int
159174
*
160175
* @param int $post_id
161176
* @param WP_Post $post
162-
* @param array $translations
177+
* @param int[] $translations
163178
*/
164179
function on_page_for_custom_post_type_change($post_id, $post, $translations): void
165180
{
@@ -176,6 +191,9 @@ function on_page_for_custom_post_type_change($post_id, $post, $translations): vo
176191
}
177192

178193
$post_type = \array_search($default_page_for_post_type_id, $page_ids, true);
194+
if (!\is_string($post_type)) {
195+
return;
196+
}
179197

180198
// Flush cache/rules
181199
$pfcpt->flush_rewrite_rules($post_type);

src/integrations/wordpress-seo.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
use Yoast\WP\SEO\Surfaces\Values\Meta;
1111

1212
// Fix Schema
13-
\add_filter('wpseo_schema_webpage_type', __NAMESPACE__ . '\\fix_schema_webpage_type', 10, 2);
13+
\add_filter('wpseo_schema_webpage_type', __NAMESPACE__ . '\\fix_schema_webpage_type');
14+
/**
15+
* Add CollectionPage schema to custom post type pages
16+
*
17+
* @param string|string[] $type
18+
*
19+
* @return string|string[]
20+
*/
1421
function fix_schema_webpage_type($type)
1522
{
1623
$pfcpt = Plugin::get_instance();
@@ -37,10 +44,12 @@ function fix_schema_webpage_type($type)
3744
/**
3845
* Fix Yoast breadcrumbs on post
3946
*
47+
* @param Indexable[] $indexables
4048
* @param Meta_Tags_Context $context
41-
* @return array
49+
*
50+
* @return Indexable[]
4251
*/
43-
function fix_post_breadcrumbs(array $indexables, $context)
52+
function fix_post_breadcrumbs(array $indexables, $context): array
4453
{
4554
$current_post_type = $context->indexable->object_sub_type ?? null;
4655
if (!$current_post_type || !\is_singular($current_post_type)) {
@@ -78,9 +87,11 @@ function fix_post_breadcrumbs(array $indexables, $context)
7887
/**
7988
* Fix Yoast breadcrumbs on taxonomy
8089
*
81-
* @return array
90+
* @param Indexable[] $indexables
91+
*
92+
* @return Indexable[]
8293
*/
83-
function fix_taxonomy_breadcrumbs(array $indexables, Meta_Tags_Context $context)
94+
function fix_taxonomy_breadcrumbs(array $indexables, Meta_Tags_Context $context): array
8495
{
8596
$current_taxonomy = $context->indexable->object_sub_type ?? null;
8697
if (!\is_tax($current_taxonomy)) {
@@ -134,10 +145,12 @@ function fix_taxonomy_breadcrumbs(array $indexables, Meta_Tags_Context $context)
134145
/**
135146
* Fix Yoast breadcrumbs on home
136147
*
148+
* @param Indexable[] $indexables
137149
* @param Meta_Tags_Context $context
138-
* @return array
150+
*
151+
* @return Indexable[]
139152
*/
140-
function fix_home_breadcrumbs(array $indexables, $context)
153+
function fix_home_breadcrumbs(array $indexables, $context): array
141154
{
142155
$pfcpt = Plugin::get_instance();
143156
if (!$pfcpt->is_query_page_for_custom_post_type()) {
@@ -160,12 +173,12 @@ function fix_home_breadcrumbs(array $indexables, $context)
160173
$front_page_id = $yoast->helpers->current_page->get_front_page_id();
161174
if ($front_page_id === 0) {
162175
$home_page_ancestor = $indexable_repository->find_for_home_page();
163-
if (\is_a($home_page_ancestor, Indexable::class)) {
176+
if (!\is_bool($home_page_ancestor) && \is_a($home_page_ancestor, Indexable::class)) {
164177
$static_ancestors[] = $home_page_ancestor;
165178
}
166179
} else {
167180
$static_ancestor = $indexable_repository->find_by_id_and_type($front_page_id, 'post');
168-
if (\is_a($static_ancestor, Indexable::class) && $static_ancestor->post_status !== 'unindexed') {
181+
if (!\is_bool($static_ancestor) && \is_a($static_ancestor, Indexable::class) && $static_ancestor->post_status !== 'unindexed') {
169182
$static_ancestors[] = $static_ancestor;
170183
}
171184
}
@@ -178,16 +191,6 @@ function fix_home_breadcrumbs(array $indexables, $context)
178191
return $indexables;
179192
}
180193

181-
function add_filter_once($hook, $callback, $priority = 10, $args = 1)
182-
{
183-
$singular = function () use ($hook, $callback, $priority, $args, &$singular) {
184-
\call_user_func_array($callback, \func_get_args());
185-
\remove_filter($hook, $singular, $priority);
186-
};
187-
188-
return \add_filter($hook, $singular, $priority, $args);
189-
}
190-
191194
/**
192195
* Shortcircuit get_option to return the page ID for a custom post type
193196
*

src/plugin.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ public function update_post_type_args(array $args, string $post_type): array
198198
public function get_page_slug(int $page_id): ?string
199199
{
200200
$page_url = \get_permalink($page_id);
201-
return $page_url ? \trim(\parse_url($page_url, PHP_URL_PATH), '/') : null;
201+
if ($page_url === false) {
202+
return null;
203+
}
204+
$page_path = \parse_url($page_url, PHP_URL_PATH);
205+
return \is_string($page_path) ? \trim($page_path, '/') : null;
202206
}
203207

204208
/**
@@ -365,8 +369,10 @@ public function page_for_post_type_field(array $args): void
365369
/**
366370
* Add an indicator to show if a page is set as a post type archive.
367371
*
368-
* @param array $post_states an array of post states to display after the post title
372+
* @param string[] $post_states an array of post states to display after the post title
369373
* @param WP_Post $post the current post object
374+
*
375+
* @return string[]
370376
*/
371377
public function display_post_states($post_states, $post): array
372378
{
@@ -524,7 +530,9 @@ public function is_query_page_for_custom_post_type(?WP_Query $query = null): boo
524530
/**
525531
* Change the template hierarchy on pages for custom post type
526532
*
527-
* @param array<string> $templates
533+
* @param string[] $templates
534+
*
535+
* @return string[]
528536
*/
529537
public function set_home_template_hierarchy(array $templates): array
530538
{
@@ -626,6 +634,8 @@ public function get_page_id_from_query(WP_Query $query): ?int
626634

627635
/**
628636
* Get page ids.
637+
*
638+
* @return int[]
629639
*/
630640
public function get_page_ids(bool $apply_filters = true): array
631641
{
@@ -808,6 +818,8 @@ private function delete_option(string $post_type): void
808818

809819
/**
810820
* Get post types.
821+
*
822+
* @return WP_Post_Type[]
811823
*/
812824
private function get_post_types(): array
813825
{

tests/integration/test-page-for-custom-post-type.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class TestPageForCustomPostType extends Testkit_Test_Case
1111
{
1212
private string $home_for_book_title = 'Home for Books';
1313

14-
private int $home_page_id;
14+
private int $static_front_page_id;
15+
16+
private int $static_page_for_posts_id;
1517

1618
private int $home_for_book_id;
1719

@@ -70,9 +72,21 @@ public function test_query()
7072
], $hierarchy->hierarchy());
7173
}
7274

75+
public function test_query_with_static_pages_set()
76+
{
77+
// update_option('show_on_front', 'page');
78+
// update_option('page_on_front', $this->static_front_page_id);
79+
// update_option('page_for_posts', $this->static_page_for_posts_id);
80+
81+
$this->get(\get_permalink($this->home_for_book_id));
82+
83+
global $wp_query;
84+
$this->assertEquals(\get_post_type_archive_link('post'), \get_permalink($this->static_page_for_posts_id));
85+
}
86+
7387
public function test_query_on_non_home_page()
7488
{
75-
$this->get(\get_permalink($this->home_page_id));
89+
$this->get(\get_permalink($this->static_front_page_id));
7690

7791
global $wp_query;
7892
$this->assertTrue(\property_exists($wp_query, 'is_page_for_custom_post_type'));
@@ -262,15 +276,26 @@ private function setup_data()
262276
$this->bike_post_type => $this->home_for_bike_id,
263277
]);
264278

265-
$home = \get_page_by_path('welcome-home');
266-
if (!$home) {
267-
$this->home_page_id = self::factory()->post->create([
279+
$front_page = \get_page_by_path('welcome-home');
280+
if (!$front_page) {
281+
$this->static_front_page_id = self::factory()->post->create([
268282
'post_type' => 'page',
269283
'post_title' => 'Welcome Home!',
270284
'post_name' => 'welcome-home',
271285
]);
272286
} else {
273-
$this->home_page_id = $home->ID;
287+
$this->static_front_page_id = $front_page->ID;
288+
}
289+
290+
$page_for_posts = \get_page_by_path('posts-page');
291+
if (!$page_for_posts) {
292+
$this->static_page_for_posts_id = self::factory()->post->create([
293+
'post_type' => 'page',
294+
'post_title' => 'Posts Page',
295+
'post_name' => 'posts-page',
296+
]);
297+
} else {
298+
$this->static_page_for_posts_id = $page_for_posts->ID;
274299
}
275300
}
276301
}

0 commit comments

Comments
 (0)