Skip to content

Commit

Permalink
PHP 8.4 Support: Property hooks (Part 1)
Browse files Browse the repository at this point in the history
- apache#8035
- https://wiki.php.net/rfc#php_84
- https://wiki.php.net/rfc/property-hooks

- Fix the lexer and parser(grammar)
- Add `PropertyHookDeclaration` as an `ASTNode`
- Fix `PHP5ErrorHandlerImpl` (handle missing tokens)
- Fix/Add unit tests for the lexer and parser
- Remove `ArrayDimensionSyntaxSuggestionHint` because this no longer works.

Example:
```php
class PropertyHooksClass {
    public int $backed = 100 {
        get {
            return $this->backed;
        }
        set {
            $this->backed = $value;
        }
    }
    public $doubleArrow { // virtual
        get => $this->test();
        set => $this->test() . $value;
    }
    public $attributes {
        #[Attr1] get {}
        #[Attr2] set ($value) {
            $this->attributes = $value;
        }
    }
    public $reference {
        &get => $this->reference;
    }
    public $final {
        final get => $this->final;
    }

    // constructor property promotion
    public function __construct(
        public $prop {
            get {
                return $this->prop;
            }
            set {
                $this->prop = $value;
            }
        }
    ) {}
}

class Child extends PropertyHooksClass {
    public $prop {
        get => parent::$prop::get();
        set {
            parent::$prop::set($value);
        }
    }
}

interface PropertyHooksInterface {
    public string $prop1 {
        get;
    }
    final public int $prop2 {
        set;
    }
    public $prop3 {
        get;
        set;
    }
    public $ref { &get; }
}
```

Note: Curly braces array access (`{}` e.g. `$array{1}`, `$array{'key'}`) can use no longer.
Because a conflict occurs in the following case:

```php
"string"{1};
public string $prop = "string" {
    get => $this->prop;
    set {}
}
```
  • Loading branch information
junichi11 committed Feb 9, 2025
1 parent 51fa132 commit 5e2fad5
Show file tree
Hide file tree
Showing 211 changed files with 78,435 additions and 65,686 deletions.
2,159 changes: 1,092 additions & 1,067 deletions php/php.editor/src/org/netbeans/modules/php/editor/lexer/PHP5ColoringLexer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public enum PHPTokenId implements TokenId {
PHP_PARENT(null, "keyword"), //NOI18N
PHP__CLASS__(null, "constant"), //NOI18N
PHP__TRAIT__(null, "constant"), //NOI18N
PHP__PROPERTY__(null, "constant"), //NOI18N PHP 8.4
PHP__METHOD__(null, "constant"), //NOI18N
PHP_TRUE(null, "keyword"), //NOI18N
PHP_FALSE(null, "keyword"), //NOI18N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void addElement(ModelElementImpl element) {
assert element instanceof TypeScope || element instanceof VariableName
|| element instanceof MethodScope || element instanceof FieldElement
|| element instanceof CaseElement // allowed by parser although class can't have cases
|| element instanceof ClassConstantElement : element.getPhpElementKind();
|| element instanceof ClassConstantElement : element.getPhpElementKind() + " " + this.toString();
if (element instanceof TypeScope) {
Scope inScope = getInScope();
if (inScope instanceof ScopeImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.netbeans.modules.php.editor.model.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -55,7 +54,7 @@
*/
class FieldElementImpl extends ScopeImpl implements FieldElement {

String defaultType;
private String defaultType;
private String defaultFQType;
private String className;
private final boolean isAnnotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public void visit(ConstantDeclaration node) {
@Override
public void visit(SingleFieldDeclaration node) {
scan(node.getValue());
scan(node.getPropertyHooks());
}

@Override
Expand Down
6,991 changes: 3,694 additions & 3,297 deletions php/php.editor/src/org/netbeans/modules/php/editor/parser/ASTPHP5Parser.java

Large diffs are not rendered by default.

2,149 changes: 1,088 additions & 1,061 deletions php/php.editor/src/org/netbeans/modules/php/editor/parser/ASTPHP5Scanner.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

//----------------------------------------------------
// The following code was generated by CUP v0.11a beta 20060608
// Sun Jan 12 13:42:53 JST 2025
// Wed Jan 29 19:33:57 JST 2025
//----------------------------------------------------

package org.netbeans.modules.php.editor.parser;
Expand Down Expand Up @@ -198,6 +198,7 @@ public interface ASTPHP5Symbols {
public static final int T_INSTANCEOF = 23;
public static final int T_DIV_EQUAL = 94;
public static final int T_NUM_STRING = 9;
public static final int T_PROPERTY_C = 175;
public static final int T_HALT_COMPILER = 50;
public static final int T_ENUM = 168;
public static final int T_GOTO = 33;
Expand Down
5,464 changes: 2,732 additions & 2,732 deletions php/php.editor/src/org/netbeans/modules/php/editor/parser/EncodedActionTable1.java

Large diffs are not rendered by default.

Loading

0 comments on commit 5e2fad5

Please sign in to comment.