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

Change array<int...> to list<...> in PHP doc #114

Closed
wants to merge 3 commits into from

Conversation

phil-davis
Copy link
Contributor

Ref: comment #113 (comment)

@@ -47,7 +47,7 @@ public function once(string $eventName, callable $callBack, int $priority = 100)
* Lastly, if there are 5 event handlers for an event. The continueCallback
* will be called at most 4 times.
*
* @param array<int, mixed> $arguments
* @param list<mixed> $arguments
Copy link
Member

Choose a reason for hiding this comment

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

not sure this will work with named arguments. do we have a test-case for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I change to array<int|string, mixed> then phpstan complains:

------ ---------------------------------------------------------------------------------------------------------------------- 
  Line   lib/EmitterTrait.php (in context of class Sabre\Event\Emitter)                                                        
 ------ ---------------------------------------------------------------------------------------------------------------------- 
  80     Parameter #2 $parameters of function call_user_func_array expects array<int, mixed>, array<int|string, mixed> given.  
  91     Parameter #2 $parameters of function call_user_func_array expects array<int, mixed>, array<int|string, mixed> given.  
 ------ ---------------------------------------------------------------------------------------------------------------------- 

phpstan thinks that the PHP function call_user_func_array must be passed just a "list" array (int keys).

But https://w ww.php.net/manual/en/function.call-user-func-array.php says:
"If any keys of args are strings, those elements will be passed to callback as named arguments, with the name given by the key."

Maybe phpstan needs to be adjusted so that it knows what can be passed to call_user_func_array ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure this will work with named arguments. do we have a test-case for that?

The tests only have cases with a single unnamed argument. Named arguments only became a thing in PHP 8.0 (I think). So that will be why there are no test cases for it.

Hmmm - so maybe the phpstan thing in comment above is because I am running phpstan on PHP 7.4.

I think that the code will "just work". But annotations and test case(s) will have to apply only for PHP 8.
Should we try to add official support for named arguments now?

Copy link
Member

Choose a reason for hiding this comment

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

I remember working on this topic in phpstan/phpstan-src#2501 months ago

its a open issue: phpstan/phpstan#5934

lib/Loop/Loop.php Show resolved Hide resolved
Copy link

codecov bot commented Nov 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8112929) 99.21% compared to head (c778e0a) 99.21%.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #114   +/-   ##
=========================================
  Coverage     99.21%   99.21%           
  Complexity      111      111           
=========================================
  Files             7        7           
  Lines           380      380           
=========================================
  Hits            377      377           
  Misses            3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -88,7 +88,7 @@ public function setInterval(callable $cb, float $timeout): array
/**
* Stops a running interval.
*
* @param array<int, mixed> $intervalId
* @param array{0:string, 1:boolean} $intervalId
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to declare that the boolean is a reference to a boolean?

Copy link
Member

Choose a reason for hiding this comment

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

not that I am aware of.

but maybe it makes sense to use a phpstan-type so one can see that we should pass something in here, which was returned by setInterval

Comment on lines +4 to +5
typeAliases:
IntervalStatus: 'array{0:string, 1:boolean}'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@staabm I found that I could not use a local type alias because of:

  1. @phpstan-type defined type doesn't work as a return value phpstan/phpstan#9424 - local type alias cannot be the return type of a function
  2. I couldn't get it to work in https://github.com/sabre-io/event/blob/master/lib/Loop/functions.php - that code is not a class, and @phpstan-import-type does not seem to be effective for "ordinary" code that is not a class.

So I made a global type alias. That works everywhere.

But will doing that be an issue for consumers of this? They will use PHPstan to analyse their use of setInterval and clearInterval, but their phpstan.neon will not know what IntervalStatus is. They might have to add this to their phpstan.neon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The older issue is phpstan/phpstan#9164

Copy link
Member

@staabm staabm Nov 20, 2023

Choose a reason for hiding this comment

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

the global type aliases will not work for library consumers.
(or only if they also define this alias in their config).
additionally the global alias might confuse IDEs which don't know about this stuff, as they would try to find a same named class.

not sure this is worth it since the functions/methods involved with this type are the actual api surface of our package. I think we should stop using it until phpstan supports using the local alias properly for functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made PR #116 that has the code from here, without the last commit. Please review that. Then I can close this PR #114 - leaving this discussion.

@phil-davis
Copy link
Contributor Author

What we can do, is done in PR #116

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