This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 7efb4f8 [NETBEANS-377] Add PSR-4 hints new e481dad Merge pull request #2630 from KacerCZ/netbeans-377-psr4-hints 7efb4f8 is described below commit 7efb4f842d36f512f9d9cb391d23f815daf2ed3e Author: Tomas Prochazka <ka...@razdva.cz> AuthorDate: Fri Jan 1 14:45:55 2021 +0100 [NETBEANS-377] Add PSR-4 hints 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. --- .../modules/php/editor/resources/layer.xml | 5 + .../php/editor/verification/Bundle.properties | 1 + .../modules/php/editor/verification/PSR4Hint.java | 262 +++++++++++++++++++++ .../Vendor/App/ClassName.php | 7 + .../App/ClassName.php.testClassNameDifferent.hints | 4 + .../Vendor/App/ClassName.php | 7 + .../ClassName.php.testClassNameDifferentCase.hints | 4 + .../PSR4/testClassNameOk/Vendor/App/ClassName.php | 7 + .../Vendor/App/ClassName.php.testClassNameOk.hints | 0 .../Vendor/App/InterfaceName.php | 7 + ...erfaceName.php.testInterfaceNameDifferent.hints | 4 + .../Vendor/App/InterfaceName.php | 7 + ...ceName.php.testInterfaceNameDifferentCase.hints | 4 + .../Vendor/App/InterfaceName.php | 7 + .../InterfaceName.php.testInterfaceNameOk.hints | 0 .../Vendor/App/ClassName.php | 7 + .../App/ClassName.php.testNamespaceDifferent.hints | 4 + .../Vendor/App/ClassName.php | 7 + .../ClassName.php.testNamespaceDifferentCase.hints | 4 + .../PSR4/testNamespaceOk/Vendor/App/ClassName.php | 7 + .../Vendor/App/ClassName.php.testNamespaceOk.hints | 0 .../PSR4/testSubNamespaceOk/App/ClassName.php | 7 + .../App/ClassName.php.testSubNamespaceOk.hints | 4 + .../Vendor/App/TraitName.php | 7 + .../App/TraitName.php.testTraitNameDifferent.hints | 4 + .../Vendor/App/TraitName.php | 7 + .../TraitName.php.testTraitNameDifferentCase.hints | 4 + .../PSR4/testTraitNameOk/Vendor/App/TraitName.php | 7 + .../Vendor/App/TraitName.php.testTraitNameOk.hints | 0 .../php/editor/verification/PSR4HintTest.java | 108 +++++++++ 30 files changed, 503 insertions(+) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/resources/layer.xml b/php/php.editor/src/org/netbeans/modules/php/editor/resources/layer.xml index c6dd17d..f20f420 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/resources/layer.xml +++ b/php/php.editor/src/org/netbeans/modules/php/editor/resources/layer.xml @@ -362,6 +362,11 @@ <file name="org-netbeans-modules-php-editor-verification-PSR1Hint$PropertyNameHint.instance"/> <file name="org-netbeans-modules-php-editor-verification-PSR1Hint$SideEffectHint.instance"/> </folder> + <folder name="psr4"> + <attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.php.editor.verification.Bundle"/> + <file name="org-netbeans-modules-php-editor-verification-PSR4Hint$NamespaceDeclarationHint.instance"/> + <file name="org-netbeans-modules-php-editor-verification-PSR4Hint$TypeDeclarationHint.instance"/> + </folder> <folder name="superglobals"> <attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.php.editor.verification.Bundle"/> <file name="org-netbeans-modules-php-editor-verification-SuperglobalsHint$GetSuperglobalHint.instance"/> diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties b/php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties index 55ab59f..9272196 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/verification/Bundle.properties +++ b/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 csl-hints/text/x-php5/hints/superglobals=Superglobals csl-hints/text/x-php5/hints/toomanylines=Too Many Lines NestedHintsCustomizer.jLabel1.text=Number of Allowed Nested Blocks: diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java b/php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java new file mode 100644 index 0000000..7a44166 --- /dev/null +++ b/php/php.editor/src/org/netbeans/modules/php/editor/verification/PSR4Hint.java @@ -0,0 +1,262 @@ +/* + * 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()) { + if (CancelSupport.getDefault().isCancelled()) { + return; + } + 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 (case-sensitive) 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 (case-sensitive) 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 Collections.unmodifiableList(hints); + } + + protected void createHint(ASTNode node, String message) { + int endOffset = node.getEndOffset(); + createHint(node, message, endOffset); + } + + @NbBundle.Messages({ + "# {0} - Text which describes the violation", + "PSR4ViolationHintText=PSR-4 Violation:\n{0}" + }) + protected void createHint(ASTNode node, String message, int endOffset) { + OffsetRange offsetRange = new OffsetRange(node.getStartOffset(), endOffset); + if (psr4hint.showHint(offsetRange, baseDocument)) { + hints.add(new Hint( + psr4hint, + Bundle.PSR4ViolationHintText(message), + fileObject, + offsetRange, + null, + 500)); + } + } + + protected File getFile() { + return file; + } + + } + + @Override + public boolean getDefaultEnabled() { + return true; + } +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php new file mode 100644 index 0000000..fe4fdf4 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +class DifferentClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php.testClassNameDifferent.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php.testClassNameDifferent.hints new file mode 100644 index 0000000..130f832 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferent/Vendor/App/ClassName.php.testClassNameDifferent.hints @@ -0,0 +1,4 @@ +class DifferentClassName { + ------------------ +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php new file mode 100644 index 0000000..4ec3647 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +class classname { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php.testClassNameDifferentCase.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php.testClassNameDifferentCase.hints new file mode 100644 index 0000000..026a82e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameDifferentCase/Vendor/App/ClassName.php.testClassNameDifferentCase.hints @@ -0,0 +1,4 @@ +class classname { + --------- +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameOk/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameOk/Vendor/App/ClassName.php new file mode 100644 index 0000000..90aef6f --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameOk/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +class ClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameOk/Vendor/App/ClassName.php.testClassNameOk.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testClassNameOk/Vendor/App/ClassName.php.testClassNameOk.hints new file mode 100644 index 0000000..e69de29 diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php new file mode 100644 index 0000000..b2f7cc2 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +interface DifferentInterfaceName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php.testInterfaceNameDifferent.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php.testInterfaceNameDifferent.hints new file mode 100644 index 0000000..d72aa8b --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferent/Vendor/App/InterfaceName.php.testInterfaceNameDifferent.hints @@ -0,0 +1,4 @@ +interface DifferentInterfaceName { + ---------------------- +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php new file mode 100644 index 0000000..4a2c7b9 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +interface interfacename { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php.testInterfaceNameDifferentCase.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php.testInterfaceNameDifferentCase.hints new file mode 100644 index 0000000..7e2735b --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameDifferentCase/Vendor/App/InterfaceName.php.testInterfaceNameDifferentCase.hints @@ -0,0 +1,4 @@ +interface interfacename { + ------------- +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameOk/Vendor/App/InterfaceName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameOk/Vendor/App/InterfaceName.php new file mode 100644 index 0000000..8f3021e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameOk/Vendor/App/InterfaceName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +interface InterfaceName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameOk/Vendor/App/InterfaceName.php.testInterfaceNameOk.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testInterfaceNameOk/Vendor/App/InterfaceName.php.testInterfaceNameOk.hints new file mode 100644 index 0000000..e69de29 diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php new file mode 100644 index 0000000..854543c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\Application; + +class ClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php.testNamespaceDifferent.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php.testNamespaceDifferent.hints new file mode 100644 index 0000000..6bc6a04 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferent/Vendor/App/ClassName.php.testNamespaceDifferent.hints @@ -0,0 +1,4 @@ +namespace Vendor\Application; + ------------------ +HINT:PSR-4 Violation: +Namespace declaration name doesn't correspond to current directory structure. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php new file mode 100644 index 0000000..908b787 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\app; + +class ClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php.testNamespaceDifferentCase.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php.testNamespaceDifferentCase.hints new file mode 100644 index 0000000..ac5a77e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceDifferentCase/Vendor/App/ClassName.php.testNamespaceDifferentCase.hints @@ -0,0 +1,4 @@ +namespace Vendor\app; + ---------- +HINT:PSR-4 Violation: +Namespace declaration name doesn't correspond to current directory structure. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceOk/Vendor/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceOk/Vendor/App/ClassName.php new file mode 100644 index 0000000..90aef6f --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceOk/Vendor/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +class ClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceOk/Vendor/App/ClassName.php.testNamespaceOk.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testNamespaceOk/Vendor/App/ClassName.php.testNamespaceOk.hints new file mode 100644 index 0000000..e69de29 diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php new file mode 100644 index 0000000..90aef6f --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +class ClassName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php.testSubNamespaceOk.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php.testSubNamespaceOk.hints new file mode 100644 index 0000000..76d14b5 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testSubNamespaceOk/App/ClassName.php.testSubNamespaceOk.hints @@ -0,0 +1,4 @@ +namespace Vendor\App; + ------ +HINT:PSR-4 Violation: +Namespace declaration name doesn't correspond to current directory structure. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php new file mode 100644 index 0000000..71d7c40 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +trait DifferentTraitName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php.testTraitNameDifferent.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php.testTraitNameDifferent.hints new file mode 100644 index 0000000..3281e98 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferent/Vendor/App/TraitName.php.testTraitNameDifferent.hints @@ -0,0 +1,4 @@ +trait DifferentTraitName { + ------------------ +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php new file mode 100644 index 0000000..2dff3a8 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +trait traitname { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php.testTraitNameDifferentCase.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php.testTraitNameDifferentCase.hints new file mode 100644 index 0000000..ab70fb7 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameDifferentCase/Vendor/App/TraitName.php.testTraitNameDifferentCase.hints @@ -0,0 +1,4 @@ +trait traitname { + --------- +HINT:PSR-4 Violation: +Type declaration name doesn't correspond to current file name. diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameOk/Vendor/App/TraitName.php b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameOk/Vendor/App/TraitName.php new file mode 100644 index 0000000..0de3d9a --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameOk/Vendor/App/TraitName.php @@ -0,0 +1,7 @@ +<?php + +namespace Vendor\App; + +trait TraitName { + +} diff --git a/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameOk/Vendor/App/TraitName.php.testTraitNameOk.hints b/php/php.editor/test/unit/data/testfiles/verification/PSR4/testTraitNameOk/Vendor/App/TraitName.php.testTraitNameOk.hints new file mode 100644 index 0000000..e69de29 diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/PSR4HintTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/PSR4HintTest.java new file mode 100644 index 0000000..f799d31 --- /dev/null +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/PSR4HintTest.java @@ -0,0 +1,108 @@ +/* + * 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.Collections; +import java.util.Map; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.modules.csl.api.Rule; +import org.netbeans.modules.php.project.api.PhpSourcePath; +import org.netbeans.spi.java.classpath.support.ClassPathSupport; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; + +public class PSR4HintTest extends PHPHintsTestBase { + + public PSR4HintTest(String testName) { + super(testName); + } + + public void testNamespaceOk() throws Exception { + checkHints(new PSR4Hint.NamespaceDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testNamespaceDifferent() throws Exception { + checkHints(new PSR4Hint.NamespaceDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testNamespaceDifferentCase() throws Exception { + checkHints(new PSR4Hint.NamespaceDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testSubNamespaceOk() throws Exception { + checkHints(new PSR4Hint.NamespaceDeclarationHint(), "App/ClassName.php"); + } + + public void testClassNameOk() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testClassNameDifferent() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testClassNameDifferentCase() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/ClassName.php"); + } + + public void testInterfaceNameOk() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/InterfaceName.php"); + } + + public void testInterfaceNameDifferent() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/InterfaceName.php"); + } + + public void testInterfaceNameDifferentCase() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/InterfaceName.php"); + } + + public void testTraitNameOk() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/TraitName.php"); + } + + public void testTraitNameDifferent() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/TraitName.php"); + } + + public void testTraitNameDifferentCase() throws Exception { + checkHints(new PSR4Hint.TypeDeclarationHint(), "Vendor/App/TraitName.php"); + } + + @Override + protected void checkHints(Rule hint, String fileName) throws Exception { + super.checkHints(hint, getTestDir() + "/" + fileName); + } + + @Override + protected Map<String, ClassPath> createClassPathsForTest() { + return Collections.singletonMap( + PhpSourcePath.SOURCE_CP, + ClassPathSupport.createClassPath(new FileObject[]{ + FileUtil.toFileObject(new File(getDataDir(), "/" + TEST_DIRECTORY + getTestDir())) + }) + ); + } + + private String getTestDir() { + return "PSR4/" + getName(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists