The Drupal.Classes.ClassCreateInstance.ParenthesisMissing can cause syntax error.

Checking a php file with the following content will result in this validation error: "Calling class constructors must always include parentheses"
This is correct, but all of them could be corrected automatically.

<?php

$obj1 = new DateTime;
$obj1 = new \DateTime;
$obj2 = $obj1->add(new \DateTime);
$obj2 = $obj1->add(new Vendor);
$obj2 = $obj1->add(new Vendor\DateTools);
$obj2 = $obj1->add(new Vendor\DateTools\DateInterval);
$obj2 = $obj1->add(new \Vendor\DateTools\DateInterval);

The fixer includes the required parentheses in wrong place when namespace separators are present. The result will look like this:

<?php

$obj1 = new DateTime();
$obj1 = new \DateTime;
$obj2 = $obj1->add(new \DateTime);
$obj2 = $obj1->add(new Vendor());
$obj2 = $obj1->add(new Vendor()\DateTools);
$obj2 = $obj1->add(new Vendor()\DateTools\DateInterval);
$obj2 = $obj1->add(new \Vendor\DateTools\DateInterval);

The correct result should be:

<?php

$obj1 = new DateTime();
$obj1 = new \DateTime();
$obj2 = $obj1->add(new \DateTime());
$obj2 = $obj1->add(new Vendor());
$obj2 = $obj1->add(new Vendor\DateTools());
$obj2 = $obj1->add(new Vendor\DateTools\DateInterval());
$obj2 = $obj1->add(new \Vendor\DateTools\DateInterval());

Comments

smatyas created an issue. See original summary.

smatyas’s picture

  • klausi committed 9a5656d on 8.x-2.x
    Issue #2618020: Fixed class parenthesis fixer with namespace separators
    
klausi’s picture

Status: Active » Fixed

Committed a fix, thanks for reporting!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.