Describe the bug
In some cases anonymous classes with extends and implements cause a ParseError after "fixing" the file.
It happens if there is no space between the last interface-name and the {, see following code samples.
The following happens, see code samples at the end:
- The
new keyword gets additional () braces
- The interface-names don't get indented and lined up correctly
- The opening
{ for the class gets removed
- The methods get wrong
Code sample
<?php
namespace Jwittorf\Psr12Linting\Psr12;
use Jwittorf\Psr12Linting\UseableClass;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfaces;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesB;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesC;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesD;
$instance = new class() extends UseableClass implements MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC, MultipleInterfacesD{
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
$instance2 = new class extends UseableClass implements MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC, MultipleInterfacesD{
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
Custom ruleset
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer">
<file>src</file>
<!-- Don't hide tokenizer exceptions -->
<rule ref="Internal.Tokenizer.Exception">
<type>error</type>
</rule>
<rule ref="PSR12"/>
</ruleset>
To reproduce
Steps to reproduce the behavior:
- Create a file called
test.php with the code sample above...
- Run
phpcs test.php ...
- See messed up PHP code below
<?php
namespace Jwittorf\Psr12Linting\Psr12;
use Jwittorf\Psr12Linting\UseableClass;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfaces;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesB;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesC;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesD;
$instance = new() class () extends UseableClass implements
MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC, MultipleInterfacesD
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
$instance2 = new() class extends UseableClass implements
MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC, MultipleInterfacesD
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
Expected behavior
I would expect the correct formatting like the code below with following "steps":
- Put every interface-name on an own line, indented 4 spaces
- Move the opening
{ into an own line, no indention
- DON'T try to add
() after the new keyword
<?php
namespace Jwittorf\Psr12Linting\Psr12;
use Jwittorf\Psr12Linting\UseableClass;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfaces;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesB;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesC;
use Jwittorf\Psr12Linting\Psr4\MultipleInterfacesD;
$instance = new class() extends UseableClass implements
MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC,
MultipleInterfacesD
{
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
$instance2 = new class extends UseableClass implements
MultipleInterfaces,
MultipleInterfacesB,
MultipleInterfacesC,
MultipleInterfacesD
{
public function __construct()
{
echo "Created anonymous class in file '" . __FILE__ . "'.\n";
}
public function someFunctionToBeImplemented(): void
{
echo "This text was created with the function " . __FUNCTION__ . " in file '" . __FILE__ . "'.\n";
}
};
Versions:
- OS: MacOS 12.6
- PHP: 7.4.33
- PHPCS: 3.7.1
- Standard: PSR12
Describe the bug
In some cases anonymous classes with
extendsandimplementscause aParseErrorafter "fixing" the file.It happens if there is no space between the last interface-name and the
{, see following code samples.The following happens, see code samples at the end:
newkeyword gets additional()braces{for the class gets removedCode sample
Custom ruleset
To reproduce
Steps to reproduce the behavior:
test.phpwith the code sample above...phpcs test.php ...Expected behavior
I would expect the correct formatting like the code below with following "steps":
{into an own line, no indention()after thenewkeywordVersions: