squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

PSR12.Classes.ClassInstantiation.MissingParentheses false positive when instantiating parent classes #3668

Closed BigBadBassMan closed 2 years ago

BigBadBassMan commented 2 years ago

Describe the bug When instantiating a parent class using the keyword "parent", PHPCS flags this as wrong and PHPCBF inserts brackets after the new thus breaking code.

Code sample see also: https://3v4l.org/5WDl4

class SomeClass extends ParentClass
{
//...
    public function foo(): static
    {
        $class = new parent();
        //...
    }
}

To reproduce Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed

FOUND 1 ERROR AFFECTING 1 LINE

line | ERROR | [x] Parentheses must be used when instantiating a new class | | (PSR12.Classes.ClassInstantiation.MissingParentheses)



**Expected behavior**
No error report and no changed code from PHPCBF.

**Versions (please complete the following information):**
 - OS: Linux
 - PHP: 8.0
 - PHPCS: 3.7.1
 - Standard: PSR12
jrfnl commented 2 years ago

@BigBadBassMan Thanks for reporting this! I can confirm this is a regression introduced in PHPCS 3.7.0. PR #3669 should fix this. Testing appreciated.

BigBadBassMan commented 2 years ago

@jrfnl Thanks for the extremely speedy fix, confirmed to work! Cannot wait to see this merged and released!

jrfnl commented 2 years ago

@BigBadBassMan Thanks for testing and confirming the fix!

gsherwood commented 2 years ago

Thanks a lot @jrfnl for fixing this and similar issues in 2 other sniffs. Thanks for the bug report @BigBadBassMan.

gautamverma40 commented 1 year ago

@gsherwood Thanks for the update. Not sure if there is any reason behind this but the updated tag is not available on Packagist yet. Can you push the tag there as well so that it can be installed via composer easily?

jrfnl commented 1 year ago

@gautamverma40 PHPCS 3.7.2 hasn't been released yet...