Skip to content
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

Problem with custom attribute #5533

Closed
OskarStark opened this issue Mar 9, 2021 · 21 comments · Fixed by #5596 or #5665
Closed

Problem with custom attribute #5533

OskarStark opened this issue Mar 9, 2021 · 21 comments · Fixed by #5596 or #5665
Assignees
Labels

Comments

@OskarStark
Copy link
Contributor

OskarStark commented Mar 9, 2021

Bug report

PHP 8.0.2 (cli) (built: Feb  4 2021 18:10:29) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.2, Copyright (c), by Zend Technologies
PHP CS Fixer 2.18.2 Remote Void by Fabien Potencier and Dariusz Ruminski
Runtime: PHP 8.0.2
php vendor/bin/php-cs-fixer fix src/Controller/Security/Abo/ConfirmAboController.php --verbose

Code:

<?php

declare(strict_types=1);

namespace App\Controller\Security\Abo;

use App\Entity\Abo;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/abo/confirm/{aboSecret}', name: 'foo')]
final class ConfirmAboController
{
    public function __invoke(Abo $abo, Request $request, #[CurrentUser] ?User $user = null): Response
    {
return new Response('hi');
    }
}

leads to:

Files that were not fixed due to errors reported during fixing:
   1) /Users/oskar.stark/dev/cockpit/src/Controller/Security/Abo/ConfirmAboController.php
error sending signal urgent I/O condition os: process already finished
exit status 64

Changing to:

-    public function __invoke(Abo $abo, Request $request, #[CurrentUser] ?User $user = null): Response
+    public function __invoke(Abo $abo, Request $request, #[CurrentUser] ?User $user): Response

ends up in a non ending process and my macbook starts to explode 🤔

Changing to:

-    public function __invoke(Abo $abo, Request $request, #[CurrentUser] ?User $user): Response
+    public function __invoke(Abo $abo, Request $request, #[CurrentUser] User $user): Response

Cs-fixer can fix and lint it, but my code is not working anymore 😄

cc @localheinz @derrabus

@localheinz
Copy link
Member

As a workaround, perhaps use annotations for now?

@OskarStark
Copy link
Contributor Author

Unfortunately this is not possible for the CurrentUser attribute 😞

@derrabus
Copy link
Contributor

The workaround would be to remove the attribute and change the type hint to UserInterface. Anyhow, CS Fixer should not explode when using that attribute. 😉

@OskarStark
Copy link
Contributor Author

OskarStark commented Mar 10, 2021

I checked this, but unfortunately:

#[CurrentUser] ?UserInterface $user = null

nor

#[CurrentUser] ?UserInterface $user

nor

#[CurrentUser] UserInterface $user = null

are working @derrabus 🤔
Without been nullable all is fine 👍

@derrabus
Copy link
Contributor

derrabus commented Mar 10, 2021

No, but this works:

?UserInterface $user = null

@OskarStark
Copy link
Contributor Author

You mean without the attribute? 🤔

@keradus
Copy link
Member

keradus commented Mar 10, 2021

@OskarStark , thanks for raising the concern.
Are you able to raise a PR to fix it?

also, pls share the configuration ruleset that you are using, to help reproducing the issue

@OskarStark
Copy link
Contributor Author

OskarStark commented Mar 11, 2021

@OskarStark , thanks for raising the concern.

You are welcome, thanks for your hard work on this tool 👍

Are you able to raise a PR to fix it?

Unfortunately I am not so deep ion this topic and have a lot of work on my desk 😢

also, pls share the configuration ruleset that you are using, to help reproducing the issue

Is there a way I can dump the used config?

@derrabus
Copy link
Contributor

@OskarStark Usually, your project has a .php_cs or .php_cs.dist file that contains the configuration.

@jrmajor
Copy link
Contributor

jrmajor commented Apr 5, 2021

@keradus @derrabus

I've managed to reproduce this bug with the following code:

<?php

function test(#[Attribute] ?User $user) {}

The problem is in TernaryToElvisOperatorFixer. Nullable type hint is treated as a part of a ternary operator.

https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/9bf2760ce9d2218cc127cb4106e9ddfedca4eb3a/src/Fixer/Operator/TernaryToElvisOperatorFixer.php#L126-L134

$beforeOperator == ['start' => 6, 'end' => 8], which is #[Attribute].

Then inside getAfterOperator() it falls into an infinite loop, because getNextMeaningfulToken() returns the first token after returning the last one:

https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/9bf2760ce9d2218cc127cb4106e9ddfedca4eb3a/src/Fixer/Operator/TernaryToElvisOperatorFixer.php#L210-L219

Unfortunately, I don't know how to fix this, but I hope this will help you.

@kubawerlos
Copy link
Contributor

@OskarStark, @derrabus, @jrmajor I've made a fix ☝🏼 - do not hesitate and drop a review there, please.

@OskarStark
Copy link
Contributor Author

Please reopen this issue and see my comment in the referenced PR.

The PR which closed this issue is not solving the issue.

Thanks

@kubawerlos
Copy link
Contributor

kubawerlos commented Apr 6, 2021

@OskarStark can you check if your problem is present in v2.18.5?

@OskarStark
Copy link
Contributor Author

Sure, I did and it is not working:
CleanShot 2021-04-07 at 08 42 33

@kubawerlos
Copy link
Contributor

@OskarStark can you share the config or output when running with -v flag? I cannot reproduce it.

@kubawerlos kubawerlos reopened this Apr 7, 2021
@OskarStark
Copy link
Contributor Author

Sure, will come back tomorrow 👍

keradus added a commit that referenced this issue Apr 13, 2021
This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

Tokens - fix for checking block edges

Found this when investigating #5533.

[This loop](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v2.18.4/src/Fixer/Operator/TernaryToElvisOperatorFixer.php#L210) was problematic, `)` was passed to `findBlockEnd` and it was returning index of `(` and thus loop was infinitive.

See separate commits for better understanding if needed.

Commits
-------

eb4a86b Tokens - fix for checking block edges
@OskarStark
Copy link
Contributor Author

Running
symfony php vendor/bin/php-cs-fixer fix --config=.php_cs.dist --diff --diff-format=udiff -v

produces:

PHP CS Fixer 2.18.6 Remote Void by Fabien Potencier and Dariusz Ruminski
Runtime: PHP 8.0.3
Loaded config datana (PHP 7.4) from ".php_cs.dist".
Using cache file "/Users/oskar.stark/dev/datana/cockpit/.build/php-cs-fixer/.php_cs.cache".
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSEESSSSSSSSESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error

Fixed all files in 0.126 seconds, 20.000 MB memory used

Files that were not fixed due to errors reported during linting after fixing:
   1) src/Controller/Registration/RegistrationController.php
   2) src/Controller/Registration/VerifyEmailController.php
   3) src/Controller/Security/Abo/ConfirmAboController.php
exit status 64

This is my config:

<?php

declare(strict_types=1);

use Datana\PhpCsFixer;

$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php74(''), [
    'php_unit_internal_class' => false,
]);

$config->getFinder()
    ->in('build')
    ->in('migrations')
    ->in('src')
    ->in('tests')
    ->in('tools')
;

$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php_cs.cache');

return $config;

this is the ruleset:
https://gist.github.com/OskarStark/6a3c60f384e5a6b57e3958b02a7ae2df

Anything else I can do?
Thanks for your help

@keradus
Copy link
Member

keradus commented Apr 28, 2021

can you share minimum code sample that reproduces the issue? or it's the same as in first msg in this thread?

@OskarStark
Copy link
Contributor Author

It is the same I mentioned

@kubawerlos
Copy link
Contributor

Failing test (and soon hopefully a fix): #5665

@OskarStark
Copy link
Contributor Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants