Skip to content

PSR12.Classes.ClassInstantiation incorrectly fix when using member vars and some variable formats #2053

Closed
@jrfnl

Description

@jrfnl

WPCS already contained a ClassInstantiation sniff, so after seeing #2047 I've run the PSR12 version over the WPCS unit tests for the sniff to see if we could switch over at some point.

The test run identified a number of bugs in the PSR12 sniffs:

Test cases:

$a = new self::$transport[$cap_string];
$renderer = new $this->inline_diff_renderer;
$a = new ${$varHoldingClassName};

Fixed as:

$a = new self()::$transport[$cap_string];
$renderer = new $this()->inline_diff_renderer;
$a = new $(){$varHoldingClassName};

Expected fixes:

$a = new self::$transport[$cap_string]();
$renderer = new $this->inline_diff_renderer();
$a = new ${$varHoldingClassName}();

👉 Would you like me to look into fixing these ?

It also raises the question Why does FIG ignore this rule for anonymous classes ?, though that's outside the scope of PHPCS.

Other than the above, the WPCS sniff has some additional features.

  • Detect class instantiation without parentheses in JS.
  • Throw error when "Assigning new by reference" is detected (PHP only).
  • Throw fixable error for whitespace between class name and open parenthesis (PHP + JS).
  • Metrics for all errors.

👉 Would you be interested in any of these ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions