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: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
