From 026a322dd24a1f4d291f2ae6d4807931fb900bf7 Mon Sep 17 00:00:00 2001 From: adonohue Date: Fri, 14 Jan 2011 14:28:01 -0800 Subject: [PATCH] Implement lint check that filename matches only declared interface/class. Differential Revision: 201593 Reviewed By: epriestley --- .../linter/xhpast/ArcanistXHPASTLinter.php | 46 +++++++++++++++++++ .../data/parens-hug-contents.lint-test | 1 + .../data/surprising-constructors.lint-test | 1 + .../use-of-this-in-static-method.lint-test | 1 + 4 files changed, 49 insertions(+) diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index 548937d7..960eea8a 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -38,6 +38,8 @@ class ArcanistXHPASTLinter extends ArcanistLinter { const LINT_TODO_COMMENT = 16; const LINT_EXIT_EXPRESSION = 17; const LINT_COMMENT_STYLE = 18; + const LINT_CLASS_FILENAME_MISMATCH = 19; + public function getLintNameMap() { return array( @@ -59,6 +61,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter { self::LINT_TODO_COMMENT => 'TODO Comment', self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression', self::LINT_COMMENT_STYLE => 'Comment Style', + self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch', ); } @@ -128,6 +131,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter { $this->lintUndeclaredVariables($root); $this->lintArrayIndexWhitespace($root); $this->lintHashComments($root); + $this->lintPrimaryDeclarationFilenameMatch($root); } protected function lintHashComments($root) { @@ -880,6 +884,48 @@ class ArcanistXHPASTLinter extends ArcanistLinter { } } + /** + * Lint that if the file declares exactly one interface or class, + * the name of the file matches the name of the class, + * unless the classname is funky like an XHP element. + */ + private function lintPrimaryDeclarationFilenameMatch($root) { + $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); + $interfaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION'); + + if (count($classes) + count($interfaces) != 1) { + return; + } + + $declarations = count($classes) ? $classes : $interfaces; + $declarations->rewind(); + $declaration = $declarations->current(); + + $decl_name = $declaration->getChildByIndex(1); + $decl_string = $decl_name->getConcreteString(); + + //Exclude strangely named classes. + if (!preg_match('/\w+/', $decl_string)) { + return; + } + + $rename = $decl_string.'.php'; + + $path = $this->getActivePath(); + $filename = basename($path); + + if ($rename == $filename) { + return; + } + + $this->raiseLintAtNode( + $decl_name, + self::LINT_CLASS_FILENAME_MISMATCH, + "The name of this file differs from the name of the class or interface ". + "it declares. Rename the file to '{$rename}'." + ); + } + protected function raiseLintAtToken( XHPASTToken $token, $code, diff --git a/src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test b/src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test index 3f82d924..5cd11a37 100644 --- a/src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test @@ -35,6 +35,7 @@ warning:14:10 warning:14:19 warning:15:12 warning:15:15 +error:16:7 XHP19 Class-Filename Mismatch warning:17:21 warning:17:24 warning:20:10 diff --git a/src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test b/src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test index 229683f5..363e6f8a 100644 --- a/src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test @@ -5,4 +5,5 @@ class Platypus { } } ~~~~~~~~~~ +error:2:7 XHP19 Class-Filename Mismatch error:3:19 \ No newline at end of file diff --git a/src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test b/src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test index 1d340a55..9007c9c0 100644 --- a/src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test @@ -10,4 +10,5 @@ class A { } ~~~~~~~~~~ +error:3:7 XHP19 Class-Filename Mismatch error:8:5 Use of $this in a static method. -- 2.43.0