Discovered as part of #2935947: Comply with code standards.

Specifically, {1} only matches a single occurrence of a character, + is for "one or more".

         // First character must be "globally allowed". Length must be >=1.
         self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .
         '(' .
           // As many non-globally allowed characters as desired.
           self::MEMBER_NAME_INNER_ALLOWED_CHARACTERS . '*' .
           // If length > 1, then it must end in a "globally allowed" character.
           self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .

Overall, this is an especially hard to read bit of code regardless. See #2935947-24: Comply with code standards.

Any patch should also address code standard violations.

Comments

gabesullice created an issue. See original summary.

gabesullice’s picture

gabesullice’s picture

Assigned: Unassigned » gabesullice
wim leers’s picture

Overall, this is an especially hard to read bit of code regardless.

Happy to make it more legible. But code that checks spec conformity is bound to be hard to read. That's why it references http://jsonapi.org/format/#document-member-names!

Specifically, {1} only matches a single occurrence of a character, + is for "one or more".

So you're referring to that first line of code that you quoted, right?

gabesullice’s picture

Issue summary: View changes

Happy to make it more legible. But code that checks spec conformity is bound to be hard to read. That's why it references http://jsonapi.org/format/#document-member-names!

Definitely. And it's regex too! I'm really referring to the concatenation across multiple lines. E.g. a sprintf might be a clearer way to do this and pass code sniffs :)

So you're referring to that first line of code that you quoted, right?

Yep, and it's totally possible that I'm misreading the regular expression because of rules that come later in the expression.

wim leers’s picture

Yep, and it's totally possible that I'm misreading the regular expression because of rules that come later in the expression.

That's exactly what I think is happening. To be fair, it is confusing:

         // First character must be "globally allowed". Length must be >=1.
         self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .

This comment applies to the ENTIRE regexp. Note this is indented less than what follows:

           // As many non-globally allowed characters as desired.
           self::MEMBER_NAME_INNER_ALLOWED_CHARACTERS . '*' .
           // If length > 1, then it must end in a "globally allowed" character.
           self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .

All thee rules together comply with what the spec says:

  • Member names MUST contain at least one character.
  • Member names MUST contain only the allowed characters listed below.
  • Member names MUST start and end with a “globally allowed character”, as defined below.

So AFAICT this is correct. But if we can clarify the comments/code, then great. OTOH, I think there's little value in doing so for something like this, precisely because it's the codifying of a spec, and comparing the code with the spec should always lead to readers ending up concluding it is correct. Hence, agreed with Minor priority.

gabesullice’s picture

gabesullice’s picture

Category: Bug report » Task
gabesullice’s picture

Status: Active » Closed (works as designed)