]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Implement lint check that filename matches only declared interface/class.
authoradonohue <aran@fb.com>
Fri, 14 Jan 2011 22:28:01 +0000 (14:28 -0800)
committeradonohue <aran@fb.com>
Fri, 14 Jan 2011 23:30:11 +0000 (15:30 -0800)
Differential Revision: 201593
Reviewed By: epriestley

src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test
src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test
src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test

index 548937d70ef178b50802e8480c8f7fa05bdc2cfd..960eea8a1ff134bfb3488420e724b5ac528ace51 100644 (file)
@@ -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,
index 3f82d924369a4515868869b212e240bcccdd8223..5cd11a3719cdcba9250b703db7028137c2c8a583 100644 (file)
@@ -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
index 229683f5b7458ed63f64a2fb1136b3e2d909450e..363e6f8acc97baa6ff000fa73539cb33f3967d05 100644 (file)
@@ -5,4 +5,5 @@ class Platypus {
   }
 }
 ~~~~~~~~~~
+error:2:7 XHP19 Class-Filename Mismatch
 error:3:19
\ No newline at end of file
index 1d340a5541da2063c260393706585158964bfae6..9007c9c0c5649af5c1cb5cc7a0d23aa83763f0ab 100644 (file)
@@ -10,4 +10,5 @@ class A {
 }
 
 ~~~~~~~~~~
+error:3:7 XHP19 Class-Filename Mismatch
 error:8:5 Use of $this in a static method.