prettier / prettier/plugin-php

Unable to optional chain from parentheses

Open
#2,447 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.9k
Forks
139
PR merge metrics
No merged PRs in 30d

Description

@prettier/plugin-php v0.24.0
Playground link

Input:

<?php

($var)?->func();

Output:

Parse Error : syntax error, unexpected '?->' (T_NULLSAFE_OBJECT_OPERATOR), expecting ';' on line 3

  1 | <?php
  2 |
> 3 | ($var)?->func();
    |      ^

Prettier throws a syntax error when optional chaining from an expression wrapped in parentheses. Practical example that I was working on when I encountered this issue:

private function isSyncJob(object $job): bool
{
    return ($job->job ?? null)?->getConnectionName() === "sync";
}

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 linked Playground reproduction and compare parsing of ($var)?->func() with the unparenthesized form and the practical ($job->job ?? null)?->getConnectionName() example. Trace the parser or formatter entry point responsible for this syntax and add coverage for the parenthesized cases; done means they format without a syntax error and preserve the nullsafe call.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.