From eb8b414cc73b9283a5558ad16dfa9d3ac598fe08 Mon Sep 17 00:00:00 2001 From: vrana Date: Thu, 14 Feb 2013 15:18:39 -0800 Subject: [PATCH] Don't run disabled lint rules Summary: We always generate all messages and then filter them out based on minimum severity. It's lots of useless work, especially in commit hook mode where we are interested only in errors. Test Plan: $ arc lint --cache 0 --severity error ArcanistXHPASTLinter.php 0.406 s before, 0.074 after Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D4963 --- src/lint/ArcanistLintSeverity.php | 5 +- src/lint/engine/ArcanistLintEngine.php | 8 ++- src/lint/linter/ArcanistLinter.php | 5 ++ src/lint/linter/ArcanistXHPASTLinter.php | 79 +++++++++++++++--------- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/lint/ArcanistLintSeverity.php b/src/lint/ArcanistLintSeverity.php index 3466cde1..05ec3723 100644 --- a/src/lint/ArcanistLintSeverity.php +++ b/src/lint/ArcanistLintSeverity.php @@ -33,9 +33,7 @@ final class ArcanistLintSeverity { return $map[$severity_code]; } - public static function isAtLeastAsSevere( - ArcanistLintMessage $message, - $level) { + public static function isAtLeastAsSevere($message_sev, $level) { static $map = array( self::SEVERITY_DISABLED => 10, @@ -45,7 +43,6 @@ final class ArcanistLintSeverity { self::SEVERITY_ERROR => 40, ); - $message_sev = $message->getSeverity(); if (empty($map[$message_sev])) { return true; } diff --git a/src/lint/engine/ArcanistLintEngine.php b/src/lint/engine/ArcanistLintEngine.php index 39f995ea..9ea1ddca 100644 --- a/src/lint/engine/ArcanistLintEngine.php +++ b/src/lint/engine/ArcanistLintEngine.php @@ -247,9 +247,8 @@ abstract class ArcanistLintEngine { $this->didRunLinters($linters); foreach ($linters as $linter) { - $minimum = $this->minimumSeverity; foreach ($linter->getLintMessages() as $message) { - if (!ArcanistLintSeverity::isAtLeastAsSevere($message, $minimum)) { + if (!$this->isSeverityEnabled($message->getSeverity())) { continue; } if (!$this->isRelevantMessage($message)) { @@ -307,6 +306,11 @@ abstract class ArcanistLintEngine { return $this->results; } + public function isSeverityEnabled($severity) { + $minimum = $this->minimumSeverity; + return ArcanistLintSeverity::isAtLeastAsSevere($severity, $minimum); + } + private function shouldUseCache($cache_granularity, $repository_version) { switch ($cache_granularity) { case ArcanistLinter::GRANULARITY_FILE: diff --git a/src/lint/linter/ArcanistLinter.php b/src/lint/linter/ArcanistLinter.php index a910accc..1d5867ab 100644 --- a/src/lint/linter/ArcanistLinter.php +++ b/src/lint/linter/ArcanistLinter.php @@ -226,6 +226,11 @@ abstract class ArcanistLinter { // This is a hook. } + protected function isCodeEnabled($code) { + $severity = $this->getLintMessageSeverity($code); + return $this->getEngine()->isSeverityEnabled($severity); + } + public function getLintSeverityMap() { return array(); } diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 52b55c74..4f6035ba 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -175,34 +175,57 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { $root = $this->trees[$path]->getRootNode(); - $this->lintUseOfThisInStaticMethods($root); - $this->lintDynamicDefines($root); - $this->lintSurpriseConstructors($root); - $this->lintPHPTagUse($root); - $this->lintVariableVariables($root); - $this->lintTODOComments($root); - $this->lintExitExpressions($root); - $this->lintSpaceAroundBinaryOperators($root); - $this->lintSpaceAfterControlStatementKeywords($root); - $this->lintParenthesesShouldHugExpressions($root); - $this->lintNamingConventions($root); - $this->lintPregQuote($root); - $this->lintUndeclaredVariables($root); - $this->lintArrayIndexWhitespace($root); - $this->lintCommentSpaces($root); - $this->lintHashComments($root); - $this->lintPrimaryDeclarationFilenameMatch($root); - $this->lintTautologicalExpressions($root); - $this->lintPlusOperatorOnStrings($root); - $this->lintDuplicateKeysInArray($root); - $this->lintReusedIterators($root); - $this->lintBraceFormatting($root); - $this->lintRaggedClasstreeEdges($root); - $this->lintImplicitFallthrough($root); - $this->lintPHP53Features($root); - $this->lintPHP54Features($root); - $this->lintStrposUsedForStart($root); - $this->lintStrstrUsedForCheck($root); + $method_codes = array( + 'lintStrstrUsedForCheck' => self::LINT_SLOWNESS, + 'lintStrposUsedForStart' => self::LINT_SLOWNESS, + 'lintPHP53Features' => self::LINT_PHP_53_FEATURES, + 'lintPHP54Features' => self::LINT_PHP_54_FEATURES, + 'lintImplicitFallthrough' => self::LINT_IMPLICIT_FALLTHROUGH, + 'lintBraceFormatting' => self::LINT_BRACE_FORMATTING, + 'lintTautologicalExpressions' => self::LINT_TAUTOLOGICAL_EXPRESSION, + 'lintCommentSpaces' => self::LINT_COMMENT_SPACING, + 'lintHashComments' => self::LINT_COMMENT_STYLE, + 'lintReusedIterators' => self::LINT_REUSED_ITERATORS, + 'lintVariableVariables' => self::LINT_VARIABLE_VARIABLE, + 'lintUndeclaredVariables' => array( + self::LINT_EXTRACT_USE, + self::LINT_REUSED_AS_ITERATOR, + self::LINT_UNDECLARED_VARIABLE, + ), + 'lintPHPTagUse' => array( + self::LINT_PHP_SHORT_TAG, + self::LINT_PHP_ECHO_TAG, + self::LINT_PHP_OPEN_TAG, + self::LINT_PHP_CLOSE_TAG, + ), + 'lintNamingConventions' => self::LINT_NAMING_CONVENTIONS, + 'lintSurpriseConstructors' => self::LINT_IMPLICIT_CONSTRUCTOR, + 'lintParenthesesShouldHugExpressions' => self::LINT_PARENTHESES_SPACING, + 'lintSpaceAfterControlStatementKeywords' => + self::LINT_CONTROL_STATEMENT_SPACING, + 'lintSpaceAroundBinaryOperators' => self::LINT_BINARY_EXPRESSION_SPACING, + 'lintDynamicDefines' => self::LINT_DYNAMIC_DEFINE, + 'lintUseOfThisInStaticMethods' => self::LINT_STATIC_THIS, + 'lintPregQuote' => self::LINT_PREG_QUOTE_MISUSE, + 'lintExitExpressions' => self::LINT_EXIT_EXPRESSION, + 'lintArrayIndexWhitespace' => self::LINT_ARRAY_INDEX_SPACING, + 'lintTODOComments' => self::LINT_TODO_COMMENT, + 'lintPrimaryDeclarationFilenameMatch' => + self::LINT_CLASS_FILENAME_MISMATCH, + 'lintPlusOperatorOnStrings' => self::LINT_PLUS_OPERATOR_ON_STRINGS, + 'lintDuplicateKeysInArray' => self::LINT_DUPLICATE_KEYS_IN_ARRAY, + 'lintRaggedClasstreeEdges' => self::LINT_RAGGED_CLASSTREE_EDGE, + ); + + foreach ($method_codes as $method => $codes) { + foreach ((array)$codes as $code) { + if ($this->isCodeEnabled($code)) { + call_user_func(array($this, $method), $root); + break; + } + } + } + } public function lintStrstrUsedForCheck($root) {