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

False positive in Universal.CodeAnalysis.ConstructorDestructorReturn #201

Closed
1 task done
anomiex opened this issue Jan 4, 2023 · 3 comments · Fixed by #202
Closed
1 task done

False positive in Universal.CodeAnalysis.ConstructorDestructorReturn #201

anomiex opened this issue Jan 4, 2023 · 3 comments · Fixed by #202

Comments

@anomiex
Copy link

anomiex commented Jan 4, 2023

Bug Description

The sniff Universal.CodeAnalysis.ConstructorDestructorReturn false-positives if an anonymous function is used inside a constructor. Presumably this also happens for destructors.

Given the following reproduction Scenario

The issue happens when running this command:

vendor/bin/phpcs -s --standard=Universal --sniffs=Universal.CodeAnalysis.ConstructorDestructorReturn test.php

... over a file containing this code:

<?php

class Foo {
    public function __construct() {
        do_something(
            function () {
                return true;
            },
        );

        $this->callback = function () {
            return false;
        };
    }
}

I'd expect the following behaviour

No warnings or errors.

Instead this happened

FILE: /tmp/test/test.php
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7 | WARNING | A "__construct()" magic method can not return a value. Found: "return true;"
    |         | (Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound)
 12 | WARNING | A "__construct()" magic method can not return a value. Found: "return false;"
    |         | (Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Environment

Environment Answer
PHP version 8.0.26
PHP_CodeSniffer version 3.7.1
PHPCSExtra version 1.0.0
PHPCSUtils version 1.0.0
Install type Composer project local

Additional Context (optional)

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of PHPCSExtra.
@jrfnl
Copy link
Member

jrfnl commented Jan 4, 2023

@anomiex Thanks for reporting this! I have confirmed the bug. Fix upcoming.

@jrfnl
Copy link
Member

jrfnl commented Jan 4, 2023

PR #202 should fix the issue.

@jrfnl jrfnl closed this as completed in #202 Jan 4, 2023
@anomiex
Copy link
Author

anomiex commented Jan 5, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants