Skip to content

PHP 8.4 Support: new MyClass()->method() without parentheses#8117

Merged
junichi11 merged 7 commits intoapache:masterfrom
junichi11:php84-new-without-parentheses
Jan 8, 2025
Merged

PHP 8.4 Support: new MyClass()->method() without parentheses#8117
junichi11 merged 7 commits intoapache:masterfrom
junichi11:php84-new-without-parentheses

Conversation

@junichi11
Copy link
Copy Markdown
Member

PHP 8.4 Support: Add PHP 8.4 to the PhpVersion

  • Add PHP 8.4
  • Fix the available periods

nb-php84-add-php-version

new MyClass()->method() without parentheses (Part 1)

  • Fix the gramar file(ASTPHP5Parser.cup)
  • Add the PHP84UnhandledError
  • Add unit tests for the parser

Example:

new Example()->method();
new $class()?->method();
new Example()::staticMethod1()::CONSTANT;
new (trim(' Example '))()::staticMethod1()->field;
new Example()::{'CONSTANT'};

new Example()();
new $class()();

new Example()['key'];
new $class()['key'];
new (trim(' Example '))()['key'];

// anonymous classes
echo new class {
    const CONSTANT = 'constant';
}::CONSTANT;

new class {
    public function method() {}
}->method();

new class () implements ArrayAccess {
}['key'];

PHP 8.4

nb-php84-new-without-parentheses-syntax

PHP 8.3

nb-php84-new-without-parentheses-syntax-php83

new MyClass()->method() without parentheses (Part 2)

  • Fix the formatter
  • Add Other to spaces within parentheses option e.g. ($a + $b);, new (trim(' Example '))()->field;
  • Add/Fix unit tests

nb-php84-new-without-parentheses-formatting-option

new MyClass()->method() without parentheses (Part 3)

  • Fix "Go to Declaration" & "Mark Occurrences"
  • Add unit tests

new MyClass()->method() without parentheses (Part 4)

  • Fix the UnusedVariableHint
  • Add a unit test

new MyClass()->method() without parentheses (Part 5)

  • Fix Code Completion feature
  • Add unit tests

Note: The following case is not supported yet.
The statement is broken. (it has a syntax error.)
So, the anonymous class is not parsed correctly. i.e. We can't get members.
We have to sanitize an error part.

echo new class() {
    public const string CONSTANT = "constant";
}::^

new MyClass()->method() without parentheses (Part 6)

  • Add a unit test for the navigator

- apache#8035
- https://wiki.php.net/rfc#php_84
- https://wiki.php.net/rfc/new_without_parentheses
- Fix the gramar file(`ASTPHP5Parser.cup`)
- Add the `PHP84UnhandledError`
- Add unit tests for the parser

Example:
```php
new Example()->method();
new $class()?->method();
new Example()::staticMethod1()::CONSTANT;
new (trim(' Example '))()::staticMethod1()->field;
new Example()::{'CONSTANT'};

new Example()();
new $class()();

new Example()['key'];
new $class()['key'];
new (trim(' Example '))()['key'];

// anonymous classes
echo new class {
    const CONSTANT = 'constant';
}::CONSTANT;

new class {
    public function method() {}
}->method();

new class () implements ArrayAccess {
}['key'];
```
- apache#8035
- https://wiki.php.net/rfc#php_84
- https://wiki.php.net/rfc/new_without_parentheses
- Fix the formatter
- Add `Other` to spaces within parentheses option e.g. `($a + $b);`, `new (trim(' Example '))()->field;`
- Add/Fix unit tests
- apache#8035
- https://wiki.php.net/rfc#php_84
- https://wiki.php.net/rfc/new_without_parentheses
- Fix Code Completion feature
- Add unit tests

Note: The following case is not supported yet.
The statement is broken. (it has a syntax error.)
So, the anonymous class is not parsed correctly. i.e. We can't get members.
We have to sanitize an error part.
```php
echo new class() {
    public const string CONSTANT = "constant";
}::^
```
@junichi11 junichi11 added the PHP [ci] enable extra PHP tests (php/php.editor) label Jan 6, 2025
@junichi11 junichi11 added this to the NB25 milestone Jan 6, 2025

throw $instance->createException();
throw ( $instance->createException());
throw ($instance->createException());
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed an existing bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

// for code completion
// e.g. new class() {}::
// PHP Parse error: syntax error, unexpected token ";", expecting identifier or variable or "{" or "$"
parser.syntax_error();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an error because this case is a syntax error.

Comment on lines +476 to +483
// TODO: add tests for typing anonymous class without ";"
// e.g.
// echo new class() {
// public const string CONSTANT = "constant";
// }::^
// in the above case, the statement is broken (it has a syntax error)
// so, the anonymous class is not parsed correctly i.e. we can't get members
// we have to sanitize an error part
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't fix this because it's so hard/difficult to fix it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no problem, I think.

@junichi11 junichi11 requested a review from tmysik January 6, 2025 15:08
@junichi11
Copy link
Copy Markdown
Member Author

@tmysik Most of the files that were added are unit tests. Sorry for the huge change.

@junichi11 junichi11 mentioned this pull request Jan 6, 2025
4 tasks
Copy link
Copy Markdown
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done, thanks a lot. Great to have all the unit tests.

@junichi11
Copy link
Copy Markdown
Member Author

@tmysik Thank you for your time! Merging.

@junichi11 junichi11 merged commit 9bf3674 into apache:master Jan 8, 2025
@junichi11 junichi11 deleted the php84-new-without-parentheses branch January 8, 2025 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PHP [ci] enable extra PHP tests (php/php.editor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants