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

PHP8 - multiple attributes in same #[] bracket notation with \ in name fails, e.g. Assert\NotBlank in certain combination. #1974

Closed
AndreasA opened this issue May 4, 2022 · 4 comments · Fixed by #1987

Comments

@AndreasA
Copy link
Contributor

AndreasA commented May 4, 2022

Not sure if it is actually the plugin or the php parser but the code snippet fails to be formatted and actually fails with an exception.

For this issue to occur some specific requirements have to be met:

  • if \ is used for the argument, e.g. fully qualified class name.
  • At least two attributes are used in the same #[] bracket.
  • At least two of them use \.
  • At least two attributes use two arguments or more.

Prettier 2.6.2

PHP Plugin 0.18.4

Options:

{
  "printWidth": 80,
  "tabWidth": 4,
  "useTabs": false,
  "singleQuote": true,
  "phpVersion": "8.1",
  "trailingCommaPHP": true,
  "braceStyle": "psr-2",
  "requirePragma": false,
  "insertPragma": false
}

Input:

<?php declare(strict_types=1);

namespace Foo;

use Symfony\Component\Validator\Constraints as Assert;

class ValueModel
{
    #[
        Assert\NotBlank(allowNull: false, groups: ['foo']),
        Assert\Length(max: 255, groups: ['foo']),
    ]
    public ?string $value = null;
}

Output:

Parse Error : syntax error, unexpected '\' (T_NS_SEPARATOR), expecting ']' on line 11

   9 |     #[
  10 |         Assert\NotBlank(allowNull: false, groups: ['foo']),
> 11 |         Assert\Length(max: 255, groups: ['foo']),
     |              ^
  12 |     ]
  13 |     public ?string $value = null;
  14 | }

Expected behavior:
Not to fail.

Playground example

@AndreasA
Copy link
Contributor Author

AndreasA commented May 4, 2022

As mentioned this might actually be due to issues with the php-parser: glayzzle/php-parser#486

@AndreasA
Copy link
Contributor Author

AndreasA commented May 23, 2022

php-parser just released a new version https://www.npmjs.com/package/php-parser/v/3.1.0-beta.6 which fixes various attribute issues - which should include this one.

@czosel
Copy link
Collaborator

czosel commented May 23, 2022

@AndreasA correct - I wanted to bump the parser dependency here already, but ran into a regression: glayzzle/php-parser#934
I hope that we can get it fixed soon, then shipping this fix (and others) should be simple.

@czosel
Copy link
Collaborator

czosel commented May 23, 2022

Fixed in v0.18.5 🎉

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

Successfully merging a pull request may close this issue.

2 participants