Skip to content

PHPStan level 9#80

Merged
swissspidy merged 2 commits intomainfrom
add/phpstan
Mar 25, 2026
Merged

PHPStan level 9#80
swissspidy merged 2 commits intomainfrom
add/phpstan

Conversation

@swissspidy
Copy link
Member

No description provided.

@swissspidy swissspidy added this to the 2.2.2 milestone Mar 25, 2026
@swissspidy swissspidy requested a review from a team as a code owner March 25, 2026 10:16
@swissspidy swissspidy added the scope:testing Related to testing label Mar 25, 2026
Copilot AI review requested due to automatic review settings March 25, 2026 10:16
@github-actions
Copy link
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the codebase's robustness by integrating PHPStan at level 9. The primary goal is to improve code quality and prevent potential runtime errors through comprehensive static analysis. This involved adding a dedicated PHPStan configuration and refactoring existing code to adhere to strict type-checking rules, ensuring better maintainability and reliability.

Highlights

  • PHPStan Level 9 Integration: Introduced PHPStan static analysis at its highest level (level 9) to enforce stricter code quality and type safety.
  • Type Hinting and Casting: Implemented numerous type hints, explicit type casts, and PHPDoc annotations across Sidebar_Command.php and Widget_Command.php to resolve issues identified by PHPStan.
  • Configuration: Added a phpstan.neon.dist configuration file, specifying scan paths, WordPress stubs, and ignoring specific missingType errors.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces PHPStan configuration and significantly enhances type hinting across the Sidebar_Command and Widget_Command classes to improve code quality and static analysis. This includes adding detailed PHPDoc for properties, return types, and variables, as well as explicit type casting. The review feedback highlights a critical issue in src/Widget_Command.php where get_option() is called without a default, potentially leading to a fatal error if the option is not found. It suggests using the get_widget_options() helper and the null coalescing operator for safer access to widget options.

@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces PHPStan configuration targeting level 9 and updates the widget/sidebar command implementations with additional type annotations and a few type casts to satisfy stricter static analysis.

Changes:

  • Add phpstan.neon.dist with PHPStan level 9 configuration and scan paths/stubs.
  • Add PHPDoc type annotations for $fields arrays and several Widget_Command helper methods/locals.
  • Add a couple of explicit casts to stabilize inferred types (e.g., widget option index/key handling).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Widget_Command.php Adds PHPStan-oriented typing and a few casts in widget option/sidebar widget handling.
src/Sidebar_Command.php Adds a typed PHPDoc for the $fields property.
phpstan.neon.dist Introduces PHPStan level 9 config, scan paths, and ignore rules for missing param/return types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 675 to 685
@@ -666,14 +678,18 @@ private function wp_get_sidebars_widgets() {
unset( $sidebars_widgets['array_version'] );
}

/**
* @var array<string, array<int, string>> $sidebars_widgets
*/

return $sidebars_widgets;
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp_get_sidebars_widgets() declares it returns array<string, array<int, string>>, but get_option('sidebars_widgets', array()) can still return a non-array value (e.g. option stored as false). To match the declared return type and avoid warnings in callers that iterate it, normalize the value to [] when it isn't an array (similar to Sidebar_Command::get_inactive_sidebar_ids()).

Copilot uses AI. Check for mistakes.
Comment on lines 719 to +727
* @param string $name
* @return array
* @return array<string, mixed>
*/
private function get_widget_options( $name ) {
return get_option( 'widget_' . $name, array() );
$options = get_option( 'widget_' . $name, array() );
/**
* @var array<string,mixed> $options
*/
return $options;
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_widget_options() is documented/annotated as array<string, mixed>, but throughout this class it is accessed with both string keys (e.g. _multiwidget) and numeric instance indexes. This docblock will misrepresent the type for PHPStan and can lead to offset-type errors; update it to a key type that matches real usage (e.g. array<int|string, mixed> or a more precise shape), and consider normalizing get_option() to an array in case it returns false.

Copilot uses AI. Check for mistakes.
@swissspidy swissspidy merged commit 8aa9d83 into main Mar 25, 2026
60 checks passed
@swissspidy swissspidy deleted the add/phpstan branch March 25, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants