Skip to content

Fix PHPStan Issues#291

Merged
Messhias merged 7 commits intoPHP-Open-Source-Saver:mainfrom
Blurazzle:main
Mar 17, 2025
Merged

Fix PHPStan Issues#291
Messhias merged 7 commits intoPHP-Open-Source-Saver:mainfrom
Blurazzle:main

Conversation

@Blurazzle
Copy link
Copy Markdown
Contributor

@Blurazzle Blurazzle commented Mar 16, 2025

Description

This update resolves multiple PHPStan issues:

  • Fixed an issue in JWTGenerateSecretCommand by ensuring displayKey($key); is called before returning, avoiding returning a void method.
  • Added missing return true; statements in Expiration.php, IssuedAt.php, and NotBefore.php to comply with PHPStan expectations.
  • Refactor hasAllClaims method to avoid unsafe usage of new static().
  • Updated CHANGELOG.md to reflect these fixes.

Checklist:

  • I've added tests for my changes or tests are not applicable
  • I've changed documentations or changes are not required
  • I've added my changes to CHANGELOG.md

Made the constructor final in Collection.php to prevent overriding, ensuring safe usage of new static(). This resolves the PHPStan issue where extending the class with a different constructor signature could cause runtime errors.
Added missing return true; statements in validatePayload() and validateRefresh() methods of Expiration.php, IssuedAt.php, and NotBefore.php. This resolves PHPStan errors about missing return values in methods expected to return bool.
Refactored return $this->displayKey($key); to explicitly call displayKey($key); before returning.
The issue was returning a value from a method (displayKey) that has a void return type, which PHPStan flagged as an error.
Added entries to the CHANGELOG for fixes addressing PHPStan issues:
- Ensured displayKey($key); is called before returning in JWTGenerateSecretCommand.
- Added missing return true; statements in Expiration.php, IssuedAt.php, and NotBefore.php.
- Made the constructor final in Collection.php to fix new static() usage.
Copy link
Copy Markdown
Contributor

@mfn mfn left a comment

Choose a reason for hiding this comment

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

I'm strongly against making the constructor final, defeats a lot oft the purpose have a non-final collection at all.

The rest of the changes LGTM. It's interesting that the default validatePayload returns the value itself 🤷🏼

Comment thread src/Claims/Collection.php Outdated
* @return void
*/
public function __construct($items = [])
final public function __construct($items = [])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm against this change, it doesn't make sense and also no explanation.

If you want to fix the phpstan, I suggest to take a look at hasAllClaims() if there's a solution without new static possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I refactored hasAllClaims and removed final from constructor.

- Removed `final` keyword from the constructor to allow subclassing.
- Refactored `hasAllClaims` method to replace `new static($claims)->diff($this->keys())->isEmpty()`
  with a foreach loop.
@Messhias Messhias requested a review from mfn March 17, 2025 08:28
Copy link
Copy Markdown
Contributor

@mfn mfn left a comment

Choose a reason for hiding this comment

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

Nice work!

Please update the PR description, since the constructor has not been made final, thank you

@Blurazzle
Copy link
Copy Markdown
Contributor Author

Thanks, I have updated the description.

@Messhias Messhias merged commit 9af3bd9 into PHP-Open-Source-Saver:main Mar 17, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants