Skip to content

Key should be quoted when it contains spaces in ArrayShapeItemNode #251

Not planned
@ruudk

Description

@ruudk
Contributor

I noticed something odd.

$input = "array{states: array{'Hello World': int}}";
$expected = "array{states: array{'Hello World': int}}";
$lexer = new Lexer();
$constExprParser = new ConstExprParser(true, true);
$typeParser = new TypeParser($constExprParser);
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);

$tokens = new TokenIterator($lexer->tokenize($input));
$phpDocNode = $phpDocParser->parseTagValue($tokens, '@var');

$this->assertInstanceOf(VarTagValueNode::class, $phpDocNode);
$this->assertInstanceOf(ArrayShapeNode::class, $phpDocNode->type);

$printer = new Printer();
$this->assertSame($expected, $printer->print($phpDocNode));

Failed asserting that two strings are identical.
Expected :'array{states: array{'Hello World': int}}'
Actual :'array{states: array{Hello World: int}}'

As you can see, the key Hello World is not quoted, while it should.

When I try to feed the wrong PHPDoc into parseTagValue it returns InvalidTagValueNode.

Am I doing something wrong, or is this a bug that I can try to fix 😊 ?

Activity

added a commit that references this issue on Sep 6, 2024
2cd0981
ruudk

ruudk commented on Sep 6, 2024

@ruudk
ContributorAuthor

I think the same problem can happen for object key names. Those keys can also contain spaces.

Created a PR to fix it: #252

added 2 commits that reference this issue on Sep 6, 2024
added a commit that references this issue on Sep 6, 2024
b3dc1d9
ruudk

ruudk commented on Sep 6, 2024

@ruudk
ContributorAuthor

I'm sorry for this, but the problem was that my TypeParser did not have $quoteAwareConstExprString enabled. While my ConstExprParser did. I didn't notice I had to configure it in 2 locations.

ondrejmirtes

ondrejmirtes commented on Sep 7, 2024

@ondrejmirtes
Member

Yeah, I'm aware of this potential issue and that's why for 2.0 I'm changing the constructor to accept a shared ParserConfig object: 9d57f3d

I know that this "service locator" is an anti-pattern, but you gotta know the rules so that you can know when to break them 😂

Also: All of the current "options" in 1.x are there for the same reason bleeding edge exists: They represent new behaviour which will become the default (and only) behaviour in 2.0. That's why the 2.0 removes all of these options now.

I'm adding ParserConfig class where the new options during 2.x series development will be added and shared across services 👍

github-actions

github-actions commented on Oct 9, 2024

@github-actions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

locked as resolved and limited conversation to collaborators on Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ruudk@ondrejmirtes

      Issue actions

        Key should be quoted when it contains spaces in ArrayShapeItemNode · Issue #251 · phpstan/phpdoc-parser