Opened 2 years ago
Last modified 2 years ago
#56499 new defect (bug)
count() used in the loop condition
Reported by: | krunal265 | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | lowest |
Severity: | normal | Version: | |
Component: | Comments | Keywords: | needs-testing has-patch |
Focuses: | administration, coding-standards | Cc: |
Description
The use of count() inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead.
Attachments (2)
Change History (12)
#1
@
2 years ago
- Keywords needs-testing added
- Severity changed from major to normal
Thanks for the patch @krunal265 . Have you tested this change? The $comments
array gets populated inside the while
which means this change will break this functionality because the count is no longer updated with each iteration.
#2
@
2 years ago
Hi there, welcome back to WordPress Trac! Thanks for the ticket.
Do you get any PHPCS warnings from this file? When running WPCS checks for WordPress core with the default phpcs.xml.dist file, wp-admin/includes/dashboard.php
appears to pass all the checks successfully in my testing. Could you clarify how you run the PHPCS checks?
As noted above, the $comments
array is changed inside the loop, so the value needs to be recalculated on each iteration, and the patch may not work as expected. That said, I agree that the condition could probably be simplified.
#4
follow-up:
↓ 6
@
2 years ago
@johnbillion, @SergeyBiryukov Thank you for replying and looking into this ticket.
Yes, I have tested this change and it's working fine. Every new comment is displayed on the dashboard after changing this.
#6
in reply to:
↑ 4
@
2 years ago
- Component changed from Administration to Comments
- Focuses administration added
- Milestone changed from Awaiting Review to 6.1
Replying to krunal265:
Yes, I have tested this change and it's working fine. Every new comment is displayed on the dashboard after changing this.
Thanks! I think an answer to the questions from comment:2 would be helpful to move the ticket forward, as the file passes all WPCS checks for core and it's not quite clear to me what is the exact issue we're trying to fix here:
Do you get any PHPCS warnings from this file? When running WPCS checks for WordPress core with the default phpcs.xml.dist file,
wp-admin/includes/dashboard.php
appears to pass all the checks successfully in my testing. Could you clarify how you run the PHPCS checks?
The patch as is does not look correct to me, as $comments
is set to an empty array at the beginning of the function, so count( $comments )
would always be zero at that point, and the condition becomes redundant, though it looks like the function still works due to the break
statements inside the loop.
If we need to remove the count()
call from the loop condition here, we could change the loop to do ... while
instead, see 56499.diff. I'm not 100% sure this change is necessary, but it might be a readability improvement, so I'm moving this to 6.1 for further discussion.
#7
@
2 years ago
- Milestone changed from 6.1 to Future Release
As the patch and the issue itself are still being discussed, and since beta 1 is in a few days, let's move this ticket to Future Release
milestone.
@krunal265 there is still some questions to answer about the potential issue underlined in this ticket, that's why I'm moving it to Future Release. We can still move it to the next major (6.2) if the issue is clarified :)
#8
@
2 years ago
@audrasjb, We can use do...while loop in this issue instead of while loop. From this change, coding standards and readability will improve. Because the use of count() inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead.
#9
@
2 years ago
- Keywords needs-patch added; has-patch reporter-feedback removed
- Milestone changed from Future Release to Awaiting Review
- Priority changed from normal to lowest
- Version 6.0 deleted
#10
@
2 years ago
- Keywords has-patch added; needs-patch removed
I believe 56499.diff addresses the request here.
patch added.