Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 25, 2021

No description provided.

@staabm staabm marked this pull request as ready for review August 25, 2021 19:14
@staabm
Copy link
Contributor Author

staabm commented Aug 25, 2021

Error is unrelated

'array_column' => ['array', 'array'=>'array', 'column_key'=>'mixed', 'index_key='=>'mixed'],
'array_combine' => ['array|false', 'keys'=>'array', 'values'=>'array'],
'array_count_values' => ['int[]', 'input'=>'array'],
'array_count_values' => ['array<int|string, 0|positive-int>', 'input'=>'array'],
Copy link
Member

Choose a reason for hiding this comment

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

just do array<0|positive-int> - the difference is that the key will be BenevolentUnionType

Copy link
Member

Choose a reason for hiding this comment

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

Of course a dynamic return type extension would be nice and precise :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course a dynamic return type extension would be nice and precise :)

Agree. Was just hunting low hanging fruits in the signature file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BenevolentUnionType

i read this classname in a lot of spots, but have no idea what it is/why its better/different then e.g. array-key or int|string

Copy link
Member

Choose a reason for hiding this comment

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

The difference is that if you have array<string> then:

foreach ($array as $k => $v) {
    // $k is BenevolentUnionType of int|string
    $this->requireString($k); // PHPStan does not complain
    $this->requireInt($k); // PHPStan does not complain
    $this->requireStdClass($k); // PHPStan complains on level 5+
}

But with array<int|string, string>:

foreach ($array as $k => $v) {
    // $k is UnionType of int|string
    $this->requireString($k); // PHPStan complains on level 7+
    $this->requireInt($k); // PHPStan complains on level 7+
    $this->requireStdClass($k); // PHPStan complains on level 5+
    $this->requireIntOrString($k); // PHPStan does not complain
}

@ondrejmirtes ondrejmirtes merged commit 69c3b3b into phpstan:master Aug 25, 2021
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the patch-1 branch August 25, 2021 19:32
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