Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #6833: Refactor to comply with phpstan level 4 #6971

Merged
merged 40 commits into from
Sep 30, 2024

Conversation

Miraeld
Copy link
Contributor

@Miraeld Miraeld commented Sep 12, 2024

Description

This PR cover the level 4 of PHPStan. As I've put everything in a single commit, this could look overwhelming. However, here is a quick overview of errors PHPStan was giving:

1. Unused Methods/Properties

These errors highlight methods or properties defined in tests but never used. They might indicate dead code or functionality that was added but not utilized, possibly leftovers from refactoring or incomplete test cases.

2. Unreachable Code

PHPStan detects sections of code that can never be executed due to conditions or logic that always result in termination (e.g., return statements or exceptions). These often point to redundant code or improper flow control.

3. Boolean Logic Issues

Certain boolean expressions are flagged because they always resolve to the same outcome. This suggests logic errors, often due to overly simplistic conditions or unnecessary negations that don’t change the result.

4. Assertions that Always Evaluate to True/False

Some test assertions will always pass or fail because their conditions are inherently true or false. This reduces the usefulness of the test, indicating they need refinement or their logic adjusted for meaningful validation.

5. PHPDoc-Related Type Mismatches

PHPStan finds discrepancies between types declared in PHPDoc comments and the actual types used in the code, particularly in method calls. This could mean incorrect assumptions in documentation, which may lead to confusion or improper type handling during execution.

Fixes #6833
It doesn't impact user.

Type of change

  • New feature (non-breaking change which adds functionality).
  • Bug fix (non-breaking change which fixes an issue).
  • Enhancement (non-breaking change which improves an existing functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as before).
  • Sub-task of #(issue number)
  • Release

Detailed scenario

No scenario.

Technical description

Documentation

1. Unused Methods/Properties

Methods & Properties that were never used have been deleted if it was okay in the process of WP Rocket.

2. Unreachable Code

Some code were unreachable due to two main reason:
a. Some functions are stopping the execution of PHP ($this->markTestSkipped(), wp_error_json()), and we had code afterwards. In this case, as it's breaking tests if we are removing the code, a ignore line has been added.

b. Sometimes, we are returning a value, while having other function call after. In this case, this code has been removed.

3. Boolean Logic Issues

Most of the time, this is due to over-safeguarding. To avoid that, I've added typed parameters in functions and also replaced the use of apply_filters by wpm_apply_filters_typed().

4. Assertions that Always Evaluate to True/False
This happens in tests, where we have $this->assertTrue(true);. In this case a ignore line has been added to avoid the test to be flag without assertion.

5. PHPDoc-Related Type Mismatches

PhpDocumentation has been updated to reflect the truth about some function return type, and parameters as it's been modified overtime, without a change in the phpdoc, misleading PHPStan.

New dependencies

N/A

Risks

N/A

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.).
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

@Miraeld Miraeld requested a review from a team September 12, 2024 19:16
@Miraeld Miraeld self-assigned this Sep 12, 2024
Copy link

codacy-production bot commented Sep 13, 2024

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for de60b471 69.14% (target: 50.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (de60b47) Report Missing Report Missing Report Missing
Head commit (8028445) 37719 16561 43.91%

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#6971) 81 56 69.14%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

Footnotes

  1. Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

@Miraeld Miraeld force-pushed the enhancement/6833-increase-phpstan-lvl-4 branch from ea83d55 to c950dad Compare September 13, 2024 00:22
@Miraeld Miraeld force-pushed the enhancement/6833-increase-phpstan-lvl-4 branch from 3e28b64 to 33523cd Compare September 13, 2024 13:40
inc/Addon/Cloudflare/Subscriber.php Outdated Show resolved Hide resolved
inc/Addon/WebP/Subscriber.php Outdated Show resolved Hide resolved
inc/Dependencies/Database/Base.php Outdated Show resolved Hide resolved
inc/Engine/Admin/Beacon/Beacon.php Outdated Show resolved Hide resolved
inc/Engine/Admin/Metaboxes/PostEditOptionsSubscriber.php Outdated Show resolved Hide resolved
inc/Engine/Preload/Links/Subscriber.php Outdated Show resolved Hide resolved
inc/Engine/Preload/Subscriber.php Outdated Show resolved Hide resolved
inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php Outdated Show resolved Hide resolved
inc/ThirdParty/Plugins/Optimization/AMP.php Outdated Show resolved Hide resolved
inc/ThirdParty/Plugins/SEO/Yoast.php Outdated Show resolved Hide resolved
@Tabrisrp Tabrisrp added the type: enhancement Improvements that slightly enhance existing functionality and are fast to implement label Sep 18, 2024
@Mai-Saad
Copy link
Contributor

Copy link

@hanna-meda hanna-meda left a comment

Choose a reason for hiding this comment

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

Relating Tests results below:
Uploading testrail-report-639.pdf…

@Miraeld Miraeld added this pull request to the merge queue Sep 30, 2024
Merged via the queue into develop with commit 7348869 Sep 30, 2024
13 checks passed
@Miraeld Miraeld deleted the enhancement/6833-increase-phpstan-lvl-4 branch September 30, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase PHPStan level to 4
4 participants