Skip to content
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

Squiz/NonExecutableCode: simplify + improve handling of comments and closures #92

Merged
merged 3 commits into from
Dec 5, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 11, 2023

Description

Recreation of upstream PR squizlabs/PHP_CodeSniffer#3898:

Some tweaks for the sniff which I noticed while reviewing squizlabs/PHP_CodeSniffer#3770.

Squiz/NonExecutableCode: make sniff more code style independent

When determining whether a return statement is the last code token in a function body, comments should be ignored, but weren't.

Fixed now. Includes tests.

Squiz/NonExecutableCode: flag redundant return statements in closures too

A return statement which doesn't return a value at the end of a function body would be flagged as "not required" for named functions, but not so for anonymous functions.

Fixed now. Includes tests.

Squiz/NonExecutableCode: fold duplicate code

Follow up on commits 0e10f43 and 01754d9, which both deal with fixing bugs where the sniff would not handle if/elseif/else conditions without curly braces correctly.

This commit merges the two near duplicate code blocks, which the above mentioned commits introduced, each containing code doing essentially the same thing.

Also note that T_ELSE is handled separately now as else does not take parentheses and can therefore not be a parenthesis owner.

This change is already covered by pre-existing tests.


Discussion in the original ticket:

@fredden asked:

While reviewing this pull request, I noticed there is a false-negative in this sniff. With the following test file, I get a warning about Squiz.PHP.NonExecutableCode.ReturnNotRequired on line 9, but not on line 6. Even if I remove line 9, I still don't get a warning about line 6.

<?php

function test() {
    if (true) {
        echo 'yes';
        return;
    }

    return;
}

Is this something which is in scope here, or should I raise this separately so it can be fixed in a different pull request?

To which @jrfnl replied:

In my opinion that is outside the scope of this PR. The ReturnNotRequired check currently only checks for a return statement without a return value, which is directly followed by the function close brace.

Expanding that check to find other potentially redundant return statements could get complex and I think this might even be better off in a separate sniff.

Suggested changelog entry

  • Squiz/NonExecutableCode: redundant return statements just before a function close brace will now be flagged more often.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

jrfnl added 3 commits December 5, 2023 23:00
When determining whether a `return` statement is the last code token in a function body, comments should be ignored, but weren't.

Fixed now. Includes tests.
…es too

A return statement which doesn't return a value at the end of a function body would be flagged as "not required" for named functions, but not so for anonymous functions.

Fixed now. Includes tests.
Follow up on commits 0e10f43 and 01754d9, which both deal with fixing bugs where the sniff would not handle if/elseif/else conditions without curly braces correctly.

This commit merges the two near duplicate code blocks, which the above mentioned commits introduced, each containing code doing essentially the same thing.

Also note that `T_ELSE` is handled separately now as `else` does not take parentheses and can therefore not be a parenthesis owner.

This change is already covered by pre-existing tests.
@jrfnl jrfnl force-pushed the feature/squiz-nonexecutablecode-various-tweaks branch from d8833d5 to 632600b Compare December 5, 2023 22:00
@jrfnl jrfnl merged commit 4c2ba65 into master Dec 5, 2023
64 checks passed
@jrfnl jrfnl deleted the feature/squiz-nonexecutablecode-various-tweaks branch December 5, 2023 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant