You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2021/01/01 13:47:00 UTC

[GitHub] [netbeans] KacerCZ opened a new pull request #2630: [NETBEANS-377] Add PSR-4 hints

KacerCZ opened a new pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630


   https://issues.apache.org/jira/browse/NETBEANS-377
   
   - Added hints for [PSR-4 Autoloader](https://www.php-fig.org/psr/psr-4/) standard.
   - Checks that class/interface/trait declaration matches file name.
   - Checks that namespace matches directory structure.
   - Highlights only non-matching part of namespace.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] tmysik commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
tmysik commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553493391



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {

Review comment:
       Thanks!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] tmysik commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
tmysik commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553184447



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties
##########
@@ -31,6 +31,7 @@ ImmutableVariablesCustomizer.jLabel1.text=Number of Allowed Assignments per Vari
 csl-hints/text/x-php5/hints/braces=Braces
 csl-hints/text/x-php5/hints/psr0=PSR-0: Autoloading Standard
 csl-hints/text/x-php5/hints/psr1=PSR-1: Basic Coding Standard
+csl-hints/text/x-php5/hints/psr4=PSR-4: Autoloader

Review comment:
       Should not it be "Autoloading Standard", similarly to PSR-0?
   

##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {

Review comment:
       Perhaps calling `CancelSupport.getDefault().isCancelled()` in this loop could be useful?
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] junichi11 edited a comment on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
junichi11 edited a comment on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-754417583


   We should leave a comment for unsupported cases somewhere.
   
   Add a test for the following case?
   ``` php
   // file: Vendor/App/ClassName.php
   namespace Vendor\App;
   
   class classname {
   
   }
   
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] junichi11 merged pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
junichi11 merged pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-754773963


   I added tests where casing is different.
   
   Also added note about case-sensitivity to hint descriptions:
   - "Namespace MUST match (case-sensitive) file directory." 
   - "Type name MUST match (case-sensitive) file name."


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] junichi11 commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
junichi11 commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r551732726



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,258 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {
+                        Identifier segment = segmentsIterator.previous();
+                        if (!segment.getName().equals(currentDir.getName())) {
+                            createHint(namespaceName, Bundle.PSR4WrongNamespaceNameHintText(), endOffset);
+                            break;
+                        }
+                        // Move end offset by length of directory name plus separator.
+                        endOffset -= currentDir.getName().length() + 1;
+                        currentDir = currentDir.getParentFile();
+                    }
+                }
+                super.visit(node);
+            }
+
+        }
+
+        @Override
+        public String getId() {
+            return HINT_ID;
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4NamespaceHintDesc=Namespace MUST match file directory.")
+        public String getDescription() {
+            return Bundle.PSR4NamespaceHintDesc();
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4NamespaceHintDisp=Namespace Declaration")
+        public String getDisplayName() {
+            return Bundle.PSR4NamespaceHintDisp();
+        }
+    }
+
+    public static class TypeDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Type"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new TypeVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class TypeVisitor extends CheckVisitor {
+
+            private static final String PHP_FILE_EXTENSION = ".php"; //NOI18N
+
+            public TypeVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            public void visit(ClassDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @Override
+            public void visit(InterfaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @Override
+            public void visit(TraitDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @NbBundle.Messages("PSR4WrongTypeNameHintText=Type declaration name doesn't correspond to current file name.")
+            private void processTypeDeclaration(TypeDeclaration node) {
+                String currentTypeName = CodeUtils.extractTypeName(node);
+                String filename = getFile().getName();
+                if (!filename.equals(currentTypeName + PHP_FILE_EXTENSION)) {
+                    Identifier name = node.getName();
+                    createHint(name, Bundle.PSR4WrongTypeNameHintText());
+                }
+            }
+
+        }
+
+        @Override
+        public String getId() {
+            return HINT_ID;
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4TypeHintDesc=Type name MUST match file name.")
+        public String getDescription() {
+            return Bundle.PSR4TypeHintDesc();
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4TypeHintDisp=Type Declaration")
+        public String getDisplayName() {
+            return Bundle.PSR4TypeHintDisp();
+        }
+    }
+
+    private abstract static class CheckVisitor extends DefaultVisitor {
+
+        private final PSR4Hint psr4hint;
+        private final FileObject fileObject;
+        private final BaseDocument baseDocument;
+        private final File file;
+        private final List<Hint> hints;
+
+        public CheckVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+            this.psr4hint = psr4hint;
+            this.fileObject = fileObject;
+            this.baseDocument = baseDocument;
+            this.file = FileUtil.toFile(fileObject);
+            this.hints = new ArrayList<>();
+        }
+
+        public List<Hint> getHints() {
+            return hints;

Review comment:
       Use `Collections.unmodifiableList(hints)`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] tmysik commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
tmysik commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553240587



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {

Review comment:
       In general, this check should be present in every loop to avoid unneeded work to be done. The check is very cheap and IO operations are slow, in general (yes, I heard about SSD :) so I think it should be there.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-756715567


   @junichi11 @tmysik Thanks for review.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-843242307


   @ahajzik-crido No, mapping from composer.json is separate task - https://issues.apache.org/jira/browse/NETBEANS-5341 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] tmysik commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
tmysik commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553238965



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties
##########
@@ -31,6 +31,7 @@ ImmutableVariablesCustomizer.jLabel1.text=Number of Allowed Assignments per Vari
 csl-hints/text/x-php5/hints/braces=Braces
 csl-hints/text/x-php5/hints/psr0=PSR-0: Autoloading Standard
 csl-hints/text/x-php5/hints/psr1=PSR-1: Basic Coding Standard
+csl-hints/text/x-php5/hints/psr4=PSR-4: Autoloader

Review comment:
       OK.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] ahajzik-crido commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
ahajzik-crido commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-843118428


   Does this feature support psr-4 declaration from composer.json?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] junichi11 commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
junichi11 commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-754417583


   We should leave a comment for unsupported cases somewhere.
   
   Add test for the following case?
   ``` php
   // file: Vendor/App/ClassName.php
   namespace Vendor\App;
   
   class classname {
   
   }
   
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] junichi11 commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
junichi11 commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-756392359


   Will re-run jobs.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#issuecomment-756715567


   @junichi11 @tmysik Thanks for review.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553200726



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {

Review comment:
       One iteration of the loop will be done for one part of namespace.
   I expect no more than 10 iterations even if someone uses very deep structure.
   
   Can traversing directories can take long time and getting its name take so much time it will be noticeable?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r552071971



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,258 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {
+                        Identifier segment = segmentsIterator.previous();
+                        if (!segment.getName().equals(currentDir.getName())) {
+                            createHint(namespaceName, Bundle.PSR4WrongNamespaceNameHintText(), endOffset);
+                            break;
+                        }
+                        // Move end offset by length of directory name plus separator.
+                        endOffset -= currentDir.getName().length() + 1;
+                        currentDir = currentDir.getParentFile();
+                    }
+                }
+                super.visit(node);
+            }
+
+        }
+
+        @Override
+        public String getId() {
+            return HINT_ID;
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4NamespaceHintDesc=Namespace MUST match file directory.")
+        public String getDescription() {
+            return Bundle.PSR4NamespaceHintDesc();
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4NamespaceHintDisp=Namespace Declaration")
+        public String getDisplayName() {
+            return Bundle.PSR4NamespaceHintDisp();
+        }
+    }
+
+    public static class TypeDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Type"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new TypeVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class TypeVisitor extends CheckVisitor {
+
+            private static final String PHP_FILE_EXTENSION = ".php"; //NOI18N
+
+            public TypeVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            public void visit(ClassDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @Override
+            public void visit(InterfaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @Override
+            public void visit(TraitDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                processTypeDeclaration(node);
+                super.visit(node);
+            }
+
+            @NbBundle.Messages("PSR4WrongTypeNameHintText=Type declaration name doesn't correspond to current file name.")
+            private void processTypeDeclaration(TypeDeclaration node) {
+                String currentTypeName = CodeUtils.extractTypeName(node);
+                String filename = getFile().getName();
+                if (!filename.equals(currentTypeName + PHP_FILE_EXTENSION)) {
+                    Identifier name = node.getName();
+                    createHint(name, Bundle.PSR4WrongTypeNameHintText());
+                }
+            }
+
+        }
+
+        @Override
+        public String getId() {
+            return HINT_ID;
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4TypeHintDesc=Type name MUST match file name.")
+        public String getDescription() {
+            return Bundle.PSR4TypeHintDesc();
+        }
+
+        @Override
+        @NbBundle.Messages("PSR4TypeHintDisp=Type Declaration")
+        public String getDisplayName() {
+            return Bundle.PSR4TypeHintDisp();
+        }
+    }
+
+    private abstract static class CheckVisitor extends DefaultVisitor {
+
+        private final PSR4Hint psr4hint;
+        private final FileObject fileObject;
+        private final BaseDocument baseDocument;
+        private final File file;
+        private final List<Hint> hints;
+
+        public CheckVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+            this.psr4hint = psr4hint;
+            this.fileObject = fileObject;
+            this.baseDocument = baseDocument;
+            this.file = FileUtil.toFile(fileObject);
+            this.hints = new ArrayList<>();
+        }
+
+        public List<Hint> getHints() {
+            return hints;

Review comment:
       Changed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553488574



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.verification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.parser.PHPParseResult;
+import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
+import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
+import org.netbeans.modules.php.editor.parser.astnodes.InterfaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.NbBundle;
+
+/**
+ * Check class name corresponds to PSR-4 rules.
+ *
+ * @see https://www.php-fig.org/psr/psr-4/
+ */
+public abstract class PSR4Hint extends HintRule {
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> result) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() != null) {
+            FileObject fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+            if (fileObject != null) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                CheckVisitor checkVisitor = createVisitor(fileObject, context.doc);
+                phpParseResult.getProgram().accept(checkVisitor);
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                result.addAll(checkVisitor.getHints());
+            }
+        }
+    }
+
+    abstract CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument);
+
+    public static class NamespaceDeclarationHint extends PSR4Hint {
+
+        private static final String HINT_ID = "PSR4.Hint.Namespace"; //NOI18N
+
+        @Override
+        CheckVisitor createVisitor(FileObject fileObject, BaseDocument baseDocument) {
+            return new NamespaceVisitor(this, fileObject, baseDocument);
+        }
+
+        private static final class NamespaceVisitor extends CheckVisitor {
+
+            public NamespaceVisitor(PSR4Hint psr4hint, FileObject fileObject, BaseDocument baseDocument) {
+                super(psr4hint, fileObject, baseDocument);
+            }
+
+            @Override
+            @NbBundle.Messages("PSR4WrongNamespaceNameHintText=Namespace declaration name doesn't correspond to current directory structure.")
+            public void visit(NamespaceDeclaration node) {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;
+                }
+                NamespaceName namespaceName = node.getName();
+                if (namespaceName != null) {
+                    int endOffset = namespaceName.getEndOffset();
+                    File currentDir = getFile().getParentFile();
+                    ListIterator<Identifier> segmentsIterator = namespaceName.getSegments().listIterator(namespaceName.getSegments().size());
+                    while (segmentsIterator.hasPrevious()) {

Review comment:
       Right, I added it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] KacerCZ commented on a change in pull request #2630: [NETBEANS-377] Add PSR-4 hints

Posted by GitBox <gi...@apache.org>.
KacerCZ commented on a change in pull request #2630:
URL: https://github.com/apache/netbeans/pull/2630#discussion_r553190847



##########
File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties
##########
@@ -31,6 +31,7 @@ ImmutableVariablesCustomizer.jLabel1.text=Number of Allowed Assignments per Vari
 csl-hints/text/x-php5/hints/braces=Braces
 csl-hints/text/x-php5/hints/psr0=PSR-0: Autoloading Standard
 csl-hints/text/x-php5/hints/psr1=PSR-1: Basic Coding Standard
+csl-hints/text/x-php5/hints/psr4=PSR-4: Autoloader

Review comment:
       "Autoloader" is official name of PSR-4 - https://www.php-fig.org/psr/psr-4/




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists