Bump PHPStan to level 4 and fix all resulting issues#1200
Open
utkarshcloudinary wants to merge 2 commits into
Open
Bump PHPStan to level 4 and fix all resulting issues#1200utkarshcloudinary wants to merge 2 commits into
utkarshcloudinary wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Approach
Raise the PHPStan analysis level from 3 to 4 and resolve all 93 resulting errors. Level 4 adds "impossible/always-true/always-false condition" analysis, so most findings are checks that PHPStan can now prove redundant. The overwhelming majority are resolved safely with no runtime behavior change — the tightened PHPDoc from the level-3 pass had become the source of truth, and where it was too narrow PHPStan (correctly) flagged legitimate runtime guards as "impossible". Fixes fall into three groups:
1. Over-narrow PHPDoc widened so genuine runtime guards stay live (majority, no behavior change)
setup()/init_*()were declared as non-null; widened to|nullso the guarding! $this->x/empty()/is_null()checks are valid again:Cache::$file_system,File_System::$wp_file_system,Media::$sync,Media::$gallery,Api::$media,Plugin::$settings,Sync::$settings,Storage::{$media,$sync,$download,$connect},Upload_Sync::$pusher,Setting::$component,Singleton_Trait::$instance,Relation_Trait::$query_relations,WordPress::$cloudinary_url.null/false/WP_Error/arraycase:Cache_Point::url_to_path(string|null),Cache_Point::get_cache_point(\WP_Post|null),Api::upload_cache(array|string|\WP_Error),Media::get_public_id(string|null),Media::image_srcset(array|false),Media::create_attachment(int, notint|WP_Error),Sync::generate_signature(mixed— it returnsapply_filters()output).@paramtypes that are reallymixed/broader because the value comes from WP core,apply_filters, metadata, or array keys:Delivery::filter_out_cloudinary/sanitize_url,Admin::filter_template(int|stringkey),Admin::add_admin_notice(string|array),Text-familysanitize_valueoverrides (File_Folder,React),Component::{is_struct_array,add_content},Params_Trait::{set_params,is_public},File_System::get_file_urls(string|null),Storage::get(bool),Sync::set_signature_item(string|null), pluswp_get_attachment_metadata()results annotatedarray|false(Upgrade,Download_Sync).Sync::$managerswidened toarray<string, ...|null>so themanagers['download']fallback is reachable;Connect::$usagetoarray|mixed.Plugin::get_component()return union gainsCronso the WPMLinstanceof Cronguards type-check.2. Genuinely redundant/dead code removed (no behavior change)
ifor an earlieris_wp_error()early-return:Analytics(is_array/null !==/$user &&),Connect::usage_stats(is_arrayafteris_wp_error),Delivery(&& $relation['slashed']),Media::prepare_size(is_array),Api::cloudinary_url(is_array),Upload_Sync::add_inline_action(re-checkedis_syncable/is_uploadable_media),Settings::set_pending(if ( $found )—find_settingwith$create=truealways returns an object),Opt_Level(is_wp_erroron aSetting).Delivery(wp_get_registered_image_subsizes),Gallery(WP_Screen::is_block_editor).Media::$cloudinary_ids(shadowed by astaticlocal),Video::queue_video_config()+ its now-orphaned$attachmentsproperty,Upload_Sync::$enabledproperty and its dead constructor parameter.3. Deliberate cross-version/optional-integration guards kept and suppressed inline
method_exists()checks that defend against optional Elementor or an old autoloadedUtilsduring a plugin update are kept and annotated with// @phpstan-ignore function.alreadyNarrowedType(the stub/concrete class makes them look always-true, but they are real at runtime).Utils::get_post_parentkeeps itsis_callable( 'get_post_parent' )fallback for WP < 6.0 (suppressed).Plugin::is_wpcom_vip_prod()reads the conditionally-defined VIP constant viaconstant()and suppresses the undefined-constant narrowing.parent::pre_render()/parent::enqueue_scripts()calls (base methods are empty no-ops) are kept for hierarchy consistency and suppressed with// @phpstan-ignore staticMethod.resultUnused.Latent bugs fixed (behavior change — surfaced by the always-true/false analysis)
Api::prepare_options—if ( 0 <= strpos( $transformation, 'f_auto' ) )was always true (falsecoerces to0); corrected tofalse !== strpos( … )so the eager-format branch only runs whenf_autois present.Media::upgrade_settings—if ( 2.4 === $previous_version )compared a float to the version string and was always false, so the 2.4→2.5 migration never ran; corrected to'2.4' === $previous_version.Cache_Point::check_version—if ( ! is_numeric( $cache_point ) )on aWP_Post|nullwas always true; corrected toif ( $cache_point instanceof \WP_Post )to match the evident intent (only act when a cache-point post exists).QA notes
[OK] No errors.composer lintmay OOM; scope to changed files with a raised limit):Api::prepare_options): upload/eager URLs still generate the expectedf_avif/f_webpderivatives only whenf_autois used.Media::upgrade_settings): upgrading from 2.4 migrates the legacy global image/video transformation options.Cache_Point::check_version): cache-point post meta version updates when the plugin version changes.