#61648 closed task (blessed) (fixed)
WP_Debug_Data::debug_data() is overly complex
Reported by: | apermo | Owned by: | dmsnell |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | 6.6 |
Component: | Site Health | Keywords: | has-patch needs-docs dev-reviewed |
Focuses: | Cc: |
Description
The function WP_Debug_Data::debug_data()
starts at line 37 and ends at line 1490, this does not yet include the 13 lines of phpDoc above itself, which results in a total of 1466 lines including documentation for a single function.
This breaks with so many programming best practices. And even though I know the rule "Don't refactor, just because you can" I do think, that this one needed refactoring.
The function does generate the complete output at /wp-admin/site-health.php?tab=debug, including all 12(13) accordions.
As of writing this ticket, I have already refactored the function, so I can at this moment already point out some design flaws.
Most noticeable is a hidden bug inside the function at line 1302. For documentation reasons I will create an additional trac ticket for this. But basically in line 1302 the value of $parent_theme_auto_update_string
is overwritten by the content of $auto_updates_string
, which was likely never found due to two reasons, it barely changed the result and due to the massive complexity of the function, no one ever ran into it by accident.
Also the structure of the whole function is counter intuitive. In some cases the label and structure is created first and a several/hundreds lines later the fields will be populated.
In other cases all is done in a single place.
This makes extending the function within core already quite challenging. From what I've researched, this code grew inside a featured plugin for 2 years before it was copied into WordPress core, without any noticeable refactoring. And after that, the number of lines and it's complexity grew by a few hundred lines.
Another thing, which the current filter does not easily provide. If you want to inject own debug content through the filter 'debug_information'
you will either append your debug information to the end, or you are force to manually restructure the array, which could potentially create conflicts if plugin authors do not pay attention here. Having one author doing a poor job here, could potentially mean, that debug informations by others could be removed by the attempt to insert his/her content at a precise place.
I am aware that this would be the plugin authors fault, but by using the filter from the beginning to inject all core debug information, will provide a safe, easy and robust way, for authors to inject their debug information at other places than at the very end.
Due to the massive size of the function, it is absolutely impossible to ever write any reasonable unit test for this.
In my PR I have done the following things:
- Bumped compatibility to 7.2 by adding types where suitable to the file.
- Fixed the mentioned bug
- Refactored code to distinct function, so that nearly all accordions are handled by a single function. Only exception to this rule is active/inactive plugins, since these are heavily tied together.
- using
add_filter( 'debug_information', CALLBACK );
to populate the debug information array, which provides a robust way to inject contents at places other than the end.
Change History (91)
This ticket was mentioned in PR #7027 on WordPress/wordpress-develop by @apermo.
5 months ago
#1
#2
@
5 months ago
Separate Ticket for the mentioned bug https://core.trac.wordpress.org/ticket/61649
This PR includes a fix for it.
5 months ago
#4
This seems like valuable work; thanks @apermo for putting this together.
At the same time, it's definitely challenging to review, probably for the same reason it might have been challenging to refactor: size and scope.
Since I'm not familiar with this subsystem I would be happy if others (who _are_) would be able to examine this and evaluate it.
In the case that nobody is available, I might offer breaking this into multiple smaller changes. The raw size of the diff is one thing, but also the renames all combined make it more difficult to follow changes to the control flow. I wonder if we could address each sub-function one at a time where it's easier to isolate the changes and consider each one on its own. For example, there is a lot of code in there working through imagick
limits - it would be nice to be able to focus solely on that change and not have to consider it within the scope of all of the others.
This is more work on you, which I find unfortunate, so please take this only as one voice suggesting a way to get it through faster, if that's your goal. This is often how I like to structure my work.
5 months ago
#5
Thanks @dmsnell for the comment. Do understand you correctly that you suggest to basically have 1 PR per added/refactored function/add_action()?
If so, I can surely break that into single commits per new function.
#6
follow-up:
↓ 22
@
5 months ago
That's right @apermo. One PR for each atomic change, sharing the reference to this ticket, closing this ticket once they are all through.
Also if you take this approach I recommend starting by doing one small change first, getting it through, and only then doing the next one. This is a recommendation to guard your time in case something turns up during that first PR.
@azaozz @SergeyBiryukov I would be interested on your thoughts with this too. I don't want to ask @apermo to do a bunch of work if people are able to confidently review the patch as proposed.
5 months ago
#7
Hm. I thought for sure that I responded to this but maybe my connection was bad and the comment was lost. Yes, @apermo I was thinking that one PR for each incremental atomic piece would be helpful simply because it shrinks the surface-area of the review.
For instance, there is a lot of code dedicated to finding limits of Imagick. That could all be isolated in one change that only addresses the Imagick pieces. You are invited to _cc_ me on any splits or follow-ups to this.
5 months ago
#8
Thanks for this work @apermo! I agree with @dmsnell that addressing this in separate PRs would be ideal. I'd recommend starting with one PR for one piece. We can then review this and ensure the approach is supported and is ready for inclusion in Core. From there, the remaining PRs can be created that follow the same approach.
I noticed that some existing methods had return type declarations added to them. I'd suggest that the scope of this work is limited to splitting WP_Debug_Data::debug_data()
up to improve maintainability.
5 months ago
#9
@costdev I sure will, i will break this down into multiple commits so that we can do multiples PRs out of it. I will leave this open until I have the first new PR
5 months ago
#10
I have renamed the original branch.
But this PR can now remain closed.
https://github.com/apermo/wordpress-develop/tree/refactor-wp-debug-data-old
This ticket was mentioned in PR #7065 on WordPress/wordpress-develop by @apermo.
5 months ago
#11
Changed: Moving $infowp-filesystem? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
To keep the structure intact over all PRs, we will start moving content from the $info
array into own function from the bottom up, this way the result will always be identical and combarable.
Summary of the coming steps:
- wp-filesystem (we are here)
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
5 months ago
#12
I decided to move the add_filter()
calls from WP_Debug_Data::debug_data()
to a new function WP_Debug_Data::init_filters()
which is called on admin_init
this way the filters be altered in a non hacky way by plugin authors easily.
5 months ago
#13
Update: I had to move that around a bit, in order to minimize dependencies. To allow plugin authors a convenient way to rearrange debug information, I introduced a new action pre_debug_information
that will be fired directly before the filter debug_information
, I decided to do this, instead of relying on plugin authors to use the filter to achieve the same, since I understand that it is a kind of hacky approach to use a filter to do an actions job.
5 months ago
#14
In branch refactor-wp-debug-data-old is the original PR, with a minor update, to integrate changed approaches and more phpDoc from the new refactoring.
In refactor-wp-debug-data/all-parts there is already one commit for each refactored function prepared as stated above.
5 months ago
#15
Thanks @apermo! I think this looks like a good approach for making things more maintainable.
Hopefully @afragen and @clorith have some time to take a look at this and review as Site Health component maintainers.
#20
@
5 months ago
- Milestone changed from Awaiting Review to 6.7
- Owner set to dmsnell
- Status changed from new to assigned
Post commit housekeeping
This ticket was mentioned in PR #7106 on WordPress/wordpress-develop by @apermo.
5 months ago
#21
Changed: Moving $infowp-constants? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants (we are here)
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
#22
in reply to:
↑ 6
@
5 months ago
Replying to dmsnell:
One PR for each atomic change, sharing the reference to this ticket, closing this ticket once they are all through.
Also if you take this approach I recommend starting by doing one small change first, getting it through, and only then doing the next one. This is a recommendation to guard your time in case something turns up during that first PR.
This does indeed seem like a reasonable approach here, making the changes easier to review. Thanks!
This ticket was mentioned in PR #7143 on WordPress/wordpress-develop by @kebbet.
4 months ago
#25
https://core.trac.wordpress.org/ticket/61648
Follow up to #7106
This ticket was mentioned in PR #7283 on WordPress/wordpress-develop by @apermo.
3 months ago
#28
Changed: Moving $infowp-server? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server (we are here)
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
This ticket was mentioned in PR #7284 on WordPress/wordpress-develop by @kebbet.
3 months ago
#29
Next step: move wp-server debug info to it's own function
3 months ago
#30
Close in favor for https://github.com/WordPress/wordpress-develop/pull/7283
This ticket was mentioned in PR #7289 on WordPress/wordpress-develop by @dmsnell.
3 months ago
#31
During a refactor to modularize the debug data class, it came up that the ordering of the sections inside of the returned debug info is relevant to existing UIs, as they iterate the array, which happens in insertion order.
This patch presets each section at the start to ensure that the ordering remains consistent even as code within the method is rearranged.
See Core-61648.
This ticket was mentioned in PR #7305 on WordPress/wordpress-develop by @apermo.
3 months ago
#34
Changed: Moving $infowp-mu-plugins? to own function
I've started a second parallel PR, a little lower, so that we should not get too many merge conflicts.
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins (we are here)
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
3 months ago
#35
@dmsnell since we have our structure now, I've opened a second PR, I've opted to have it a little in the middle, so that we can easily rebase if needed with hopefully little to no merge conflicts.
3 months ago
#38
@apermo I've merged trunk
into this and hope I did that properly. how does it look? is this ready in your opinion for merging?
3 months ago
#39
@apermo I've merged
trunk
into this and hope I did that properly. how does it look? is this ready in your opinion for merging?
Looks good to me. @dmsnell
This ticket was mentioned in PR #7337 on WordPress/wordpress-develop by @kebbet.
3 months ago
#42
This is step 5, wp-media.
https://core.trac.wordpress.org/ticket/61648
This ticket was mentioned in PR #7356 on WordPress/wordpress-develop by @apermo.
3 months ago
#43
Changed: Moving $infowp-mu-plugins? to own function
I've started a second parallel PR, a little lower, so that we should not get too many merge conflicts.
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media (we are here)
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
This ticket was mentioned in PR #7357 on WordPress/wordpress-develop by @apermo.
3 months ago
#44
Changed: Moving $infowp-core? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core (we are here)
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
This ticket was mentioned in PR #7418 on WordPress/wordpress-develop by @apermo.
3 months ago
#48
Changed: Moving $infowp-dropins? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins (we are here)
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
This ticket was mentioned in PR #7445 on WordPress/wordpress-develop by @apermo.
3 months ago
#51
Changed: Moving $infowp-paths-sizes? to own function
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes (we are here)
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
This ticket was mentioned in PR #7446 on WordPress/wordpress-develop by @mukesh27.
3 months ago
#52
Trac ticket: https://core.trac.wordpress.org/ticket/61648
This ticket was mentioned in PR #7458 on WordPress/wordpress-develop by @apermo.
2 months ago
#55
Added get_wp_plugins_raw_data()
Moved wp-plugins-active definition to get_wp_plugins_active() Moved wp-plugins-inactive definition to get_wp_plugins_inactive()
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive (we are here)
- wp-mu-plugins
- wp-themes-inactive
- wp-parent-theme
- wp-active-theme
- wp-dropins
- wp-paths-sizes
- wp-core
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
- fixed an error with
$parent_theme_auto_update_string
showing the value from$auto_updates_string
(This will be handled only in Step 9)
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
#56
@
2 months ago
- Resolution set to fixed
- Status changed from assigned to closed
With 6.7 Beta 1 releasing shortly, I'm closing this ticket as Fixed
. Any follow-up work can happen in new tickets. If there's additional iteration needed here specifically, this can be reopened as a Task (Blessed)
in 6.7
.
This ticket was mentioned in PR #7482 on WordPress/wordpress-develop by @apermo.
2 months ago
#57
Fixed visibilty for newly added functions prior 6.7 release.
CC @dmsnell
Namely:
WP_Debug_Data::get_wp_dropins() WP_Debug_Data::get_wp_server() WP_Debug_Data::get_wp_media() WP_Debug_Data::get_wp_mu_plugins() WP_Debug_Data::get_wp_constants() WP_Debug_Data::get_wp_database()
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
#58
@
2 months ago
- Type changed from enhancement to task (blessed)
I'm re-opening to merge a visibility fix, which slipped past us in review. Some of the new methods are private and some are public, when they all ought to be private for now.
This ticket was mentioned in Slack in #core by dmsnell. View the logs.
2 months ago
#63
@
2 months ago
- Resolution fixed deleted
- Status changed from closed to reopened
@desrosj should we be making these formatting changes in separate tickets? I've wondered this question, because in order to ensure limited scope, the changes here have been focused on extracting code from the single large debug_data()
method into separate task-specific sub-methods.
there are a number of existing patterns in the code that I'm sure people want to change, but I'm worried that making those at the same time, or at least on the same ticket, as moving code from one place to another, will dilute the focus and history of what's happening.
asking just in case the context wasn't clear - this ticket is not about introducing new code or changing coding patterns apart from splitting code off into chunks that can be more safely analyzed, reviewed, and tested in isolation.
@davidbaumwald I'm going to re-open this as a Task (Blessed)
for 6.7 and focus on getting the remaining work through before RC1, which I believe should be easy. thankfully, most of the questions arising from the refactor have been addressed, and while some additional ones may show up, I think the remainder of the sub-tasks will go smoothly.
if this is not the right thing to do then I'm happy to change or create a new Ticket, such as Finish the work of #61648 before RC1
or whatever is appropriate.
2 months ago
#64
@apermo I've updated this branch by reverting the past two commits/merges and then merging in trunk
. it took a bit of manual work because @aaronjorbin, @peterwilsoncc, and @SergeyBiryukov also made changes to the wp-core
code in the meantime, but I think I properly resolved the conflicts and incorporated their changes.
this is now ready for final review, and if you are one of those others, can you double-check that I didn't accidentally undo your changes because I wasn't aware of them?
2 months ago
#65
@apermo I proposed an alternative mechanism for removing sections. please let me know what you think of it. this change is bigger, and as experience tells us, more sensitive to review. but I think ultimately it will be nice to give the new methods the ability to remove their own section, and helps avoid recreating the stack of conditionals for section inclusion or removal. in other words, it encourages keeping the logic for each section local to that section.
This ticket was mentioned in PR #7507 on WordPress/wordpress-develop by @apermo.
2 months ago
#72
'wp-active-theme' => self::get_wp_active_theme(),
'wp-parent-theme' => self::get_wp_parent_theme(),
'wp-themes-inactive' => self::get_wp_themes_inactive(),
---
Follow up to https://github.com/WordPress/wordpress-develop/pull/7027 as discussed with @dmsnell and @costdev
Summary of the coming steps:
- wp-filesystem
- wp-constants
- wp-database
- wp-server
- wp-media
- wp-plugins-active & wp-plugins-inactive
- wp-mu-plugins
8-10. wp-themes-inactive, wp-parent-theme & wp-active-theme (we are here)
Refactoring WP_Debug_Data::debug_data(); into smaller functions for
- better maintainability
- reduced complexity
- preparation for future unit tests
- improved extensibility (see trac for details)
- added php7.2 style type hints
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket
2 months ago
#75
Github's diffing utility is having a hard time with this patch. For those wanting to review it, I encourage using an external diff tool, or change the algorithm with git diff --patience
, for example.
2 months ago
#76
@apermo I pushed a commit with a few changes. I would appreciate your review of two in particular:
- I removed what appears to be unused variables in
debug_data()
- perhaps we overlooked removing those in an earlier commit. - In
get_wp_parent_theme()
I replaced theif ( $parent_theme )
with an early abortif ( ! $parent_theme )
. this was not only to remove the level of indentation, but I believe it was previously returning an undefined variable$fields
since that was set inside theif
structure.
This ticket was mentioned in PR #7509 on WordPress/wordpress-develop by @kebbet.
2 months ago
#79
https://core.trac.wordpress.org/ticket/61648
Not sure about the line 32 * @throws ImagickException
, it should possibly move to the media function.
#80
@
2 months ago
- Resolution fixed deleted
- Status changed from closed to reopened
Docblocks needs an update to. Missed during latest commits.
2 months ago
#82
@apermo Any suggestions or insights in the
@throws
in debug_data docblock?
That one is correct.
get_wp_media()
can possibly throw that, and it will bubble up to debug_data()
, since there is no try catch
so it is correct to keep the @throws ImagickException
in line 32.
But thanks for catching the other doc error.
#84
@
7 weeks ago
- Keywords needs-docs added
Dennis is currently on sabbatical.
RC1 is due out any moment. It looks like there are only some outstanding doc changes, so marking needs-docs
.
there are a number of existing patterns in the code that I'm sure people want to change, but I'm worried that making those at the same time, or at least on the same ticket, as moving code from one place to another, will dilute the focus and history of what's happening.
The commit above fixed the results of composer format
, which should be run before committing to ensure the coding standards are consistently applied to all new code.
@peterwilsoncc commented on PR #7509:
7 weeks ago
#86
#87
@
7 weeks ago
- Keywords dev-feedback added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening to consider [59290] for backport to the 6.7 branch pending another committers sign off.
Refactored WP_Debug_Data::debug_data(); into smaller functions for
$parent_theme_auto_update_string
showing the value from$auto_updates_string
Tested the code multiple times, to ensure that the output is identical to before, as expected only server time did change.
Trac ticket: https://core.trac.wordpress.org/ticket/61648#ticket