DEVSENSE / phptools-docs

PHP Tools public content
Apache License 2.0
85 stars 10 forks source link

improve type resolve #676

Open ging-dev opened 2 months ago

ging-dev commented 2 months ago

Actual: image Expect: image

jakubmisek commented 2 months ago

We have noticed that many existing frameworks misuse this syntax/semantic :/

So they usually specify both ... and array types (because the variable will be an array after all). Not sure what's correct.

So in case the type is array or [], we assume, it should not be wrapped to another array again.

image
ging-dev commented 2 months ago

We have noticed that many existing frameworks misuse this syntax/semantic :/

So they usually specify both ... and array types (because the variable will be an array after all). Not sure what's correct.

So in case the type is array or [], we assume, it should not be wrapped to another array again.

image

We have noticed that many existing frameworks misuse this syntax/semantic :/

So they usually specify both ... and array types (because the variable will be an array after all). Not sure what's correct.

So in case the type is array or [], we assume, it should not be wrapped to another array again.

image

Agreed, however, I think we should require users to use the correct syntax in order to synchronize with static analysis tools, some recent frameworks have started using static analysis tools:

https://github.com/illuminate/collections/blob/d91e45472a9f4be9ab21a5799308cb72172c9323/Collection.php#L1015

I think in the example below, string[] is a more accurate input than string:

<?php

require __DIR__.'/vendor/autoload.php';

/** @var \Illuminate\Support\Collection<int, string[]> */
$c = collect();

$c->push(['hello']); // Argument '1' passed to push() is expected to be of type string, string[] given
<?php

require __DIR__.'/vendor/autoload.php';

/** @var \Illuminate\Support\Collection<int, array{name: string}> */
$c = collect();

$c->push(['name' => 'ging']); // Argument '1' passed to push() is expected to be of type string, string[] given
jakubmisek commented 1 month ago

I see, thanks!