PrefixAllGlobals: add recording of metrics #1437
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When a project which wasn't using WPCS, starts to use it or when an project upgrades to WPCS > 0.12.0, one of the things the project most likely will run into is the prefixing of all globals.
When no prefixes are passed, the sniff will not throw any errors.
However, if the project does want to comply and wants to add a prefix configuration, it would be useful to get some insight into which prefixes are currently in use in a project and how often each is used to determine what prefixes to allow and/or what prefixes to consolidate.
PHPCS offers a metrics mechanism which can be used for this, the results of which can be viewed using the
--report-info
argument.This PR takes advantage of that metrics report to:
Example workflow and output when run against the current `trunk` of the bbPress plugin (fold out for the details)
When running WPCS against bbPress
trunk
initially with someruntime-set
prefixes, 115 errors are reported:Now, obviously you can use the
full
report to go through the details, but figuring out if there are persistent prefixes which have been overlooked can - with a large report - be a lot of work.With the code in this PR, you could now run the following command:
... and see the exact break-down of the potential prefixes of these 115 violations:
N.B.: Yes, I know, there is one missing - if the function name would be
__
and in some other exceptional circumstances, no valid potential prefix can be determined, which is why the total can be slightly off.I imagine that if this PR is accepted, it would be useful to add a wiki page about how to get access to this information and how to use it.
Notes about the implementation:
$
signs from the name as found.This matches
Pascal
inPascalCase
,camel
incamelCase
,snake
insnake_case
.exclude-pattern
, prevents the error from being recorded, no "potential prefix" metric will be recorded.private
methods, this is not a BC-break.