On 13/09/2019 09:01, Robert Korulczyk wrote:
Actually you need only one additional line:
$foo = [];
foreach ( $something as $key1 ) {
foreach ( $somethingElse as $key2 ) {
$foo[$key1][$key2] ??= 0;
$foo[$key1][$key2]++;
}
}
Why? If "assume $key2 exists as a key and is an integer" is so bad that
PHP should halt my program, why should "assume $key1 exists and is an
array" be perfectly OK?
It does not look confusing. You have two lines, for two intents - start
counting from zero and increment counter on every loop iteration.
There is no intent to start counting at zero; the counter will never be
lower than 1. If we really wanted to express the intent, we would have
to write something like this:
$foo[$key1] ??= [];
if ( ! isset($foo[$key1][$key2]) ) {
$foo[$key1][$key2] = 1;
}
else {
$foo[$key1][$key2]++;
}
If one additional line is to much for making your code less ambiguous and more
bug-free, then I won't even try to change your mind.
Please can you show me a bug that adding this line has avoided? I don't
doubt that the same warning saves bugs in other scenarios, but in this
scenario, the logic is unambiguous, and any additions are just to
suppress unnecessary errors.
To reiterate, my motivation here is to discuss features that help write
these scenarios with less boilerplate, and separate them from other
scenarios where there's a real bug risk which should raise an error.
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php