PHPUnit remove $this on assertion

Please provide

  • Operating system: MacOS Sonoma 14.2.1
  • SonarLint plugin version: v4.3.0
  • Programming language you’re coding in: php
  • Is connected mode used: no
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

In one of my PHPUnit tests, the error “$this” should not be used in a static context (php:S2014) have been raised when I use any of the PHPUnit assertions as follows.

$this->assertTrue(true); // just an example

Hi @Diego_Surita,

welcome to the community. Following the PHPUnit documentation the global function wrappers would allow just the use of assertTrue() which would follow the Clean Code strategy. Nevertheless, this rule should not apply to test code in general. To just run PHPUnit-related rules on your test code, you have to configure your project to help SonarLint understanding what’s your main code and what’s your test code. Just look for Test File Pattern in the SonarLint configuration in VSCode.


1 Like

Ok, thank you.