]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Raise a lint warning for classes not marked "abstract", "final" or
authorepriestley <git@epriestley.com>
Sat, 28 Jan 2012 19:29:30 +0000 (11:29 -0800)
committerepriestley <git@epriestley.com>
Sat, 28 Jan 2012 19:29:30 +0000 (11:29 -0800)
"@concrete-extensible"

Summary:
See T795. I think ~all classes in the classtree should be "abstract" or "final",
but I provided "@concrete-extensible" if you really have "Rectangle extends
Square extends Shape" or something.

I'm not totally sure this should be enabled globally by default, maybe I should
default it to DISABLED and then enable it for libphutil/arcanist/Phabricator? It
feels like it might be a little overbearing to push on everyone by defualt.

Test Plan: See unit tests.

Reviewers: btrahan, aran, nh, arudolph, jungejason

Reviewed By: btrahan

CC: aran, epriestley
Maniphest Tasks: T795

Differential Revision: https://secure.phabricator.com/D1510

12 files changed:
src/lint/engine/phutil/PhutilLintEngine.php
src/lint/engine/phutil/__init__.php
src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__init__.php
src/lint/linter/xhpast/__tests__/ArcanistXHPASTLinterTestCase.php
src/lint/linter/xhpast/__tests__/__init__.php
src/lint/linter/xhpast/__tests__/data/naming-conventions.lint-test
src/lint/linter/xhpast/__tests__/data/parens-hug-contents.lint-test
src/lint/linter/xhpast/__tests__/data/ragged-classtree-edges.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/__tests__/data/surprising-constructors.lint-test
src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test
src/lint/linter/xhpast/__tests__/data/use-of-this-in-static-method.lint-test

index e1db7b4bf0e0a80ae5a81a9b1e762dd81c275239..ac4ecdd8b2ee7567965892c03a5f89361c7a2cfe 100644 (file)
@@ -88,6 +88,11 @@ class PhutilLintEngine extends ArcanistLintEngine {
     }
 
     $xhpast_linter = new ArcanistXHPASTLinter();
+    $xhpast_linter->setCustomSeverityMap(
+      array(
+        ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE
+          => ArcanistLintSeverity::SEVERITY_WARNING,
+      ));
     $license_linter = new ArcanistApacheLicenseLinter();
     $linters[] = $xhpast_linter;
     $linters[] = $license_linter;
index 2a59785fb32c5e8a932173346102ea90e928ce97..9c270ee57897e58aa4c3a80d64ee03eaf4110650 100644 (file)
@@ -15,6 +15,7 @@ phutil_require_module('arcanist', 'lint/linter/phutilmodule');
 phutil_require_module('arcanist', 'lint/linter/spelling');
 phutil_require_module('arcanist', 'lint/linter/text');
 phutil_require_module('arcanist', 'lint/linter/xhpast');
+phutil_require_module('arcanist', 'lint/severity');
 
 
 phutil_require_source('PhutilLintEngine.php');
index 898402121fd753e7c57f39bd4f83cbc349d1c7a6..97598bfda21d0b8c0388c56ec98de0d66cde61ad 100644 (file)
@@ -51,7 +51,8 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_PARENTHESES_SPACING       = 25;
   const LINT_CONTROL_STATEMENT_SPACING = 26;
   const LINT_BINARY_EXPRESSION_SPACING = 27;
-  const LINT_ARRAY_INDEX_SPACING       = 27;
+  const LINT_ARRAY_INDEX_SPACING       = 28;
+  const LINT_RAGGED_CLASSTREE_EDGE     = 29;
 
 
   public function getLintNameMap() {
@@ -83,6 +84,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_CONTROL_STATEMENT_SPACING => 'Space After Control Statement',
       self::LINT_BINARY_EXPRESSION_SPACING => 'Space Around Binary Operator',
       self::LINT_ARRAY_INDEX_SPACING       => 'Spacing Before Array Index',
+      self::LINT_RAGGED_CLASSTREE_EDGE     => 'Class Not abstract Or final',
     );
   }
 
@@ -110,6 +112,10 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_ARRAY_INDEX_SPACING
         => ArcanistLintSeverity::SEVERITY_WARNING,
 
+      // This is disabled by default because it implies a very strict policy
+      // which isn't necessary in the general case.
+      self::LINT_RAGGED_CLASSTREE_EDGE
+        => ArcanistLintSeverity::SEVERITY_DISABLED,
     );
   }
 
@@ -175,6 +181,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintDuplicateKeysInArray($root);
     $this->lintReusedIterators($root);
     $this->lintBraceFormatting($root);
+    $this->lintRaggedClasstreeEdges($root);
   }
 
   private function lintBraceFormatting($root) {
@@ -1376,6 +1383,42 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     }
   }
 
+  private function lintRaggedClasstreeEdges($root) {
+    $parser = new PhutilDocblockParser();
+
+    $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
+    foreach ($classes as $class) {
+
+      $is_final = false;
+      $is_abstract = false;
+      $is_concrete_extensible = false;
+
+      $attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES');
+      foreach ($attributes->getChildren() as $child) {
+        if ($child->getConcreteString() == 'final') {
+          $is_final = true;
+        }
+        if ($child->getConcreteString() == 'abstract') {
+          $is_abstract = true;
+        }
+      }
+
+      $docblock = $class->getDocblockToken();
+      if ($docblock) {
+        list($text, $specials) = $parser->parse($docblock->getValue());
+        $is_concrete_extensible = idx($specials, 'concrete-extensible');
+      }
+
+      if (!$is_final && !$is_abstract && !$is_concrete_extensible) {
+        $this->raiseLintAtNode(
+          $class->getChildOfType(1, 'n_CLASS_NAME'),
+          self::LINT_RAGGED_CLASSTREE_EDGE,
+          "This class is neither 'final' nor 'abstract', and does not have ".
+          "a docblock marking it '@concrete-extensible'.");
+      }
+    }
+  }
+
   protected function raiseLintAtToken(
     XHPASTToken $token,
     $code,
index b98d8edbd163ad1748f03850b866384ca6eff2f6..4b8d2cfe7f7594173398b0ec5f18db18e76dcd79 100644 (file)
@@ -10,6 +10,7 @@ phutil_require_module('arcanist', 'lint/linter/base');
 phutil_require_module('arcanist', 'lint/linter/xhpast/naminghook');
 phutil_require_module('arcanist', 'lint/severity');
 
+phutil_require_module('phutil', 'parser/docblock');
 phutil_require_module('phutil', 'parser/xhpast/api/tree');
 phutil_require_module('phutil', 'parser/xhpast/bin');
 phutil_require_module('phutil', 'utils');
index fa0e41ee8bd890d4fa81a97bede6cf64cc38b6a1..b18aafd4ae5d12d643227c0432f3d1a503fcf096 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright 2011 Facebook, Inc.
+ * Copyright 2012 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,13 @@ class ArcanistXHPASTLinterTestCase extends ArcanistLinterTestCase {
 
   public function testXHPASTLint() {
     $linter = new ArcanistXHPASTLinter();
+
+    $linter->setCustomSeverityMap(
+      array(
+        ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE
+          => ArcanistLintSeverity::SEVERITY_WARNING,
+      ));
+
     $working_copy = ArcanistWorkingCopyIdentity::newFromPath(__FILE__);
     return $this->executeTestsInDirectory(
       dirname(__FILE__).'/data/',
index d960ac84767824e658cf9cb97552c0faba01b0e0..98800a54f8ece0a886edd1bdcbb51c1d3f800ecf 100644 (file)
@@ -8,6 +8,7 @@
 
 phutil_require_module('arcanist', 'lint/linter/base/test');
 phutil_require_module('arcanist', 'lint/linter/xhpast');
+phutil_require_module('arcanist', 'lint/severity');
 phutil_require_module('arcanist', 'workingcopyidentity');
 
 
index 96403246c5d701a8b8a6e76af8e65ef5bd44f15b..78e16a51d9a1ba2dd8b7003f80c360bb7c91d89c 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-class a {
+final class a {
   const b = 1, c = d;
   protected $E, $H;
   public function F($G, $GG) { }
@@ -11,7 +11,7 @@ interface i { }
 
 function YY($ZZ) { }
 
-class Quack {
+final class Quack {
   const R = 1, S = 2;
   protected $tX, $uY;
   public function vV($w_w) { }
@@ -50,7 +50,7 @@ function i() {
   parent::$Z_z[0];
 }
 ~~~~~~~~~~
-warning:2:7
+warning:2:13
 warning:3:9
 warning:3:16
 warning:4:13
@@ -65,4 +65,4 @@ warning:26:13
 warning:30:3
 warning:31:3
 warning:32:3
-warning:34:3
\ No newline at end of file
+warning:34:3
index a5c95041c203d2552364c696e53b94ef35d6f7a4..690f161f0c44f2b0d4ea4242527b4a285950dad9 100644 (file)
@@ -13,7 +13,7 @@ $obj->m(
 for ( $ii = 0; $ii < 1; $ii++ ) { }
 foreach ( $x as $y ) { }
 function q( $x ) { }
-class X {
+final class X {
   public function f( $y ) {
   }
 }
@@ -35,7 +35,7 @@ warning:14:10
 warning:14:19
 warning:15:12
 warning:15:15
-error:16:7 XHP19 Class-Filename Mismatch
+error:16:13 XHP19 Class-Filename Mismatch
 warning:17:21
 warning:17:24
 warning:20:10
@@ -56,7 +56,7 @@ $obj->m(
 for ($ii = 0; $ii < 1; $ii++) { }
 foreach ($x as $y) { }
 function q($x) { }
-class X {
+final class X {
   public function f($y) {
   }
 }
diff --git a/src/lint/linter/xhpast/__tests__/data/ragged-classtree-edges.lint-test b/src/lint/linter/xhpast/__tests__/data/ragged-classtree-edges.lint-test
new file mode 100644 (file)
index 0000000..9e18089
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+class A { }
+abstract class B { }
+final class C { }
+
+/**
+ * @concrete-extensible
+ */
+class D { }
+~~~~~~~~~~
+warning:3:7
\ No newline at end of file
index 363e6f8acc97baa6ff000fa73539cb33f3967d05..f68e36975700a484cf8868ca1e627c10c76d8795 100644 (file)
@@ -1,9 +1,9 @@
 <?php
-class Platypus {
+final class Platypus {
   public function platypus() {
     // This method must be renamed to __construct().
   }
 }
 ~~~~~~~~~~
-error:2:7 XHP19 Class-Filename Mismatch
+error:2:13 XHP19 Class-Filename Mismatch
 error:3:19
\ No newline at end of file
index 0add1437fbf1043c4814693eb56fee209e74681f..18f432675f12cfd30d9d7a1f5ebb3daa323e3322 100644 (file)
@@ -39,7 +39,7 @@ function g($q) {
   $r = y();
 }
 
-class C {
+final class C {
   public function m() {
     $a++;
     x($b);
@@ -135,7 +135,7 @@ function more_exceptions() {
   }
 }
 
-class P {
+abstract class P {
   abstract public function q();
 }
 
@@ -146,7 +146,7 @@ function x() {
   f(((($lub))));
 }
 
-class A {
+final class A {
   public function foo($a) {
     $im_service = foo($a);
     if ($im_servce === false) {
index 9007c9c0c5649af5c1cb5cc7a0d23aa83763f0ab..3ae1214bd291045f532ff1d25b187a5a3b29a3cf 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class A {
+final class A {
   public function u() {
     $this->f();
   }
@@ -10,5 +10,5 @@ class A {
 }
 
 ~~~~~~~~~~
-error:3:7 XHP19 Class-Filename Mismatch
+error:3:13 XHP19 Class-Filename Mismatch
 error:8:5 Use of $this in a static method.