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 2018/10/27 05:14:34 UTC

[GitHub] tmysik closed pull request #987: Improve PHPStan level

tmysik closed pull request #987: Improve PHPStan level
URL: https://github.com/apache/incubator-netbeans/pull/987
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/PHPStanAnalyzerImpl.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/PHPStanAnalyzerImpl.java
index 457e667005..0d4506f6a4 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/PHPStanAnalyzerImpl.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/PHPStanAnalyzerImpl.java
@@ -171,7 +171,6 @@ private PHPStan getValidPHPStan() {
         return null;
     }
 
-    @CheckForNull
     private String getValidPHPStanLevel() {
         String phpStanLevel = null;
         Preferences settings = context.getSettings();
@@ -179,11 +178,10 @@ private String getValidPHPStanLevel() {
             phpStanLevel = settings.get(PHPStanCustomizerPanel.LEVEL, null);
         }
         if (phpStanLevel == null) {
-            phpStanLevel = String.valueOf(AnalysisOptions.getInstance().getPHPStanLevel());
+            phpStanLevel = AnalysisOptions.getInstance().getPHPStanLevel();
         }
         assert phpStanLevel != null;
-        return phpStanLevel;
-
+        return AnalysisOptions.getValidPHPStanLevel(phpStanLevel);
     }
 
     @CheckForNull
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/commands/PHPStan.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/commands/PHPStan.java
index 8b09b1aab4..3ee2d30dda 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/commands/PHPStan.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/commands/PHPStan.java
@@ -50,6 +50,7 @@
 
     public static final String NAME = "phpstan"; // NOI18N
     public static final String LONG_NAME = NAME + ".phar"; // NOI18N
+    public static final String MAX_LEVEL = "max"; // NOI18N
     static final File XML_LOG = new File(System.getProperty("java.io.tmpdir"), "nb-php-phpstan-log.xml"); // NOI18N
     private static final Logger LOGGER = Logger.getLogger(PHPStan.class.getName());
 
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/options/AnalysisOptions.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/options/AnalysisOptions.java
index 0faf6985fa..441f33aabf 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/options/AnalysisOptions.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/options/AnalysisOptions.java
@@ -54,6 +54,8 @@
     private static final String PHPSTAN_LEVEL = "phpstan.level"; // NOI18N
     private static final String PHPSTAN_CONFIGURATION = "phpstan.configuration"; // NOI18N
     private static final String PHPSTAN_MEMORY_LIMIT = "phpstan.memory.limit"; // NOI18N
+    public static final int PHPSTAN_MIN_LEVEL = Integer.getInteger("nb.phpstan.min.level", 0); // NOI18N
+    public static final int PHPSTAN_MAX_LEVEL = Integer.getInteger("nb.phpstan.max.level", 7); // NOI18N
 
     private volatile boolean codeSnifferSearched = false;
     private volatile boolean messDetectorSearched = false;
@@ -209,20 +211,26 @@ public void setPHPStanPath(String path) {
         getPreferences().put(PHPSTAN_PATH, path);
     }
 
-    public int getPHPStanLevel() {
-        int level = getPreferences().getInt(PHPSTAN_LEVEL, 0);
-        if (level < 0 || 7 < level) {
-            level = 0;
-        }
-        return level;
+    public String getPHPStanLevel() {
+        String level = getPreferences().get(PHPSTAN_LEVEL, String.valueOf(PHPSTAN_MIN_LEVEL));
+        return getValidPHPStanLevel(level);
+    }
+
+    public void setPHPStanLevel(String level) {
+        getPreferences().put(PHPSTAN_LEVEL, getValidPHPStanLevel(level));
     }
 
-    public void setPHPStanLevel(int level) {
-        int l = level;
-        if (level < 0 || 7 < level) {
-            l = 0;
+    public static String getValidPHPStanLevel(String level) {
+        if (PHPStan.MAX_LEVEL.equals(level)) {
+            return level;
+        }
+        String phpstanLevel;
+        try {
+            phpstanLevel = String.valueOf(AnalysisUtils.getValidInt(PHPSTAN_MIN_LEVEL, PHPSTAN_MAX_LEVEL, Integer.valueOf(level)));
+        } catch (NumberFormatException e) {
+            phpstanLevel = level;
         }
-        getPreferences().putInt(PHPSTAN_LEVEL, l);
+        return phpstanLevel;
     }
 
     @CheckForNull
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/PHPStanLevelListCellRenderer.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/PHPStanLevelListCellRenderer.java
index 6f316be25d..d98229b2d9 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/PHPStanLevelListCellRenderer.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/PHPStanLevelListCellRenderer.java
@@ -21,6 +21,7 @@
 import java.awt.Component;
 import javax.swing.JList;
 import javax.swing.ListCellRenderer;
+import org.netbeans.modules.php.analysis.options.AnalysisOptions;
 
 public class PHPStanLevelListCellRenderer implements ListCellRenderer<String> {
 
@@ -33,7 +34,7 @@ public PHPStanLevelListCellRenderer(ListCellRenderer<? super String> defaultRend
     @Override
     public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
         String level = value;
-        if ("7".equals(level)) { // NOI18N
+        if (String.valueOf(AnalysisOptions.PHPSTAN_MAX_LEVEL).equals(level)) {
             level += " (max)"; // NOI18N
         }
         return defaultRenderer.getListCellRendererComponent(list, level, index, isSelected, cellHasFocus);
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/analyzer/PHPStanCustomizerPanel.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/analyzer/PHPStanCustomizerPanel.java
index ed6b809828..9732892bed 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/analyzer/PHPStanCustomizerPanel.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/analyzer/PHPStanCustomizerPanel.java
@@ -26,6 +26,7 @@
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 import org.netbeans.modules.analysis.spi.Analyzer;
+import org.netbeans.modules.php.analysis.commands.PHPStan;
 import org.netbeans.modules.php.analysis.options.AnalysisOptions;
 import org.netbeans.modules.php.analysis.ui.PHPStanLevelListCellRenderer;
 import org.netbeans.modules.php.analysis.options.AnalysisOptionsValidator;
@@ -77,11 +78,21 @@ private void setEnabledCheckBox() {
 
     private void setLevelComboBox() {
         assert EventQueue.isDispatchThread();
+        phpStanLevelComboBox.removeAllItems();
+        for (int i = AnalysisOptions.PHPSTAN_MIN_LEVEL; i <= AnalysisOptions.PHPSTAN_MAX_LEVEL; i++) {
+            phpStanLevelComboBox.addItem(String.valueOf(i));
+        }
+        phpStanLevelComboBox.addItem(PHPStan.MAX_LEVEL);
         phpStanLevelComboBox.setRenderer(new PHPStanLevelListCellRenderer(phpStanLevelComboBox.getRenderer()));
-        phpStanLevelComboBox.setSelectedItem(settings.get(LEVEL, String.valueOf(AnalysisOptions.getInstance().getPHPStanLevel())));
+        phpStanLevelComboBox.setSelectedItem(getValidLevel());
         phpStanLevelComboBox.addItemListener(e -> setLevel());
     }
 
+    private String getValidLevel() {
+        String level = settings.get(LEVEL, AnalysisOptions.getInstance().getPHPStanLevel());
+        return AnalysisOptions.getValidPHPStanLevel(level);
+    }
+
     private void setConfigurationTextField() {
         assert EventQueue.isDispatchThread();
         phpStanConfigurationTextField.setText(settings.get(CONFIGURATION, AnalysisOptions.getInstance().getPHPStanConfigurationPath()));
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/options/PHPStanOptionsPanel.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/options/PHPStanOptionsPanel.java
index 638140b597..3c2bb5051f 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/options/PHPStanOptionsPanel.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/ui/options/PHPStanOptionsPanel.java
@@ -63,6 +63,11 @@ public PHPStanOptionsPanel() {
         "PHPStanOptionsPanel.hint=Full path of PHPStan script (typically {0} or {1}).",})
     private void init() {
         phpStanHintLabel.setText(Bundle.PHPStanOptionsPanel_hint(PHPStan.NAME, PHPStan.LONG_NAME));
+        phpStanLevelComboBox.removeAllItems();
+        for (int i = AnalysisOptions.PHPSTAN_MIN_LEVEL; i <= AnalysisOptions.PHPSTAN_MAX_LEVEL; i++) {
+            phpStanLevelComboBox.addItem(String.valueOf(i));
+        }
+        phpStanLevelComboBox.addItem(PHPStan.MAX_LEVEL);
         phpStanLevelComboBox.setRenderer(new PHPStanLevelListCellRenderer(phpStanLevelComboBox.getRenderer()));
         // add listener
         DefaultDocumentListener defaultDocumentListener = new DefaultDocumentListener();
@@ -365,9 +370,9 @@ public boolean isChanged() {
         if (saved == null ? !current.isEmpty() : !saved.equals(current)) {
             return true;
         }
-        int savedInt = AnalysisOptions.getInstance().getPHPStanLevel();
-        int currentInt = getPHPStanLevel();
-        return savedInt != currentInt;
+        String savedString = AnalysisOptions.getInstance().getPHPStanLevel();
+        String currentString = getPHPStanLevel();
+        return !savedString.equals(currentString);
     }
 
     @Override
@@ -397,12 +402,12 @@ private void setPHPStanConfigurationPath(String path) {
         phpStanConfigurationTextField.setText(path);
     }
 
-    public int getPHPStanLevel() {
-        return Integer.parseInt((String) phpStanLevelComboBox.getSelectedItem());
+    public String getPHPStanLevel() {
+        return (String) phpStanLevelComboBox.getSelectedItem();
     }
 
-    private void setPHPStanLevel(int level) {
-        phpStanLevelComboBox.setSelectedItem(String.valueOf(level));
+    private void setPHPStanLevel(String level) {
+        phpStanLevelComboBox.setSelectedItem(level);
     }
 
     public String getPHPStanMemoryLimit() {
diff --git a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/util/AnalysisUtils.java b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/util/AnalysisUtils.java
index 6e9998784f..8c0eab5410 100644
--- a/php/php.code.analysis/src/org/netbeans/modules/php/analysis/util/AnalysisUtils.java
+++ b/php/php.code.analysis/src/org/netbeans/modules/php/analysis/util/AnalysisUtils.java
@@ -167,4 +167,8 @@ private static int countPhpFiles(PhpVisibilityQuery visibilityQuery, FileObject
         return count;
     }
 
+    public static int getValidInt(int min, int max, int target) {
+        return Math.min(Math.max(min, target), max);
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

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