From 02674edcdd0cd9d30366ae44abfb84208fe16882 Mon Sep 17 00:00:00 2001 From: Bill Mitchell Date: Tue, 6 Dec 2016 22:46:25 +0000 Subject: [PATCH] Fixes #22 --- README.md | 3 + src/Codor/Sniffs/Classes/ClassLengthSniff.php | 49 ++++ tests/Sniffs/Classes/ClassLengthSniff.inc | 221 ++++++++++++++++++ tests/Sniffs/Classes/ClassLengthSniffTest.php | 24 ++ 4 files changed, 297 insertions(+) create mode 100644 src/Codor/Sniffs/Classes/ClassLengthSniff.php create mode 100644 tests/Sniffs/Classes/ClassLengthSniff.inc create mode 100644 tests/Sniffs/Classes/ClassLengthSniffTest.php diff --git a/README.md b/README.md index 96abf1d..ac97d91 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,6 @@ Functions/methods must have no more than 3 parameters. ### Codor.Files.ReturnNull ### Functions/methods must not return `null`. + +### Codor.Classes.ClassLength ### +Classes must be 200 lines of code or fewer. \ No newline at end of file diff --git a/src/Codor/Sniffs/Classes/ClassLengthSniff.php b/src/Codor/Sniffs/Classes/ClassLengthSniff.php new file mode 100644 index 0000000..2bc110e --- /dev/null +++ b/src/Codor/Sniffs/Classes/ClassLengthSniff.php @@ -0,0 +1,49 @@ +getTokens(); + $token = $tokens[$stackPtr]; + + $openParenthesis = $tokens[$token['scope_opener']]; + $closedParenthesis = $tokens[$token['scope_closer']]; + + $length = $closedParenthesis['line'] - $openParenthesis['line']; + + if ($length > $this->maxLength) { + $phpcsFile->addError("Class is {$length} lines. Must be {$this->maxLength} lines or fewer.", $stackPtr); + } + } +} diff --git a/tests/Sniffs/Classes/ClassLengthSniff.inc b/tests/Sniffs/Classes/ClassLengthSniff.inc new file mode 100644 index 0000000..08d6b80 --- /dev/null +++ b/tests/Sniffs/Classes/ClassLengthSniff.inc @@ -0,0 +1,221 @@ +runner->setSniff('Codor.Classes.ClassLength')->setFolder(__DIR__.'/'); + } + + public function testSniff() + { + $results = $this->runner->sniff('ClassLengthSniff.inc'); + $this->assertSame(1, $results->getErrorCount()); + $this->assertSame(0, $results->getWarningCount()); + } +} \ No newline at end of file