Repository navigation
Fix the PHPStan level 10 errors that core reports - #171
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
b7e08e2 to
d065411
Compare
| */ | ||
| function ipsum_get_asset_url( string $path ): string { | ||
| $url = get_parent_theme_file_uri( $path ); | ||
| $version = wp_get_theme( get_template() )->get( 'Version' ); |
There was a problem hiding this comment.
This is important because get_parent_theme_file_uri( 'style.css' ) only ever refers to the parent theme's style.css, but without passing get_template() here, then it defaults to the child theme.
Thirteen Ipsum patterns append the theme version to image URLs as a cache busting query string, read with `WP_Theme::get( 'Version' )`. That returns `string|false`, which `esc_attr()` does not accept, so each of the 60 call sites was a PHPStan level 7 error. `ipsum_get_version()` narrows the header to a string, falling back to an empty string when it cannot be read, and the patterns now call it. As before, the version comes from the parent theme. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`ipsum_enqueue_styles()` loads the parent theme's `style.css` but versioned it with `wp_get_theme()`, which is the active theme. With a child theme active, that is the child's version, so updating Ipsum left the stylesheet URL unchanged, and sites with long static file caches could keep serving the old file. Pass `get_template()` so the version is Ipsum's, as the pattern images already do and as Twenty Twenty-Four does for its parent theme styles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace `ipsum_get_version()` with `ipsum_get_asset_url()`, which takes a path relative to the theme directory, resolves it with `get_parent_theme_file_uri()`, and adds the parent theme version as the `ver` query argument with `add_query_arg()`, skipping it when the version cannot be read. The patterns now pass the image path to the helper instead of joining the template directory URI, the path, and a hand-built query string, which also stops them emitting an empty `?ver=` when there is no version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`get_parent_theme_file_uri()` returns the theme directory URL rather than a file URL when the path is empty, which includes `'0'`, so neither is a valid asset path for `ipsum_get_asset_url()`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The block binding source arguments are an object decoded from the block's `metadata.bindings` attribute, so they are keyed by string with values of any type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `postId` block context is untyped, and its fallback, `get_the_ID()`, returns `false` when there is no current post, so neither was a valid argument for `get_comments_number()` or `get_comments_link()`. Resolve the post with `get_post()` instead, which falls back to the current post just as `get_the_ID()` did, and return null when there is none, so the block keeps its original content rather than linking to comments on no post. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `default_template_types` filter passes template types keyed by slug, each an array with a string title and description. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `block_type_metadata` filter passes the decoded `block.json`, keyed by string with values of any type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The block metadata values are untyped, so writing `ariaLabel` straight into `$metadata['supports']` wrote an offset on a mixed value. Read the supports into a local array first, starting from an empty one when it is missing or not an array, then add the ARIA label support and assign it back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Block bindings pass the source arguments before the block instance, so `ipsum_comments_cta_binding()` must accept them to reach the block, even though it does not use them. Unset them to mark them as deliberately unused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d065411 to
8c6dbe8
Compare
|
Thanks, Weston. I’d rather not add a helper to the patterns, since no other default theme does that. Looking at it again, I don’t think we need ?ver= on the pattern images at all: The images are placeholders, so they won’t change after release. If one ever does, it can ship under a new file name. I’ll drop ?ver= from the patterns, which also clears those 60 errors without the helper. Could you take ipsum_get_asset_url() out of this PR and keep the rest? The stylesheet version fix is a good catch. @peterwilsoncc, this undoes the cache busting you suggested in the core review, for the reasons above. |
The pattern images are placeholders that won't change after release, and once a pattern is inserted, its image URL is saved in the post, so a `ver` query argument would never update there anyway. Twenty Twenty-Four and Twenty Twenty-Five don't version their pattern images either, and no other default theme uses a helper in its patterns. So remove `ipsum_get_asset_url()` and build the image URLs from `get_template_directory_uri()` as before, without `?ver=`. This also avoids passing the `string|false` theme version to `esc_attr()`, which was what the helper was added to fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rather than appending the image path to `get_template_directory_uri()`, pass it to `get_parent_theme_file_uri()`, which `ipsum_enqueue_styles()` already uses for `style.css`. The URL is the same, but it can now be filtered with `parent_theme_file_uri`, such as to serve the images from a CDN, and each URL is built by a single call instead of being concatenated in the markup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@MaggieCabrera Thanks. I dropped the helper in 9f94692. However, it seems like it would still be marginally better to not use |
mikachan
left a comment
There was a problem hiding this comment.
Thanks @westonruter, I think get_parent_theme_file_uri() makes sense to keep. It's a Core function rather than a theme helper, so it doesn't add anything new to the patterns, and it matches what #150 is doing for ipsum_enqueue_styles(). @MaggieCabrera, does that sound OK to you?
If we keep it, I think the example in CONTRIBUTING.md should use get_parent_theme_file_uri() too, so new patterns follow the same approach.
Resolve conflicts in functions.php and four patterns: * Keep the branch's post lookup in ipsum_comments_cta_binding() and add trunk's comments_open() check against that post, returning an empty string when comments are closed. Document the new return value. * Take trunk's renamed palette slugs in cta-membership, format-audio, kind-words, and page-soon, and rewrite their image URLs with get_parent_theme_file_uri() as the branch does elsewhere. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
The ability reads the section names from the keys of its properties, so describe the schema as an object whose properties are keyed by section. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
The global settings come back as mixed, and a site can save any shape of preset in Global Styles, so skip origins and presets that are not arrays instead of iterating or indexing them blindly. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
The global settings and styles come back as mixed, and the style variations as untyped arrays, so indexing several levels into them is unchecked. Walk each path with a helper that returns null as soon as a level is not an array or lacks the key, and keep only the array entries of a variation's block styles. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
A palette saved in Global Styles can hold any value as a slug, so check that it is a string before using it as a key into the roles. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
Block attributes parsed from template markup are unvalidated, so a Pattern block's slug is only returned when it is a non-empty string. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
A pattern registered with register_block_pattern() can carry any value as its filePath, so only take its directory when it is a non-empty string. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
The guide is keyed by section name, with each section holding its own shape of data. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
The patterns now pass each image path to `get_parent_theme_file_uri()` and no longer append a `?ver=` argument, so update the contributing guide's pattern image guidance and example to match, and say why the version is left off. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
…de." This reverts commit 9a5e9c8, keeping the string slug check for the color roles from 6155935. The helper only silenced PHPStan's level 10 reports of offset access on mixed: at runtime, `??` already falls back to the default whatever type each level of the theme.json data turns out to be. Level 10 is stricter than anything else in core, so those reports are left as they are, and the nested reads go back to plain `??`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
`??` covers a missing `blocks` key in the global styles, but not one holding another type, which `foreach` would warn on. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
A variation's `blocks` styles, and each block's styles within them, come from an untyped theme.json file, so `array_map( 'array_keys', … )` would throw a TypeError on one that is not an array. Check both levels before taking the keys. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 6d5fb8f3-c7e8-4bce-8351-2e2825d9f5b7
mikachan
left a comment
There was a problem hiding this comment.
Thanks! LGTM, pending @MaggieCabrera's thoughts.
MaggieCabrera
left a comment
There was a problem hiding this comment.
Let's bring this in. Thank you all for your patience and your work on this, it's appreciated
Core's PHPStan analysis has covered Ipsum since the import, and at level 10 it reported errors in the patterns and the design guide ability. Pattern images no longer get a `?ver=` query string, like the pattern images in Twenty Twenty-Four and Twenty Twenty-Five, and their URLs now come from `get_parent_theme_file_uri()`. Synced from WordPress/ipsum#171. See #66631. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This updates Ipsum from `17a7ada71378a21c36670d67725b3ca632845a9f` to `bc2e5af700e28980f251d97f58cf80c72ee50a13` on its `trunk` branch. A full list of changes included in this commit can be found on GitHub: WordPress/ipsum@17a7ada...bc2e5af - theme.json: Give negative outline offset for Button's focus styles (WordPress/ipsum#187) - Fix view transitions ignoring prefers-reduced-motion (WordPress/ipsum#198) - Register the pattern utility classes as block styles (WordPress/ipsum#180) - Fix the PHPStan level 10 errors that core reports (WordPress/ipsum#171) - Provide style.min.css for production (WordPress/ipsum#61) - Use the border radius shorthand in the patterns (WordPress/ipsum#206) - Describe every pattern and offer the format patterns as starter patterns (WordPress/ipsum#181) - Move what theme.json can express out of the block css strings (WordPress/ipsum#185) - Clean up editor-only attributes and serialization in the patterns (WordPress/ipsum#204) - Add DESIGN.md with the theme's design model (WordPress/ipsum#186) - Inline the theme stylesheet (WordPress/ipsum#63) - Design guide: Return all theme patterns after the registry has read them (WordPress/ipsum#192) This commit also includes changes that only exist in core: - Ipsum is added to the `cssmin:themes` Grunt task, so the build makes the `style.min.css` that the theme now enqueues. The built file is ignored. - The theme's `node_modules` directory is ignored, and left out of core's build. - The theme gets its own stylesheet build process, with `build` and `watch` commands that minify `style.css` into `style.min.css`, set up through a new `package.json`, `package-lock.json` and `.nvmrc`. - A new `contributing.txt` documents the build process and the `test-and-zip-default-themes` workflow now tests and runs that build for Ipsum. - Regression tests for the design guide ability are added in core, with a child theme fixture, because Ipsum's repository has no PHPUnit setup. Follow-up to r60934, r64677, r64678, r64679, r65224. Props yashjawale, shailu25, onemaggie, westonruter, josephscott, juanfra, mehtadev, mikachan, ravipatel, wildworks, courane01, poena, gziolo. Fixes #66631. See #66352. git-svn-id: https://develop.svn.wordpress.org/trunk@65366 602fd350-edb4-49c9-b593-d223f7449a82
This updates Ipsum from `17a7ada71378a21c36670d67725b3ca632845a9f` to `bc2e5af700e28980f251d97f58cf80c72ee50a13` on its `trunk` branch. A full list of changes included in this commit can be found on GitHub: WordPress/ipsum@17a7ada...bc2e5af - theme.json: Give negative outline offset for Button's focus styles (WordPress/ipsum#187) - Fix view transitions ignoring prefers-reduced-motion (WordPress/ipsum#198) - Register the pattern utility classes as block styles (WordPress/ipsum#180) - Fix the PHPStan level 10 errors that core reports (WordPress/ipsum#171) - Provide style.min.css for production (WordPress/ipsum#61) - Use the border radius shorthand in the patterns (WordPress/ipsum#206) - Describe every pattern and offer the format patterns as starter patterns (WordPress/ipsum#181) - Move what theme.json can express out of the block css strings (WordPress/ipsum#185) - Clean up editor-only attributes and serialization in the patterns (WordPress/ipsum#204) - Add DESIGN.md with the theme's design model (WordPress/ipsum#186) - Inline the theme stylesheet (WordPress/ipsum#63) - Design guide: Return all theme patterns after the registry has read them (WordPress/ipsum#192) This commit also includes changes that only exist in core: - Ipsum is added to the `cssmin:themes` Grunt task, so the build makes the `style.min.css` that the theme now enqueues. The built file is ignored. - The theme's `node_modules` directory is ignored, and left out of core's build. - The theme gets its own stylesheet build process, with `build` and `watch` commands that minify `style.css` into `style.min.css`, set up through a new `package.json`, `package-lock.json` and `.nvmrc`. - A new `contributing.txt` documents the build process and the `test-and-zip-default-themes` workflow now tests and runs that build for Ipsum. - Regression tests for the design guide ability are added in core, with a child theme fixture, because Ipsum's repository has no PHPUnit setup. Follow-up to r60934, r64677, r64678, r64679, r65224. Props yashjawale, shailu25, onemaggie, westonruter, josephscott, juanfra, mehtadev, mikachan, ravipatel, wildworks, courane01, poena, gziolo. Fixes #66631. See #66352. Built from https://develop.svn.wordpress.org/trunk@65366 git-svn-id: http://core.svn.wordpress.org/trunk@64505 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This updates Ipsum from `17a7ada71378a21c36670d67725b3ca632845a9f` to `bc2e5af700e28980f251d97f58cf80c72ee50a13` on its `trunk` branch. A full list of changes included in this commit can be found on GitHub: WordPress/ipsum@17a7ada...bc2e5af - theme.json: Give negative outline offset for Button's focus styles (WordPress/ipsum#187) - Fix view transitions ignoring prefers-reduced-motion (WordPress/ipsum#198) - Register the pattern utility classes as block styles (WordPress/ipsum#180) - Fix the PHPStan level 10 errors that core reports (WordPress/ipsum#171) - Provide style.min.css for production (WordPress/ipsum#61) - Use the border radius shorthand in the patterns (WordPress/ipsum#206) - Describe every pattern and offer the format patterns as starter patterns (WordPress/ipsum#181) - Move what theme.json can express out of the block css strings (WordPress/ipsum#185) - Clean up editor-only attributes and serialization in the patterns (WordPress/ipsum#204) - Add DESIGN.md with the theme's design model (WordPress/ipsum#186) - Inline the theme stylesheet (WordPress/ipsum#63) - Design guide: Return all theme patterns after the registry has read them (WordPress/ipsum#192) This commit also includes changes that only exist in core: - Ipsum is added to the `cssmin:themes` Grunt task, so the build makes the `style.min.css` that the theme now enqueues. The built file is ignored. - The theme's `node_modules` directory is ignored, and left out of core's build. - The theme gets its own stylesheet build process, with `build` and `watch` commands that minify `style.css` into `style.min.css`, set up through a new `package.json`, `package-lock.json` and `.nvmrc`. - A new `contributing.txt` documents the build process and the `test-and-zip-default-themes` workflow now tests and runs that build for Ipsum. - Regression tests for the design guide ability are added in core, with a child theme fixture, because Ipsum's repository has no PHPUnit setup. Follow-up to r60934, r64677, r64678, r64679, r65224. Props yashjawale, shailu25, onemaggie, westonruter, josephscott, juanfra, mehtadev, mikachan, ravipatel, wildworks, courane01, poena, gziolo. Fixes #66631. See #66352. git-svn-id: https://develop.svn.wordpress.org/trunk@65366 602fd350-edb4-49c9-b593-d223f7449a82
Description
This was first opened against WordPress core as WordPress/wordpress-develop#14701, which was closed in favor of this PR, so that the changes are made here and reach core with the next sync. The commits are the same, ported to this repository.
Since Ipsum was imported into core, core's PHPStan analysis covers it. At level 10, it reported 69 errors in Ipsum, all of which are fixed here. After trunk was merged in, the design guide ability added in #182 (
inc/abilities.php) brought 64 more. Those are fixed too, except for one low-severity error type, described below.?ver=query string added to the pattern images in Rename the theme functions and add the theme version to pattern images #163 is removed, as Twenty Twenty-Four and Twenty Twenty-Five don't version their pattern images either. The images are placeholders that won't change after release, and once a pattern is inserted, its image URL is saved in the post, so the version would never update there anyway. This accounted for 60 of the errors, sinceWP_Theme::get( 'Version' )returnsstring|false, whichesc_attr()does not accept. (Anipsum_get_asset_url()helper was first added to keep the version, but it was removed after review.) The URLs are now built withget_parent_theme_file_uri( 'assets/images/…' )rather than by appending the path toget_template_directory_uri(), matching howipsum_enqueue_styles()getsstyle.css. The URL is the same, but it can be filtered withparent_theme_file_uri. The pattern image guidance and example inCONTRIBUTING.mdare updated to match.ipsum_enqueue_styles()loads the parent theme'sstyle.cssbut versioned it with the active theme. With a child theme active, updating Ipsum did not change the stylesheet URL. It now useswp_get_theme( get_template() ).postIdcontext is untyped, and itsget_the_ID()fallback can befalse. The post is now resolved withget_post(), which falls back to the current post as before, and the callback returnsnullwhen there is none, so the block keeps its original content. The check from Hide the comments CTA and its separators when comments are closed #184 that hides the call to action when comments are closed now runs against that post. The unused$source_args, which has to be accepted to reach the block instance, is unset and documented as unused.ariaLabelis added.Native types were left off the hook callbacks' parameters, since a plugin filtering in a non-array would otherwise cause a fatal error.
Not addressed: 41 reports of
offsetAccess.nonOffsetAccessibleremain ininc/abilities.php. They come from nested reads such as$settings['custom']['controlScheme'] ?? ''into themixedthatwp_get_global_settings()andwp_get_global_styles()return, and into the style variations. At runtime,??already falls back to the default whatever type each level turns out to be, so these are low severity. Silencing them would take a helper to walk each path, which adds code for no change in behavior. Core's_wp_array_get()does exactly that, but it is private, it is used by only four themes in the directory (about 180 active installs in all) and by none of the bundled ones, and its first parameter is typedarray, so eachmixedvalue would still need anis_array()check before it is passed in. Level 10 is stricter than anything else in core, so they are left as an exception.Screenshots
No visual change is intended.
Testing Instructions
npm run lint. It passes.?ver=query string.ipsum-stylestylesheet URL ends in?ver=1.0, Ipsum's version, not the child theme's.ipsum/get-design-guideability. It returns every section, including the colors with their roles, the style variations, and the templates with their patterns.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Running PHPStan at level 10 in core, implementing the fixes under direction, checking the changed callbacks with WP-CLI, porting the commits from core to this repository, and resolving the merge conflicts with trunk.