Skip to content

PHPDoc lexer tokenization depends on LC_CTYPE #361

Backlog

Description

@tg-iserv

The lexer produces different tokens for the same PHPDoc depending on LC_CTYPE.
For example, under a Turkish UTF-8 locale, the identifier contentId is split at the uppercase I.
Causing an annotation like @param array{contentId: string} $var to be parsed as an InvalidTagValueNode.
The identifier pattern for this uses [a-z] with case-insensitive matching, which appears to be affected by the locale.

I would expect PHPDoc tokenization to be locale-independent, but please correct me if it is intended behavior.

This can be reproduced with the following small script and

  • phpstan/phpdoc-parser 2.6.1
  • PHP 8.5.13
  • PCRE 10.44.

The Turkish locale must also be installed

<?php

require __DIR__ . '/vendor/autoload.php';

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\ParserConfig;

$lexer = new Lexer(new ParserConfig(usedAttributes: []));
$doc = '/** @param array{contentId: string} $var */';

foreach (['C', 'tr_TR.utf8'] as $locale) {
    if (setlocale(LC_CTYPE, $locale) === false) {
        throw new RuntimeException("Locale unavailable: $locale");
    }

    echo "\n$locale:\n";
    foreach ($lexer->tokenize($doc) as [$value, $type]) {
        printf("%d: %s\n", $type, json_encode($value));
    }
}

Activity

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

      No branches or pull requests

      Issue actions