lbownik commented on code in PR #5648:
URL: https://github.com/apache/netbeans/pull/5648#discussion_r1132560046


##########
php/php.editor/src/org/netbeans/modules/php/editor/verification/ReturnStatementWithoutValueHintError.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import org.netbeans.api.annotations.common.NullAllowed;
+import org.netbeans.modules.csl.api.Hint;
+import org.netbeans.modules.csl.api.HintFix;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
+import org.netbeans.modules.php.api.PhpVersion;
+import org.netbeans.modules.php.editor.CodeUtils;
+import org.netbeans.modules.php.editor.model.FileScope;
+import org.netbeans.modules.php.editor.model.impl.Type;
+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.Expression;
+import org.netbeans.modules.php.editor.parser.astnodes.FunctionDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.IntersectionType;
+import 
org.netbeans.modules.php.editor.parser.astnodes.LambdaFunctionDeclaration;
+import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName;
+import org.netbeans.modules.php.editor.parser.astnodes.NullableType;
+import org.netbeans.modules.php.editor.parser.astnodes.ReturnStatement;
+import org.netbeans.modules.php.editor.parser.astnodes.UnionType;
+import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
+import org.openide.filesystems.FileObject;
+import org.openide.util.NbBundle;
+
+/**
+ * Checks if the return statement has a value if a return type is specified 
for the function
+ *
+ */
+public class ReturnStatementWithoutValueHintError extends HintErrorRule {
+
+    private FileObject fileObject;
+
+    @NbBundle.Messages("ReturnStatementWithoutValueHintErrorName=Return 
statement without value")
+    @Override
+    public String getDisplayName() {
+        return Bundle.ReturnStatementWithoutValueHintErrorName();
+    }
+
+    @Override
+    public void invoke(PHPRuleContext context, List<Hint> hints) {
+        PHPParseResult phpParseResult = (PHPParseResult) context.parserResult;
+        if (phpParseResult.getProgram() == null) {
+            return;
+        }
+        FileScope fileScope = context.fileScope;
+        fileObject = phpParseResult.getSnapshot().getSource().getFileObject();
+        if (fileScope != null && fileObject != null && appliesTo(fileObject)) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
+            CheckVisitor checkVisitor = new CheckVisitor();
+            phpParseResult.getProgram().accept(checkVisitor);
+            Map<ASTNode, Set<ReturnStatement>> returnStatements = 
checkVisitor.getReturnStatements();
+            checkReturnType(returnStatements, hints);
+        }
+    }
+
+    protected PhpVersion getPhpVersion(@NullAllowed FileObject file) {
+        if (file == null) {
+            return PhpVersion.getDefault();
+        }
+        return CodeUtils.getPhpVersion(file);
+    }
+
+    private boolean appliesTo(FileObject file) {
+        return getPhpVersion(file).compareTo(PhpVersion.PHP_70) >= 0;
+    }
+
+    private void checkReturnType(Map<ASTNode, Set<ReturnStatement>> 
returnStatements, List<Hint> hints) {
+        for (Entry<ASTNode, Set<ReturnStatement>> entry : 
returnStatements.entrySet()) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
+            ASTNode node = entry.getKey();
+            Expression returnType = null;
+            if (node instanceof FunctionDeclaration) {
+                returnType = ((FunctionDeclaration) node).getReturnType();
+            } else if (node instanceof LambdaFunctionDeclaration) {
+                returnType = ((LambdaFunctionDeclaration) 
node).getReturnType();
+            }
+
+            if (returnType == null) {
+                continue;
+            }
+            Set<ReturnStatement> statements = entry.getValue();
+
+            if (returnType instanceof NamespaceName) {
+                NamespaceName namespaceName = (NamespaceName) returnType;
+                String name = CodeUtils.extractUnqualifiedName(namespaceName);
+                checkReturnStatementsWithoutValue(statements, name, hints);
+            } else if (returnType instanceof NullableType
+                    || returnType instanceof UnionType
+                    || returnType instanceof IntersectionType) {
+                checkReturnStatementsWithoutValue(statements, "", hints);
+            }
+
+        }
+    }
+
+    @NbBundle.Messages({
+        "ReturnStatementWithoutValueHintErrorDesc=Return statement must be 
filled with the value"
+    })
+    private void checkReturnStatementsWithoutValue(Set<ReturnStatement> 
statements, String name, List<Hint> hints) {
+        if (!Type.VOID.equals(name) && !isNeverType(name)) {
+            // check empty return statement
+            statements.forEach((statement) -> {
+                if (CancelSupport.getDefault().isCancelled()) {
+                    return;

Review Comment:
   I don't know. I just spotted a strange costruct and brought it to your 
attention. Maybe it is perfectly fine.



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to