Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1 backed enumerations implementation #4066

Closed
1 of 2 tasks
adamturcsan opened this issue May 3, 2022 · 0 comments · Fixed by #4169
Closed
1 of 2 tasks

PHP 8.1 backed enumerations implementation #4066

adamturcsan opened this issue May 3, 2022 · 0 comments · Fixed by #4169
Assignees
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Milestone

Comments

@adamturcsan
Copy link

adamturcsan commented May 3, 2022

Description

NB 14 has support for the new features of php 8.1, but the enumeration support has a deficiency.

Enumerations cannot have properties (which is important as those are objects in the language implementation) although there is a use case, where an enumeration can have one, and only one readonly public property, called value.

This property is populated when backed enumerations are used.

Use case/motivation

Right now NB shows an error when a property on an enumeration is accessed. The error is context dependant, an example code is below:

<?php

use DateTimeImmutable;
use DateTimeZone;

enum StopDay: string
{
    case WEEKDAYS = 'weekdays';
    case WEEKEND = 'weekend';
    case MONDAY = 'Mon';
    case TUESDAY = 'Tue';
    case WEDNESDAY = 'Wed';
    case THURSDAY = 'Thu';
    case FRIDAY = 'Fri';
    case SATURDAY = 'Sat';
    case SUNDAY = 'Sun';

    public function isToday(DateTimeImmutable $date = new DateTimeImmutable('now', new DateTimeZone('UTC'))): bool
    {
        return match($this) {
            self::MONDAY => $date->format('D') === self::MONDAY->value,
            self::TUESDAY => $date->format('D') === self::TUESDAY->value,
            self::WEDNESDAY => $date->format('D') === self::WEDNESDAY->value,
            self::THURSDAY => $date->format('D') === self::THURSDAY->value,
            self::FRIDAY => $date->format('D') === self::FRIDAY->value,
            self::SATURDAY => $date->format('D') === self::SATURDAY->value,
            self::SUNDAY => $date->format('D') === self::SUNDAY->value,
            self::WEEKDAYS => $date->format('D') !== self::SATURDAY->value && $date->format('D') !== self::SUNDAY->value,
            self::WEEKEND => $date->format('D') === self::SATURDAY->value || $date->format('D') === self::SUNDAY->value
        };
    }
}

In the example below, after every ->value there's a Syntax error, because the checker expects a method call. So in this case line 21. shows an error:

Syntax error:
    unexpected: ,
    expected: (

Related issues

No response

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@adamturcsan adamturcsan added kind:feature A feature request needs:triage Requires attention from one of the committers labels May 3, 2022
@junichi11 junichi11 self-assigned this May 3, 2022
@junichi11 junichi11 added this to the NB15 milestone May 3, 2022
@junichi11 junichi11 added kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor) and removed kind:feature A feature request needs:triage Requires attention from one of the committers labels May 3, 2022
@junichi11 junichi11 changed the title PHP 8.2 backed enumerations implementation PHP 8.1 backed enumerations implementation May 4, 2022
junichi11 added a commit to junichi11/netbeans that referenced this issue May 27, 2022
…he#4066

- Fix the parser
- Add the `value` property to backed enums as CC items
- Add the members of `UnitEnum` and `BackedEnum` to enums as CC items
- Add unit tests

```php
Enums::CASE1->value;
Enums::from("name");
Enums::cases();
```
junichi11 added a commit to junichi11/netbeans that referenced this issue Jun 7, 2022
…he#4066

- Fix the parser
- Add the `value` property to backed enums as CC items
- Add the members of `UnitEnum` and `BackedEnum` to enums as CC items
- Add unit tests

```php
Enums::CASE1->value;
Enums::from("name");
Enums::cases();
```
tmysik added a commit that referenced this issue Jun 7, 2022
…cial-property-of-backed-enums

PHP 8.1: Support for the special property(value) of BackedEnum #4066
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants