Skip to content

class_definition fails with initialising anonymous class #5463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
artursvonda opened this issue Jan 22, 2021 · 6 comments
Closed

class_definition fails with initialising anonymous class #5463

artursvonda opened this issue Jan 22, 2021 · 6 comments
Labels
kind/bug status/input required For the issue to be confirmed or the PR to be process; additional input of the author is required

Comments

@artursvonda
Copy link

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
};
@kubawerlos
Copy link
Contributor

I cannot reproduce it, your example always is fixed once (space after class is removed) and that's it. Can you provide some a failing test case?

@kubawerlos kubawerlos added the status/input required For the issue to be confirmed or the PR to be process; additional input of the author is required label Jan 26, 2021
@artursvonda
Copy link
Author

Sorry, I think this was my fault. I thought I had isolated everything while testing but it seems CodeSniffer formatter was applied by IDE after CS Fixer run. Closing the ticket.

@juuuuuu
Copy link

juuuuuu commented Feb 10, 2021

I cannot reproduce it, your example always is fixed once (space after class is removed) and that's it. Can you provide some a failing test case?

Should the space after class be removed?
Looking at the PSR12 documentation, it's written:

[Anonymous Classes] MUST be declared with a space after the [class] keyword

I'm facing the same problem regarding PSR12 rules, where PHPCS tries to add the space between class and PHP-CS-Fixer tries to remove the space here new class().

@SpacePossum
Copy link
Contributor

Hi @juuuuuu ,

Could you report this as a new FR for PSR12 suport? Currently PHP-CS-Fixer will indeed remove the space, thanks!

@juuuuuu
Copy link

juuuuuu commented Apr 2, 2021

Sorry for late response, I'll do it.

keradus added a commit that referenced this issue Aug 28, 2021
…Possum)

This PR was merged into the master branch.

Discussion
----------

ClassDefinitionFixer - PSR12 for anonymous class

Adds PSR12 support for `anonymous class` notation with a space following `new class`, like `new class (1,2) {}` (note that `new class {}` is currently already covered)

Closes: #5463 (comment)
Also reported here loophp/collection#102 (comment) (cc `@drupol` )
Closes one of the points here #4502
New feature so targets `master`, no BC break, new behavior through configuration (by default configuration acts the same)

Commits
-------

4d84a83 ClassDefinitionFixer - PSR12 for anonymous class
@C-Duv
Copy link

C-Duv commented Jul 11, 2022

Looking at the PSR12 documentation, it's written:

[Anonymous Classes] MUST be declared with a space after the [class] keyword

I have a similar issue where PHP-CS-Fixer and PHPCS fight over the space between class and ($var) in:

$instance = new class ($var) extends \Foo {};

It looks like the quoted part of the documentation was removed and I cannot find a clear position of what to allow/forbid here.

There was some work in the php-fig/fig-standards#1206 PR that seems to remove the space, but it was not merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug status/input required For the issue to be confirmed or the PR to be process; additional input of the author is required
Projects
None yet
Development

No branches or pull requests

5 participants