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

Improve Collection Typings + Add strict Operation #102

Merged
merged 26 commits into from
Jun 12, 2021

Conversation

AlexandruGG
Copy link
Collaborator

@AlexandruGG AlexandruGG commented May 30, 2021

This PR starts a process of improving the types on the operations to make the Collection behave like a single-type one, without enforcing the type restriction at runtime unless the strict operation is used. Based on multiple discussions had in this PR and in #99.

@AlexandruGG
Copy link
Collaborator Author

As an aside, there seem to be some differences between the CI setup and running GrumPHP locally. The failures seen in CI don't happen on local. Do you think it could be due to the fact that Psalm is configured to use a cache in CI? (I think we should disable this btw)

ERROR: UnusedPsalmSuppress - tests/static-analysis/CollectionT/append.php:79:21 - This suppression is never used (see https://psalm.dev/207)
/** @psalm-suppress InvalidScalarArgument @phpstan-ignore-next-line */


ERROR: UnusedPsalmSuppress - tests/static-analysis/CollectionT/append.php:81:21 - This suppression is never used (see https://psalm.dev/207)
/** @psalm-suppress InvalidScalarArgument @phpstan-ignore-next-line */


ERROR: UnusedPsalmSuppress - tests/static-analysis/CollectionT/append.php:96:21 - This suppression is never used (see https://psalm.dev/207)
/** @psalm-suppress InvalidArgument @phpstan-ignore-next-line */```

@drupol
Copy link
Collaborator

drupol commented May 30, 2021

As an aside, there seem to be some differences between the CI setup and running GrumPHP locally. The failures seen in CI don't happen on local. Do you think it could be due to the fact that Psalm is configured to use a cache in CI? (I think we should disable this btw)

No I don't think this is the case.

However, you can try to disable it by adding:

tasks.psalm.no_cache: true

in the local grumphp file, under the parameters directive.

@AlexandruGG
Copy link
Collaborator Author

As an aside, there seem to be some differences between the CI setup and running GrumPHP locally. The failures seen in CI don't happen on local. Do you think it could be due to the fact that Psalm is configured to use a cache in CI? (I think we should disable this btw)

No I don't think this is the case.

However, you can try to disable it by adding:

tasks.psalm.no_cache: true

in the local grumphp file, under the parameters directive.

You're right looks like disabling the cache didn't fix the issue. I can't explain the difference in the errors reported but I've reverted the Psalm config change to report unused psalm-suppress annotations which was causing the issue. I'll probably just use it in local development.

@github-actions
Copy link

github-actions bot commented Jun 6, 2021

Since this pull request has not had any activity within the last 5 days, I have marked it as stale.
I will close it if no further activity occurs within the next 5 days.

@github-actions github-actions bot added the stale label Jun 6, 2021
@drupol drupol removed the stale label Jun 6, 2021
@AlexandruGG AlexandruGG changed the title [WIP] Just playing around with the type hints for a typed collection [WIP] Improve Collection Typings + Add strict Operation Jun 7, 2021
src/Collection.php Outdated Show resolved Hide resolved
@drupol
Copy link
Collaborator

drupol commented Jun 11, 2021

Hi Alex,

I reviewed locally your pull request. I can't wait to merge this :)

However I have a few remarks:

  • I think it would be better in this case to create an Operation instead of the TypedIterator. The strict should follow the same pattern as the other operations, at least for version ^4.
  • I understand the feeling of having a "already made" typed collection class, but I think we should not. If we want to achieve such things, we should add the documentation and explain how to create it's own class TypedCollection extending Collection.
    I actually do not like the idea of starting to pass parameters in constructors. Collection should stay very basic and flexible so people can implements whatever they want.

WDYT?

@AlexandruGG
Copy link
Collaborator Author

Hi Alex,

I reviewed locally your pull request. I can't wait to merge this :)

However I have a few remarks:

  • I think it would be better in this case to create an Operation instead of the TypedIterator. The strict should follow the same pattern as the other operations, at least for version ^4.
  • I understand the feeling of having a "already made" typed collection class, but I think we should not. If we want to achieve such things, we should add the documentation and explain how to create it's own class TypedCollection extending Collection.
    I actually do not like the idea of starting to pass parameters in constructors. Collection should stay very basic and flexible so people can implements whatever they want.

WDYT?

Hey @drupol , thanks for this.

  • regarding the Operation instead of TypedIterator I can try to refactor this. Just to clarify, this means that we still use the TypedIterator but it gets moved to an operation, similar to how the RandomIterator is used in Shuffle, right?

  • I understand the concern regarding passing parameters. I'm personally not a fan of allowing Collection to be extendable, I think it's best to keep it final like it is now though. I was going to suggest providing a TypedCollection which is very small, just extending Collection and overriding getIterator. However, that's not possible if we want to keep it final. I think for now let's go with the strict operation.

@drupol
Copy link
Collaborator

drupol commented Jun 11, 2021

* regarding the `Operation` instead of `TypedIterator` I can try to refactor this. Just to clarify, this means that we still use the `TypedIterator` but it gets moved to an operation, similar to how the `RandomIterator` is used in [Shuffle](https://github.com/loophp/collection/blob/master/src/Operation/Shuffle.php#L37), right?

Yes, this is also a valid option.

* I understand the concern regarding passing parameters. I'm personally not a fan of allowing `Collection` to be extendable, I think it's best to keep it `final` like it is now though. I was going to suggest providing a `TypedCollection` which is very small, just extending `Collection` and overriding `getIterator`. However, that's not possible if we want to keep it final. I think for now let's go with the `strict` operation.

Wait wait wait, it's not because you have the final keyword that its not possible to create another Collection class that extends the functionalities of the original one.

I'm all in favor of using the Composition pattern instead of Inheritance, I found it so much easier and cleaner in every way.

I can provide an example if you want to, but a bit later today.

@drupol
Copy link
Collaborator

drupol commented Jun 11, 2021

Here's an extremely basic (and streamlined) example on how to extend the collection: https://gist.github.com/drupol/8b646ec5e1b91d45cf20969b4eabb3af

README.md Show resolved Hide resolved
@AlexandruGG AlexandruGG changed the title [WIP] Improve Collection Typings + Add strict Operation Improve Collection Typings + Add strict Operation Jun 11, 2021
@AlexandruGG AlexandruGG marked this pull request as ready for review June 11, 2021 22:43
@AlexandruGG
Copy link
Collaborator Author

@drupol refactored into a Strict operation now + added documentation.

There's an issue with phpcsfixer and phpcs where they are in opposition -> phpcs wants space here new class () {} and phpcsfixer wants no space, like new class() {} 😄

@drupol
Copy link
Collaborator

drupol commented Jun 11, 2021

@drupol refactored into a Strict operation now + added documentation.

There's an issue with phpcsfixer and phpcs where they are in opposition -> phpcs wants space here new class () {} and phpcsfixer wants no space, like new class() {} 😄

I think you can get rid of the parenthesis after the class keyword ?

@AlexandruGG
Copy link
Collaborator Author

@drupol refactored into a Strict operation now + added documentation.
There's an issue with phpcsfixer and phpcs where they are in opposition -> phpcs wants space here new class () {} and phpcsfixer wants no space, like new class() {} 😄

I think you can get rid of the parenthesis after the class keyword ?

I tried but you cannot, because phpcsfixer adds it back 😄

@AlexandruGG
Copy link
Collaborator Author

looks like this issue: squizlabs/PHP_CodeSniffer#3200

@drupol
Copy link
Collaborator

drupol commented Jun 12, 2021

It has also been reported here: PHP-CS-Fixer/PHP-CS-Fixer#5463

@drupol
Copy link
Collaborator

drupol commented Jun 12, 2021

I think you should fix it by doing this: https://gist.github.com/drupol/39a459e29764efdc41cfacb28ee4b751

@drupol
Copy link
Collaborator

drupol commented Jun 12, 2021

I added a note on how to extend this library here: https://loophp-collection.readthedocs.io/en/latest/pages/usage.html#extending-collection

@AlexandruGG
Copy link
Collaborator Author

I added a note on how to extend this library here: https://loophp-collection.readthedocs.io/en/latest/pages/usage.html#extending-collection

Nice 👍

@AlexandruGG
Copy link
Collaborator Author

I think you should fix it by doing this: https://gist.github.com/drupol/39a459e29764efdc41cfacb28ee4b751

do you not think it's better to just ignore the docs folder? PHPCSFIXER is running on that anyway and I think it's nicer than seeing the phpcs ignore comments in the docs

@AlexandruGG
Copy link
Collaborator Author

I added a note on how to extend this library here: https://loophp-collection.readthedocs.io/en/latest/pages/usage.html#extending-collection

This also broke PHPStan but I've added an ignore for docs now

@drupol
Copy link
Collaborator

drupol commented Jun 12, 2021

I think you should fix it by doing this: https://gist.github.com/drupol/39a459e29764efdc41cfacb28ee4b751

do you not think it's better to just ignore the docs folder? PHPCSFIXER is running on that anyway and I think it's nicer than seeing the phpcs ignore comments in the docs

Ok!

@AlexandruGG
Copy link
Collaborator Author

@drupol let's get this released if you've had the chance to review 🚀

@drupol drupol merged commit 9519bed into loophp:master Jun 12, 2021
@drupol
Copy link
Collaborator

drupol commented Jun 12, 2021

Thanks mate! Very nice stuff :)

@AlexandruGG
Copy link
Collaborator Author

Thanks mate! Very nice stuff :)

Thank you for the help on this, I think we had some good collaboration!

@AlexandruGG AlexandruGG deleted the feature/typed-collection branch June 12, 2021 19:14
@drupol drupol mentioned this pull request Jun 16, 2021
keradus added a commit to PHP-CS-Fixer/PHP-CS-Fixer that referenced this pull request Aug 28, 2021
…Possum)

This PR was merged into the master branch.

Discussion
----------

ClassDefinitionFixer - PSR12 for anonymous class

Adds PSR12 support for `anonymous class` notation with a space following `new class`, like `new class (1,2) {}` (note that `new class {}` is currently already covered)

Closes: #5463 (comment)
Also reported here loophp/collection#102 (comment) (cc `@drupol` )
Closes one of the points here #4502
New feature so targets `master`, no BC break, new behavior through configuration (by default configuration acts the same)

Commits
-------

4d84a83 ClassDefinitionFixer - PSR12 for anonymous class
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.

None yet

2 participants