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

ThisFoundOutsideClass fires false-positive on anonymous class #1481

Open
1 task done
totten opened this issue Mar 22, 2023 · 3 comments
Open
1 task done

ThisFoundOutsideClass fires false-positive on anonymous class #1481

totten opened this issue Mar 22, 2023 · 3 comments
Labels
Milestone

Comments

@totten
Copy link

totten commented Mar 22, 2023

Bug Description

The sniff ThisFoundOutsideClass fires a false-positive on $this when nested inside an anonymous class and a closure.

Given the following reproduction scenario

The issue happens when running this command:

./vendor/bin/phpcs -ps example.php --standard=PHPCompatibility --runtime-set testVersion 7.4-8.1

... over a file containing this code:

<?php

return function () {

  $obj = new class() {
    protected $x = 0;
    
    public function increment(): int {
      return ++$this->x;
    }
  };

};

with the built-in ruleset.

I'd expect the following behaviour

Pass

Instead this happened

FILE: /tmp/PHPCompatibility/example.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 9 | WARNING | Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.
   |         | (PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Environment

Environment Answer
PHP version 8.1.6; 7.4.29
PHP_CodeSniffer version 3.7.2
PHPCompatibility version develop (circa 4576a17)
Install type composer

Additional Context (optional)

This issue was pointed out to me as totten/civix#294. I can see it happening, so I'm forwarding it along with a tighter description.

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of PHPCompatibility.
@jrfnl jrfnl added this to the 10.0.0 milestone Mar 22, 2023
@jrfnl jrfnl added the bug label Mar 22, 2023
@jrfnl
Copy link
Member

jrfnl commented Mar 22, 2023

@totten Thanks for reporting this. I have confirmed the issue and lined up a fix, which will be pulled & merged before the 10.0.0 release.

@jrfnl
Copy link
Member

jrfnl commented Mar 22, 2023

FYI: it's not even just anonymous classes... looks like named classes and other closed structures can all be declared within a closure: https://3v4l.org/Zb22K

@totten
Copy link
Author

totten commented Mar 22, 2023

FYI: it's not even just anonymous classes... looks like named classes and other closed structures can all be declared within a closure: https://3v4l.org/Zb22K

Ah, cool. Learn something new every day. 😄

@jrfnl jrfnl changed the title ThisFoundOutsideClass fires false-negative on anonymous class ThisFoundOutsideClass fires false-positive on anonymous class Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants