microsoft / microsoft/tolerant-php-parser

Single word namespaces which are reserved words produce an error incorrectly

Open
#362 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
894
Forks
85
Avg merge
2d 13h
Merged PRs (30d)
3

Description

PR 331 doesn't support PHP8 valid namespaces which are a single non-string token. For instance, while the first code block will work without issue on PHP8 (as T_NAME_QUALIFIED will be split to T_STRING T_NS_SEPARATOR T_STRING, resulting in Abstract being a T_STRING), the second will fail as token_get_all returns T_ABSTRACT and so the parser thinks that the namespace declaration is incomplete.

Works:

namespace Abstract\Objects;

class Foo{}

Valid PHP but doesn't work:

namespace Abstract;

class Foo{}

(DiagnosticsProvider::getDiagnostics($astNode); returns):

array(2) {
  [0] =>
  class Microsoft\PhpParser\Diagnostic#22 (4) {
    public $kind =>
    int(0)
    public $message =>
    string(13) "';' expected."
    public $start =>
    int(16)
    public $length =>
    int(0)
  }
  [1] =>
  class Microsoft\PhpParser\Diagnostic#23 (4) {
    public $kind =>
    int(0)
    public $message =>
    string(21) "Unexpected 'abstract'"
    public $start =>
    int(17)
    public $length =>
    int(8)
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the namespace parsing path and the DiagnosticsProvider::getDiagnostics($astNode) entry point, comparing the working namespace Abstract\Objects example with the failing namespace Abstract example. Review PR 331 and PHP's token_get_all behavior for single reserved-word namespaces; done means the valid PHP example produces no incorrect diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.