Skip to content

class_definition fails with initialising anonymous class #5463

@artursvonda

Description

@artursvonda

Bug report

Found an issue where php-cs-fixer keeps reporting issues with anonymous classes and not fixing them. Managed to reduce the bug to snippet below.

> format-php-cs-fixer: php-cs-fixer fix --allow-risky=yes '-vvv' 'src/fixer-test.php'
Executing command (CWD): php-cs-fixer fix --allow-risky=yes '-vvv' 'src/fixer-test.php'
PHP CS Fixer 2.18.1 Remote Void by Fabien Potencier and Dariusz Ruminski
Runtime: PHP 7.4.12
Loaded config default from "/var/www/html/app/.php_cs.dist".
Using cache file ".php_cs.cache".
Paths from configuration file have been overridden by paths provided as command arguments.
F
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) src/fixer-test.php (class_definition)

Fixed all files in 0.046 seconds, 14.000 MB memory used
<?php

declare(strict_types=1);

$srcFiles = PhpCsFixer\Finder::create()
    ->in(__DIR__)
    ->name('*.php')
    ->path(['src/'])
    ->ignoreVCSIgnored(true);

$finder = $srcFiles->append($rootFiles);

$config = new PhpCsFixer\Config();

return $config
    ->setRules([
        'class_definition' => true,
    ])
    ->setFinder($finder);

Code snippet that reproduces the problem

<?php
$var = new class () {
    // content
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugstatus/input requiredFor the issue to be confirmed or the PR to be process; additional input of the author is required

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions