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

use positive-int in array_count_values() #645

Merged
merged 2 commits into from Aug 25, 2021
Merged

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

@@ -263,7 +263,7 @@
'array_chunk' => ['array[]', 'input'=>'array', 'size'=>'int', 'preserve_keys='=>'bool'],
'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
}

resources/functionMap.php Outdated Show resolved Hide resolved
@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
2 participants