Skip to content

Add sniff to detect unused variables and undefined variables #1221

@sirbrillig

Description

@sirbrillig

While it is not exactly a code style concern, the use of undefined variables and creating variables without using them is nearly always a bug.

The following typo is a trivial example, but it is the kind of thing that can easily make it into production code because a particular code path has not been adequately tested.

function update_my_post( $post_data ) {
  wp_insert_post( $post_info );
}

While it's possible to argue that this is the responsibility of testing, I'd make the case that WordPress and its plugins are of sufficient complexity that it's difficult to test all code paths adequately. Perhaps more importantly, WordPress is a vehicle for new developers to learn coding and therefore this project is in a unique position to help those developers avoid introducing new bugs into their own code.

PHPCS can easily detect these instances and prevent a whole class of bugs from appearing. Here is a set of PHPCS sniffs which does just that: https://github.com/sirbrillig/VariableAnalysis

If there's interest, I can work on integrating these sniffs into this ruleset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions