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.4 Support: Property hooks #8227

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion php/php.editor/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ javac.release=17
javac.compilerargs=-Xlint -Xlint:-serial
nbjavac.ignore.missing.enclosing=**/CUP$ASTPHP5Parser$actions.class
nbm.needs.restart=true
spec.version.base=2.45.0
spec.version.base=2.46.0
release.external/predefined_vars-1.0.zip=docs/predefined_vars.zip
sigtest.gen.fail.on.error=false

Expand Down
9 changes: 9 additions & 0 deletions php/php.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
<specification-version>1.0</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.libs.json_simple</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>0.40</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.csl.api</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,24 @@ public final class IconsUtils {

private static final String PNG_EXTENSION = ".png"; //NOI18N
private static final String GIF_EXTENSION = ".gif"; //NOI18N
private static final String SVG_EXTENSION = ".svg"; //NOI18N
private static final String ICON_BASE = "org/netbeans/modules/php/editor/resources/"; //NOI18N
private static final String EMPTY_FILE_ICON_BASE = "org/netbeans/modules/csl/source/resources/icons/emptyfile-icon"; //NOI18N

private IconsUtils() {
}

public static ImageIcon getElementIcon(PhpElementKind elementKind, Collection<Modifier> modifiers) {
ImageIcon imageIcon;
switch (elementKind) {
case CLASS:
imageIcon = loadClassIcon();
break;
case IFACE:
imageIcon = loadInterfaceIcon();
break;
case TRAIT:
imageIcon = loadTraitIcon();
break;
case ENUM:
imageIcon = loadEnumIcon();
break;
case CONSTANT:
imageIcon = loadConstantIcon();
break;
case FUNCTION:
imageIcon = loadFunctionIcon();
break;
default:
imageIcon = ImageUtilities.loadImageIcon(EMPTY_FILE_ICON_BASE + PNG_EXTENSION, false);
break;
}
return imageIcon;
return switch (elementKind) {
case CLASS -> loadClassIcon();
case IFACE -> loadInterfaceIcon();
case TRAIT -> loadTraitIcon();
case ENUM -> loadEnumIcon();
case CONSTANT -> loadConstantIcon();
case FUNCTION -> loadFunctionIcon();
case PROPERTY_HOOK -> loadPropertyHookIcon();
default -> ImageUtilities.loadImageIcon(EMPTY_FILE_ICON_BASE + PNG_EXTENSION, false);
};
}

public static ImageIcon getElementIcon(PhpElementKind elementKind) {
Expand Down Expand Up @@ -94,6 +80,10 @@ public static ImageIcon loadFunctionIcon() {
return ImageUtilities.loadImageIcon(ICON_BASE + "function" + PNG_EXTENSION, false); // NOI18N
}

public static ImageIcon loadPropertyHookIcon() {
return ImageUtilities.loadImageIcon(ICON_BASE + "hook" + SVG_EXTENSION, false); // NOI18N
}

public static ImageIcon loadConstantIcon() {
return ImageUtilities.loadImageIcon(ICON_BASE + "constant" + PNG_EXTENSION, false); // NOI18N
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,31 @@ public enum PhpElementKind {
VARIABLE, CONSTANT, FUNCTION,
NAMESPACE_DECLARATION, USE_STATEMENT, GROUP_USE_STATEMENT, CONSTRUCTOR,
TRAIT, TRAIT_CONFLICT_RESOLUTION, TRAIT_METHOD_ALIAS, EMPTY,
ENUM, ENUM_CASE;
ENUM, ENUM_CASE,
PROPERTY_HOOK,
;

public final ElementKind getElementKind() {
ElementKind result;
switch (this) {
case CLASS:
result = ElementKind.CLASS;
break;
case TYPE_CONSTANT:
result = ElementKind.CONSTANT;
break;
case CONSTANT:
result = ElementKind.CONSTANT;
break;
case FIELD:
result = ElementKind.FIELD;
break;
case FUNCTION:
result = ElementKind.METHOD;
break;
case IFACE:
result = ElementKind.INTERFACE;
break;
case METHOD:
result = ElementKind.METHOD;
break;
case VARIABLE:
result = ElementKind.VARIABLE;
break;
case NAMESPACE_DECLARATION:
result = ElementKind.PACKAGE;
break;
case ENUM_CASE:
result = ElementKind.CONSTANT;
break;
default:
result = ElementKind.OTHER;
}
return result;
return switch (this) {
case CLASS ->
ElementKind.CLASS;
case TYPE_CONSTANT, CONSTANT, ENUM_CASE ->
ElementKind.CONSTANT;
case FIELD ->
ElementKind.FIELD;
case FUNCTION, METHOD ->
ElementKind.METHOD;
case IFACE ->
ElementKind.INTERFACE;
case VARIABLE ->
ElementKind.VARIABLE;
case NAMESPACE_DECLARATION ->
ElementKind.PACKAGE;
case CONSTRUCTOR, EMPTY, ENUM, GROUP_USE_STATEMENT,
INCLUDE, INDEX, PROGRAM, PROPERTY_HOOK,
TRAIT, TRAIT_CONFLICT_RESOLUTION,
TRAIT_METHOD_ALIAS, USE_ALIAS, USE_STATEMENT ->
ElementKind.OTHER;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private PhpModifiers(int... bitmask) {
for (int mode : bitmask) {
this.mod |= mode;
}
if (!Modifier.isPrivate(mod) && !Modifier.isProtected(mod) && !Modifier.isImplicitPublic(mod)) {
mod |= Modifier.PUBLIC;
if (!Modifier.isPrivate(mod) && !Modifier.isProtected(mod) && !Modifier.isPublic(mod)) {
mod |= Modifier.IMPLICIT_PUBLIC;
}
Comment on lines 132 to 137
Copy link
Member Author

Choose a reason for hiding this comment

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

IMPLICIT_PUBLIC is better if mod is 0 because the public modifier doesn't exist.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,56 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.netbeans.modules.php.editor.api.elements;

import java.util.List;
import org.netbeans.modules.php.editor.api.PhpElementKind;

/**
* @author Radek Matous
*/
public interface FieldElement extends TypedInstanceElement, TypeMemberElement {

PhpElementKind KIND = PhpElementKind.FIELD;

String getName(boolean dollared);

boolean isAnnotation();

boolean isUnionType();

boolean isIntersectionType();

String getDeclaredType();

/**
* Check whether this element is a hooked property(field).
*
* @param field
* @return {@code true} it's hooked property, {@code false} otherwise
* @since 2.46.0
*/
public static boolean isHooked(FieldElement field) {
return (field instanceof HookedFieldElement)
&& ((HookedFieldElement) field).isHooked();
}

public interface HookedFieldElement extends FieldElement {

/**
* Check whether this element is a hooked property.
*
* @return {@code true} it's hooked property, {@code false} otherwise
* @since 2.46.0
*/
boolean isHooked();

/**
* Get property hooks.
*
* @return property hooks
* @since 2.46.0
*/
List<? extends PropertyHookElement> getPropertyHooks();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.php.editor.api.elements;

import java.util.List;
import org.netbeans.modules.csl.api.OffsetRange;

/**
* Property hook element.
*
* @see @since 2.46.0
*/
public interface PropertyHookElement extends PhpElement {

/**
* Check whether a property hook is reference.
*
* e.g. {@code &get{}}
*
* @return {@code true} if it's reference, {@code false} otherwise
* @since 2.46.0
*/
boolean isReference();

/**
* Check whether a property hook has a body(`{}` part).
*
* Interface property and abstract properties don't have a body. (e.g.
* {@code get; set;})
*
* @return {@code true} if it has a body, {@code false} otherwise
* @since 2.46.0
*/
boolean hasBody();

/**
* Check whether a property hook has attributes. (e.g.
* {@code #[Attr] get{}})
*
* @return {@code true} if a property hook has attributes, {@code false}
* otherwise
* @since 2.46.0
*/
boolean isAttributed();

/**
* Get parameters of a property hook.
*
* e.g. {@code set(#[Attr] string $value){}}
*
* @return parameters
* @since 2.46.0
*/
List<? extends ParameterElement> getParameters();

/**
* Get the offset range.
*
* @return the offset range
* @since 2.46.0
*/
OffsetRange getOffsetRange();
// TODO add List<? extends AttributeElemnt> getAttributes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.php.editor.lexer.LexUtilities;
import org.netbeans.modules.php.editor.lexer.PHPTokenId;
import org.netbeans.modules.php.editor.model.FieldElement;
import org.netbeans.modules.php.editor.model.FileScope;
import org.netbeans.modules.php.editor.model.FunctionScope;
import org.netbeans.modules.php.editor.model.GroupUseScope;
import org.netbeans.modules.php.editor.model.MethodScope;
import org.netbeans.modules.php.editor.model.Model;
import org.netbeans.modules.php.editor.model.ModelElement;
import org.netbeans.modules.php.editor.model.PropertyHookScope;
import org.netbeans.modules.php.editor.model.Scope;
import org.netbeans.modules.php.editor.model.TypeScope;
import org.netbeans.modules.php.editor.model.UseScope;
Expand Down Expand Up @@ -108,6 +110,16 @@ public final class FoldingScanner {
Bundle.FT_Functions(),
FoldTemplate.DEFAULT_BLOCK);

@NbBundle.Messages("FT_HookedFields=Fields(Properties)")
public static final FoldType TYPE_HOOKED_FIELD = FoldType.MEMBER.derive("field", // NOI18N
Bundle.FT_HookedFields(),
FoldTemplate.DEFAULT_BLOCK);

@NbBundle.Messages("FT_PropertyHooks=Property hooks")
public static final FoldType TYPE_PROPERTY_HOOK = FoldType.MEMBER.derive("property hook", // NOI18N
Bundle.FT_PropertyHooks(),
FoldTemplate.DEFAULT_BLOCK);

@NbBundle.Messages("FT_Arrays=Arrays")
public static final FoldType TYPE_ARRAY = FoldType.NESTED.derive(
"array",
Expand Down Expand Up @@ -273,21 +285,25 @@ private void processPHPTags(Map<String, List<OffsetRange>> folds, Document docum

private void processScopes(Map<String, List<OffsetRange>> folds, List<Scope> scopes) {
processUseScopes(folds, scopes);
processTypeAndFunctionScopes(folds, scopes);
processTypeAndMemberScopes(folds, scopes);
}

private void processTypeAndFunctionScopes(Map<String, List<OffsetRange>> folds, List<Scope> scopes) {
private void processTypeAndMemberScopes(Map<String, List<OffsetRange>> folds, List<Scope> scopes) {
for (Scope scope : scopes) {
OffsetRange offsetRange = scope.getBlockRange();
if (offsetRange == null || offsetRange.getLength() <= 1) {
continue;
}
if (scope instanceof TypeScope) {
getRanges(folds, TYPE_CLASS).add(offsetRange);
} else {
if (scope instanceof FunctionScope || scope instanceof MethodScope) {
getRanges(folds, TYPE_FUNCTION).add(offsetRange);
} else if (scope instanceof FunctionScope || scope instanceof MethodScope) {
getRanges(folds, TYPE_FUNCTION).add(offsetRange);
} else if (scope instanceof FieldElement.HookedFieldElement field) {
if (field.isHooked()) {
getRanges(folds, TYPE_HOOKED_FIELD).add(offsetRange);
}
} else if (scope instanceof PropertyHookScope) {
getRanges(folds, TYPE_PROPERTY_HOOK).add(offsetRange);
}
}
}
Expand Down
Loading
Loading