geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.95k stars 519 forks source link

Specify iterable type #1140

Closed franmomu closed 1 year ago

franmomu commented 3 years ago

This will help static analysis tools to understand the iterable type of Collection

norkunas commented 1 year ago

What's missing here to merge? :)

jbelien commented 1 year ago

What's missing here to merge? :)

I must admit I don't know that @template-extends and don't seem to find documentation about it.

Could someone give some documentation and context about what this PR actually does ?

norkunas commented 1 year ago

In my understanding: IteratorAggregate can contain any element in the collection, but the geocoder stores the Location, so it extends and says to SA tools that this collection must store the Location implementing objects so we get the proper typehinting after calling iterator_to_array.

franmomu commented 1 year ago

In my understanding: IteratorAggregate can contain any element in the collection, but the geocoder stores the Location, so it extends and says to SA tools that this collection must store the Location implementing objects so we get the proper typehinting after calling iterator_to_array.

yes, IteratorAggregate is defined as generic by Static Analysis tools, about generics there is this article: https://phpstan.org/blog/generics-in-php-using-phpdocs#class-level-generics

So what this PR does is to tell SA tools (and PHPStorm) that Collection contains Location objects.

Here it's an example using this:

https://phpstan.org/r/ba1d6a3e-ba61-42db-9dde-a14169708c8b

so when you are iterating it for example, you know that each item is a Location object and can warn you if you are calling a method that does't exist in Location in this case.

PS: It would be nice to add a phpstan or psalm to the repository, but that's another PR.

jbelien commented 1 year ago

Working on adding PHPStan in #1193 I'll increase level bit by bit because there are 4783 errors for level 8. 😄