Skip to content

Promote count() warning to TypeError #4572

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

Closed
wants to merge 1 commit into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Aug 20, 2019

Split from #4566

@Girgias Girgias force-pushed the count-warning2error branch from b9d67f0 to 7a898c6 Compare August 20, 2019 21:14
@Girgias
Copy link
Member Author

Girgias commented Aug 20, 2019

Will look into the test failure after I've split up the PR into separate smaller ones.

@Girgias Girgias force-pushed the count-warning2error branch from 7a898c6 to f885924 Compare August 20, 2019 23:55
@nikic
Copy link
Member

nikic commented Aug 21, 2019

I'm unsure about this one. One on hand this is a pretty clear case where TypeError should be thrown. On the other hand we do know that the introduction of this warning had some major fallout (possibly one of the largest BC impacts overall in the whole 7.x series), so we might need to be more careful here.

@Girgias
Copy link
Member Author

Girgias commented Aug 21, 2019

Should I raise this issue on internals and see the feedback which comes with it? I would say this is inline with your TypeError RFC.

@theodorejb
Copy link
Contributor

I can see how initially introducing the warning was a large BC break, but it doesn't seem like converting it to an exception is nearly so disruptive. No more than the many other warnings being converted per the consistent type errors RFC.

@krakjoe krakjoe closed this Aug 29, 2019
@krakjoe krakjoe reopened this Aug 30, 2019
@krakjoe krakjoe requested a review from nikic August 30, 2019 07:06
@@ -1861,8 +1861,11 @@ function run_test($php, $file, $env)
}

} else {

if (!isset($section_text['PHPDBG']) && @count($section_text['FILE']) + @count($section_text['FILEEOF']) + @count($section_text['FILE_EXTERNAL']) != 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Personally I'd just replace these @count() with isset().

Copy link
Member Author

Choose a reason for hiding this comment

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

Replacing with isset() would change the logic of this condition as the usage of count() here is to assert that ONLY one of the sections is present otherwise the test file is bogus, same below.

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, this is what I had in mind:

Suggested change
if (!isset($section_text['PHPDBG']) && @count($section_text['FILE']) + @count($section_text['FILEEOF']) + @count($section_text['FILE_EXTERNAL']) != 1) {
if (!isset($section_text['PHPDBG']) && isset($section_text['FILE']) + isset($section_text['FILEEOF']) + isset($section_text['FILE_EXTERNAL']) != 1) {

Too dirty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hum, the intention of this is kinda lost, I'll probably have a look at it again later and try to see if I can't refactor the codebase a bit better.

@@ -34,5 +34,5 @@ int(3)
0 => 0
1 => 10
2 => 20
FFI\Exception: Attempt to count() on non C array
TypeError: Parameter must be an array or an object that implements Countable
Copy link
Member

Choose a reason for hiding this comment

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

Hm, why does this change? That seems odd.

Copy link
Member

Choose a reason for hiding this comment

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

Should be fixed by d266ba4.

@@ -735,7 +735,7 @@ PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */

if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(ht)) {
php_error_docref(NULL, E_WARNING, "recursion detected");
zend_throw_error(NULL, "Recursion detected");
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't change this. Just skip and return null then.

Copy link
Member Author

Choose a reason for hiding this comment

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

ACK

@nikic
Copy link
Member

nikic commented Sep 17, 2019

We discussed this in R11, some points:

  • The initial introduction of this count() warning caused quite a lot of fallout, including in popular libraries and frameworks.

  • While the use of count(new NonCountableObject) is almost certainly a bug, count(null) is rather well-defined, and probably accounts for nearly all cases where the introduction of this warning caused issues.

  • As seen from the occurrence in run-tests.php, even if code is running fine on PHP >= 7.2, it may still be affected by this change due to explicit error suppression :(

  • The switch to TypeError for this error condition does not eliminate an error return value for the function. It is already count(): int, so we won't see an improvement there.

Overall, I believe the consensus was against merging this change, though not everyone agreed (in particular @markrandall). Some people we in favor of dropping the warning for count(null) entirely, though I don't think anyone is planning to actively pursue this yet.

@nikic nikic closed this Sep 17, 2019
@Girgias Girgias deleted the count-warning2error branch January 7, 2020 21:00
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.

5 participants