Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added NumberOfMethodsInTypeChecker

  • Loading branch information...
commit 584f008542f57535102866dc4e46734e19345d26 1 parent d877deb
@matthewfarwell authored
View
17 README.md
@@ -408,3 +408,20 @@ No Parameters
<parameter name="regex">^[A-Za-z]*$</parameter>
</parameters>
</check>
+
+## Class org.scalastyle.scalariform.NumberOfMethodsInTypeChecker - Check that there are not too many methods declared in a type
+
+ * id - number.of.methods
+ * default level - WarningLevel
+
+### Parameters
+
+ * maxMethods: number of methods per type
+
+### Example
+
+ <check level="warning" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" enabled="true">
+ <parameters>
+ <parameter name="maxMethods">30</parameter>
+ </parameters>
+ </check>
View
10 lib/scalastyle_config.xml
@@ -101,4 +101,14 @@
<parameter name="maxLength">50</parameter>
</parameters>
</check>
+ <check level="warning" class="org.scalastyle.scalariform.MethodNamesChecker" enabled="true">
+ <parameters>
+ <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
+ </parameters>
+ </check>
+ <check level="warning" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" enabled="true">
+ <parameters>
+ <parameter name="maxMethods">30</parameter>
+ </parameters>
+ </check>
</scalastyle>
View
5 src/main/resources/scalastyle_definition.xml
@@ -93,4 +93,9 @@
<parameter name="regex" type="string" default="^[a-z][A-Za-z0-9]*$" />
</parameters>
</checker>
+ <checker class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" id="number.of.methods" defaultLevel="warning" >
+ <parameters>
+ <parameter name="maxMethods" type="integer" default="30" />
+ </parameters>
+ </checker>
</scalastyle-definition>
View
6 src/main/resources/scalastyle_messages.properties
@@ -154,3 +154,9 @@ method.name.label = Method name
method.name.description = Check that method names match a regular expression
method.name.regex.label = Regular expression
method.name.regex.description = The method names must match this regular expression
+
+number.of.methods.message = Number of methods in class exceeds {0}
+number.of.methods.label = Number of methods in type
+number.of.methods.description = Check that a class / trait / object does not have too many methods
+number.of.methods.maxMethods.label = Maximum methods
+number.of.methods.maxMethods.description = The maximum number of methods allowed
View
36 src/main/scala/org/scalastyle/scalariform/NumberOfMethodsInTypeChecker.scala
@@ -0,0 +1,36 @@
+// Copyright (C) 2011-2012 the original author or authors.
+// See the LICENCE.txt file distributed with this work for additional
+// information regarding copyright ownership.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.scalastyle.scalariform
+
+import scalariform.parser.CompilationUnit
+import _root_.scalariform.lexer.Tokens._
+import _root_.scalariform.lexer.Token
+import _root_.scalariform.parser._
+import org.scalastyle.ScalariformChecker
+import org.scalastyle._
+
+class NumberOfMethodsInTypeChecker extends AbstractMethodChecker {
+ val errorKey = "number.of.methods"
+ val DefaultMaxMethods = 30
+
+ override def params() = List("" + getInt("maxMethods", DefaultMaxMethods))
+
+ def matches(t: BaseClazz[AstNode]): Boolean = {
+ val maxMethods = getInt("maxMethods", DefaultMaxMethods)
+ t.subs.size > maxMethods
+ }
+}
View
60 src/test/scala/org/scalastyle/scalariform/NumberOfMethodsInTypeCheckerTest.scala
@@ -0,0 +1,60 @@
+// Copyright (C) 2011-2012 the original author or authors.
+// See the LICENCE.txt file distributed with this work for additional
+// information regarding copyright ownership.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.scalastyle.scalariform
+
+import org.scalastyle.file.CheckerTest
+import org.scalatest.junit.AssertionsForJUnit
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.scalastyle.Checker
+import org.scalastyle.StyleError
+import java.util.Set
+import org.junit.Before
+import org.junit.Test
+
+// scalastyle:off magic.number
+
+class NumberOfMethodsInTypeCheckerTest extends AssertionsForJUnit with CheckerTest {
+ val key = "number.of.methods"
+ val classUnderTest = classOf[NumberOfMethodsInTypeChecker]
+
+ @Test def testOK() = {
+ val source = """
+package foobar
+
+class F1() {
+ def method1() = 1
+ def method2() = 1
+ def method3() = 1
+ def method4() = 1
+ def method5() = 1
+}
+
+class F2() {
+ def method1() = {
+ def foobar() = 6
+ 5
+ }
+ def method2() = 1
+ def method3() = 1
+ def method4() = 1
+}
+""";
+
+ assertErrors(List(columnError(4, 6, List("4"))), source, Map("maxMethods" -> "4"))
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.