Skip to content

Commit

Permalink
Merge pull request #200 from HaxeCheckstyle/MethodCount
Browse files Browse the repository at this point in the history
MethodCount
  • Loading branch information
adireddy committed Mar 18, 2016
2 parents 232dc5f + 3e862d7 commit 2385906
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-cp src
-cp test
-lib haxeparser:3.2.0
-lib compiletime:2.6.0
-lib hxargs
Expand All @@ -10,6 +9,7 @@
-neko run.n

--next
-cp test
-lib mcover:2.1.1
-x TestMain
--macro mcover.MCover.coverage(['checkstyle'], ['src'], ['checkstyle.reporter', 'checkstyle.Main'])
Expand Down
15 changes: 15 additions & 0 deletions checkstyle.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
},
"type": "CyclomaticComplexity"
},
{
"props": {
"severity": "INFO"
},
"type": "DefaultComesLast"
},
{
"props": {
"severity": "INFO"
Expand Down Expand Up @@ -240,6 +246,15 @@
},
"type": "MemberName"
},
{
"props": {
"maxPrivate": 50,
"maxPublic": 50,
"maxTotal": 50,
"severity": "INFO"
},
"type": "MethodCount"
},
{
"props": {
"max": 50,
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"contributors": [
"adireddy"
],
"releasenote": "v2 release with more checks and lot of improvements/bug fixes",
"version": "2.0.0",
"releasenote": "new release",
"version": "2.1.0",
"url": "https://github.com/adireddy/haxe-checkstyle",
"dependencies": {

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "checkstyle",
"version": "2.0.0",
"version": "2.1.0",
"description": "Automated code analysis ideal for projects that want to enforce a coding standard.",
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@
},
"type": "MemberName"
},
{
"props": {
"maxPrivate": 100,
"maxPublic": 100,
"severity": "IGNORE",
"maxTotal": 100
},
"type": "MethodCount"
},
{
"props": {
"max": 50,
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/coding/HiddenFieldCheck.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkstyle.checks.coding;

import checkstyle.token.TokenTree;
import haxe.macro.Expr;
import haxeparser.Data.TokenDef;

using checkstyle.utils.ArrayUtils;
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/coding/MagicNumberCheck.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkstyle.checks.coding;

import checkstyle.token.TokenTree;
import haxe.macro.Expr;

using checkstyle.utils.ArrayUtils;

Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/coding/ReturnCountCheck.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkstyle.checks.coding;

import checkstyle.token.TokenTree;
import haxe.macro.Expr;

@name("ReturnCount")
@desc("Restricts the number of return statements in methods (2 by default). Ignores methods that matches `ignoreFormat` regex property.")
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/coding/TraceCheck.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkstyle.checks.coding;

import checkstyle.token.TokenTree;
import haxe.macro.Expr;

@name("Trace")
@desc("Checks for trace calls in code.")
Expand Down
52 changes: 52 additions & 0 deletions src/checkstyle/checks/size/MethodCountCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package checkstyle.checks.size;

import checkstyle.token.TokenTree;
import haxeparser.Data;

using checkstyle.utils.ArrayUtils;

@name("MethodCount")
@desc("Checks the number of methods declared in each type. This includes the number of each scope (`private` and `public`) as well as an overall total.")
class MethodCountCheck extends Check {

static var DEFAULT_MAX_COUNT:Int = 100;

public var maxTotal:Int;
public var maxPrivate:Int;
public var maxPublic:Int;

public function new() {
super(TOKEN);
maxTotal = DEFAULT_MAX_COUNT;
maxPrivate = DEFAULT_MAX_COUNT;
maxPublic = DEFAULT_MAX_COUNT;
categories = [Category.COMPLEXITY];
points = 21;
}

override function actualRun() {
var root:TokenTree = checker.getTokenTree();
var acceptableTokens:Array<TokenTree> = root.filter([Kwd(KwdFunction)], ALL);

if (acceptableTokens.length > maxTotal) {
log('Total number of methods is ${acceptableTokens.length} (max allowed is ${maxTotal})', 0, 0);
return;
}

var privateCount = 0;
var publicCount = 0;
for (token in acceptableTokens) {
if (token.filter([Kwd(KwdPublic)], FIRST).length > 0) publicCount++;
else privateCount++;
}

if (privateCount > maxPrivate) {
log('Number of private methods is ${privateCount} (max allowed is ${maxPrivate})', 0, 0);
return;
}
if (publicCount > maxPublic) {
log('Number of public methods is ${publicCount} (max allowed is ${maxPublic})', 0, 0);
return;
}
}
}
2 changes: 1 addition & 1 deletion src/checkstyle/checks/size/ParameterNumberCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import haxe.macro.Expr;
using checkstyle.utils.ArrayUtils;

@name("ParameterNumber")
@desc("Checks the number of parameters of a method (default is 7).")
@desc("Checks the number of parameters of a method.")
class ParameterNumberCheck extends Check {

static var DEFAULT_MAX_PARAMS:Int = 7;
Expand Down
160 changes: 160 additions & 0 deletions test/checks/size/MethodCountCheckTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package checks.size;

import checkstyle.checks.size.MethodCountCheck;

class MethodCountCheckTest extends CheckTestCase<MethodCountCheckTests> {

public function testTotal() {
var check = new MethodCountCheck();
assertMsg(check, TEST1, "Total number of methods is 101 (max allowed is 100)");
}

public function testPrivate() {
var check = new MethodCountCheck();
check.maxPrivate = 5;
assertMsg(check, TEST2, "Number of private methods is 8 (max allowed is 5)");
}

public function testPublic() {
var check = new MethodCountCheck();
check.maxPublic = 5;
assertMsg(check, TEST3, "Number of public methods is 7 (max allowed is 5)");
}

public function testCorrectCount() {
assertNoMsg(new MethodCountCheck(), TEST3);
}
}

@:enum
abstract MethodCountCheckTests(String) to String {
var TEST1 = "
abstractAndClass Test {
public function test1() {}
public function test2() {}
public function test3() {}
public function test4() {}
public function test5() {}
public function test6() {}
public function test7() {}
public function test8() {}
public function test9() {}
public function test10() {}
public function test11() {}
public function test12() {}
public function test13() {}
public function test14() {}
public function test15() {}
public function test16() {}
public function test17() {}
public function test28() {}
public function test19() {}
public function test20() {}
public function test21() {}
public function test22() {}
public function test23() {}
public function test24() {}
public function test25() {}
function test26() {}
public function test27() {}
public function test28() {}
public function test29() {}
public function test30() {}
public function test31() {}
public function test32() {}
public function test33() {}
public function test34() {}
public function test35() {}
public function test36() {}
public function test37() {}
public function test38() {}
static function test39() {}
public function test40() {}
public function test41() {}
public function test42() {}
public function test43() {}
public function test44() {}
function test45() {}
public function test46() {}
public function test47() {}
public function test48() {}
public function test49() {}
public function test50() {}
public function test51() {}
public function test52() {}
public function test53() {}
public function test54() {}
public function test55() {}
public function test56() {}
public static function test57() {}
public function test58() {}
public function test59() {}
public function test60() {}
public function test61() {}
public function test62() {}
public function test63() {}
public function test64() {}
public function test65() {}
public function test66() {}
public function test67() {}
public function test68() {}
public function test69() {}
public function test70() {}
public function test71() {}
public function test72() {}
public function test73() {}
public function test74() {}
public function test75() {}
public function test76() {}
public function test77() {}
public function test78() {}
public function test79() {}
public function test80() {}
public function test81() {}
function test82() {}
public function test83() {}
public function test84() {}
public function test85() {}
public function test86() {}
public function test87() {}
public function test88() {}
public function test89() {}
public function test90() {}
public function test91() {}
public function test92() {}
public function test93() {}
public function test94() {}
public function test95() {}
public function test96() {}
public function test97() {}
public function test98() {}
public function test99() {}
public function test100() {}
public function test101() {}
}";

var TEST2 = "
abstractAndClass Test {
function test1() {}
function test2() {}
function test3() {}
function test4() {}
function test5() {}
static function test6() {}
function test7() {}
static inline function test8() {}
public static inline function test9() {}
}";

var TEST3 = "
abstractAndClass Test {
public function test1() {}
public function test2() {}
public function test3() {}
public function test4() {}
public function test5() {}
public static function test6() {}
static inline function test8() {}
public static inline function test9() {}
}";
}

0 comments on commit 2385906

Please sign in to comment.