-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
chore: [PHP] fix phpdoc for MapField keys #9536
Conversation
anything other than `string|int` is not a valid offset type. `bool` may be added, but it's not a true offset because it will be cast to an `int` if used: ```php $a = [false => '1' , true => '2']; $a[0] = '3'; $a[1] = '4'; var_dump($a); // array(2) { // [0]=> // string(1) "3" // [1]=> // string(1) "4" // } ```
If we remove protobuf/php/tests/MapFieldTest.php Lines 301 to 304 in 020e4e3
I don't believe we have PHP 8.1 testing set up in our CI yet, so I'm a little bit nervous merging anything that could throw errors on 8.1 only. |
Hey @acozzette! This is a good question. The user would never receive an error from a PHPDoc change. The only thing that would be effected here is a linter. As PHP arrays do accept bools as array keys, this parameter is okay to remain |
@@ -131,7 +131,7 @@ public function getLegacyValueClass() | |||
* | |||
* This will also be called for: $ele = $arr[$key] | |||
* | |||
* @param int|bool|string $key The key of the element to be fetched. | |||
* @param int|string $key The key of the element to be fetched. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are removing bool
here. Will this cause type checking errors when a user writes $arr[True]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circling back to this! Sorry for the delay.
The param type here should be the same as the other @param $key
types (for instance, in offsetSet
) as it's the same array operation for all of them (php primitive array access). So we can either (1) update the rest of them to be int|bool|string
, or (2) update this one to be int|string
.
Advantages of (1): It's technically correct (PHP allows it), and it's "safer" (opening up the types instead of restricting them), as restricting this type could lead to static analysis errors in existing code (Note: As this is static analysis, it isn't BC-breaking, as running code would remain unaffected).
Advantages of (2): PHP is moving towards stricter types, and when a bool
is used as a PHP array key, it is casted into an int
(0
for false
and 1
for true
). So while it's accepted, it isn't truly a valid array key type. If we document this as int|string
, it's better practice, and also reflects what we would likely do once this library becomes PHP 8.0+ and we use union types.
Whichever we decide is okay with me! I am happy to change all of them to int|bool|string
, as it would be the more conservative move!
I am referring to the case where this PR removes
I took this to mean that the PR is in your court. Am I understanding correctly? |
@haberman I believe this is ready for final review / merge, but I can update this if you'd feel more comfortable with the other approach (see comment reply above!) |
object
is not a valid offset type. Anything other thanstring|int
is not a valid offset type.bool
may be added, but it's not a true offset because it will be cast to anint
if used: