Skip to content

Add native typhints to all hamcrest classes (+ install phpstan) #88

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

Merged
merged 15 commits into from
Jul 31, 2025

Conversation

pscheit
Copy link
Contributor

@pscheit pscheit commented Jun 25, 2025

Hey there 👋 ,

I felt so free to PR native typehints for this library. I noticed that most of the Matchers:: and other methods are not type hinted at all, which makes it really difficult to use the library in other typed libraries (e.g. https://github.com/webforge-labs/object-asserter).

However I went through everything in ./hamcrest with the help of phpstan and added typehints where they were missing.

Added the few "real issues" I found into the baseline.

What do you think, would this worth it to create a Version 3 of this library? Because everything I made is highly non backwards compatible, once got was extended by third parties.
For user the library api shouldn't change.

I added phpstan because that way those refactorings are much easier and easier to spot if mistakes are made.

While going through all that found some few other quirks I will address separately - which would even be backwards compatible.

Hope you like it, if not - don't sweat it, since we didnt talk about it before.

Best regards,
Philipp

Btw: you wrote about codestyle in the CONTRIBUTING notes, but couldnt find a guide?

  • fqn / importing is a bit mixed
  • sometimes the docblocks look a bit odd cause they are broken down at 120 chars? (imho code can be wider these days)
  • found a new line here and there that doesnt belong, but nothing big. Left them untouched.

@pscheit pscheit changed the title Add native to all hamcrest classes Add native typhints to all hamcrest classes (+ install phpstan) Jun 25, 2025
@@ -15,8 +15,13 @@

class IsArrayContainingKeyValuePair extends TypeSafeMatcher
{

/**
* @var mixed $_keyMatcher
Copy link
Member

Choose a reason for hiding this comment

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

If possible please use actually used type/types here and other DocBlocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mixed only got a native typehint in php 8.0.

PHP 7.4:

/**
 * @var mixed $_keyMatcher
 */
 private $_keyMatcher;

and have to use docblocks for this.

PHP 8.x:

 private mixed $_keyMatcher;

Idk if you were following the PHP update discussion, I think version 3.0 with typehints could have a minimum version of PHP 8.0 if it would be my library.
https://www.youtube.com/watch?v=iVaSGD2fCXU

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry, I think I understood your comment only now.

Fixing..

Copy link
Member

Choose a reason for hiding this comment

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

I've meant use @var string|integer|etc $_keyMatcher instead of @var mixed $_keyMatcher in DocBlocks only. Hopefully that would make PHPStan happier.

@pscheit
Copy link
Contributor Author

pscheit commented Jun 25, 2025

Thanks for the review!

So got a few questions now

  • Codestyle: do you want to generally import FQNs or keep FQNS. I felt like often there could be more imports. In general I like keeping either FQNS or importing (with use) everything, not mixing.
  • Do you want to bump version 3.x to php >=8.0 so that we can use mixed as native type hints
  • all the issues in the baseline could be fixed by asserting more: should this done by throwing an Exception or else?

I will:

  • have a look at the generated files how to fix them
  • attack the issues in the baseline as you suggested separately
  • remove the Justfile (or add it in another PR if you like that) and cleanup the .gitignore
  • check the codestyle in several places (if you care that much about codestyle, I would suggest to create a php cs fixer config in the repo)
  • add a PR for the missing extensions in composer.json

I could:

  • harmonize (separate PR) how arrays and/or lists are defined (not using Matcher[] anymore)

@pscheit
Copy link
Contributor Author

pscheit commented Jun 25, 2025

Actually, never mind about the mixing FQNS and not. I understood it now! Because the generator doesn't support imports/use statements, so this one needs to be FQDNs, because it will be taken for code generation ✅

@aik099 aik099 marked this pull request as draft June 27, 2025 07:09
@aik099
Copy link
Member

aik099 commented Jun 27, 2025

  • Codestyle: do you want to generally import FQNs or keep FQNS. I felt like often there could be more imports. In general I like keeping either FQNS or importing (with use) everything, not mixing.

I personally prefer to import everything, but this change should be done in a separate PR.

  • Do you want to bump version 3.x to php >=8.0 so that we can use mixed as native type hints

No.

  • all the issues in the baseline could be fixed by asserting more: should this done by throwing an Exception or else?

Throw an exception probably or use an assert(...) function.

  • harmonize (separate PR) how arrays and/or lists are defined (not using Matcher[] anymore)

Sure. Please do this.

  • attack the issues in the baseline as you suggested separately

OK.

Actually, never mind about the mixing FQNS and not. I understood it now! Because the generator doesn't support imports/use statements, so this one needs to be FQDNs, because it will be taken for code generation ✅

If you'd like (in separate PR) we can teach generator to understand imports.

@pscheit
Copy link
Contributor Author

pscheit commented Jun 27, 2025

No need to review, unless I request a review, because this will make things even more complicated I think

@pscheit
Copy link
Contributor Author

pscheit commented Jul 22, 2025

Hey, I did prioritize some other Open Source work before getting back to this.

To not have to leave this open for months, I would suggest to accept a non-empty phpstan baseline and start the journey of typed code with a little bit of legacy code. This is the way to at least not create new issues while the repository lives on.

Let me know what you would prefer.

@aik099
Copy link
Member

aik099 commented Jul 22, 2025

No need to review, unless I request a review, because this will make things even more complicated I think

My apologies. I'll wait for time, when you request a review then.

To not have to leave this open for months, I would suggest to accept a non-empty phpstan baseline and start the journey of typed code with a little bit of legacy code. This is the way to at least not create new issues while the repository lives on.

Then please do this:

  1. rebase this PR (because it has merge conflicts)
  2. solve the 1 issue not related to the PHPStan baseline
  3. create separate PR for each of the non-resolved PHPStan baseline-related issues
  4. send this PR for review

@pscheit
Copy link
Contributor Author

pscheit commented Jul 23, 2025

Mhh. Maybe we missunderstood.

  1. create separate PR for each of the non-resolved PHPStan baseline-related issues

I was suggesting to not do this (now).

@aik099
Copy link
Member

aik099 commented Jul 23, 2025

Mhh. Maybe we missunderstood.

  1. create separate PR for each of the non-resolved PHPStan baseline-related issues

I was suggesting to not do this (now).

Then create a task(s) explaining the idea behind each of the issues hidden in the PHPStan baseline and how it is supposed to be fixed in the future.

This way we won't lose the fact that something needs to be fixed after this PR is merged.

@pscheit
Copy link
Contributor Author

pscheit commented Jul 26, 2025

Created and linked all three issues from the latest phpstan run without the baseline - they stayed the same.

Resolved the conflicts with upstream (was just the re-generation of the generated files)

@pscheit pscheit marked this pull request as ready for review July 26, 2025 09:31
@pscheit pscheit requested a review from aik099 July 26, 2025 09:31
composer.json Outdated
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0",
"symfony/var-dumper": "^5.4"
Copy link
Member

Choose a reason for hiding this comment

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

Why the symfony/var-dumper is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, my mistake. I removed it

@aik099
Copy link
Member

aik099 commented Jul 29, 2025

I recommend adding a GitHub Action job to run the PHPStan automatically. Example: https://github.com/minkphp/driver-testsuite/blob/master/.github/workflows/ci.yml .

@pscheit
Copy link
Contributor Author

pscheit commented Jul 29, 2025

@aik099 yep, I recommend that, too ;) You have to do this in a separate PR.

Honestly this is a bit hard to do as a contributor without the checks running automatically

@pscheit pscheit requested a review from aik099 July 29, 2025 14:13
@aik099
Copy link
Member

aik099 commented Jul 30, 2025

@aik099 yep, I recommend that, too ;) You have to do this in a separate PR.

Honestly this is a bit hard to do as a contributor without the checks running automatically

Since PhpStan is added in this PR, then GitHub Action changes related to it should be here as well.

@pscheit
Copy link
Contributor Author

pscheit commented Jul 30, 2025

Added the config

@pscheit pscheit requested a review from aik099 July 30, 2025 10:29
@pscheit
Copy link
Contributor Author

pscheit commented Jul 30, 2025

ah, now the checks ran directly, didn't notice it the first time. Well then i was wrong: this was easy

@pscheit pscheit requested a review from aik099 July 31, 2025 04:42
pull_request:

jobs:
static_analysis:
Copy link
Member

Choose a reason for hiding this comment

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

Please do this:

  1. move this job into an existing YML file instead of adding a new one
  2. for an existing YML file:
    1. rename into ci.yml
    2. replace name: tests with name: ci
    3. replace name: PHP ${{ matrix.php }} into name: Tests ${{ matrix.php }}

This way we'll have all CI runs in one place. Right now I have to jump through the hooks to see static analysis and tests of the same PR on the https://github.com/hamcrest/hamcrest-php/actions page.

Example result: https://github.com/minkphp/Mink/actions/runs/13197030721.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@pscheit pscheit requested a review from aik099 July 31, 2025 08:59
@aik099 aik099 merged commit 4a0b4a0 into hamcrest:master Jul 31, 2025
7 checks passed
@aik099
Copy link
Member

aik099 commented Jul 31, 2025

Merged. Thank you @pscheit .

@pscheit pscheit deleted the phpstan-and-types branch July 31, 2025 10:55
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.

2 participants