Skip to content

PHP 8.0 | Wrong error is raised when creating new instances with FQN #3131

@ArthurHoaro

Description

@ArthurHoaro

With PHP 8.0 (beta4) instantiating a new instance with a FQN (i.e. including the namespace), PHPCS will raise the following unrelated issue:

Parentheses must be used when instantiating a new class

Code sample

<?php namespace ArthurHoaro; class PhpCsBugReport { public function method(): void { $foo = new \DateTime(); $foo = new \Other\Namespance\Unknown(); $foo = new Unknown(); } }

Custom ruleset
N/A

To reproduce
Steps to reproduce the behavior:

  1. Create a file called PhpCsBugReport.php with the code sample above.
  2. Run composer require "squizlabs/php_codesniffer"
  3. Using PHP 8.0 run vendor/bin/phpcs --standard=PSR12 PhpCsBugReport.php.
  4. See error message displayed
FILE: /usr/src/myapp/PhpCsBugReport.php ---------------------------------------------------------------------- FOUND 2 ERRORS AFFECTING 2 LINES ---------------------------------------------------------------------- 9 | ERROR | [x] Parentheses must be used when instantiating a new | | class 10 | ERROR | [x] Parentheses must be used when instantiating a new | | class ---------------------------------------------------------------------- PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY ---------------------------------------------------------------------- 

Expected behavior
Same behavior as with PHP 7.x, no issue should be raised here.

Versions (please complete the following information):

  • OS: Ubuntu 20.04
  • PHP: 8.0.0-beta4
  • PHPCS: 3.5.6
  • Standard: PSR12

Additional information

Running PHPCBF will generate a PHP syntax error:

<?php namespace ArthurHoaro; class PhpCsBugReport { public function method(): void { $foo = new() \DateTime(); $foo = new() \Other\Namespance\Unknown(); $foo = new Unknown(); } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions