Skip to content

Commit

Permalink
Fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
bmitch committed Dec 11, 2016
1 parent 00546d0 commit c1447a2
Show file tree
Hide file tree
Showing 32 changed files with 667 additions and 11 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ Classes must be no more than 200 lines.
### Codor.Files.FunctionNameContainsAndOr ###
Functions/methods cannot contain "And" or "Or". This could be a sign of a function that does more than one thing.

### Codor.Files.IndentationLevel ###
Functions/methods cannot have more than 1 level of indentation.


## Customizing Sniffs ##
Some of the sniff rules can be customized to your liking. For example, if you'd want the `Codor.Files.FunctionLength` to make sure your functions are no more than 30 lines instead of 20, you can do that. Here's an example of a `codor.xml` file with that customization:
```
Expand All @@ -94,11 +98,13 @@ Some of the sniff rules can be customized to your liking. For example, if you'd

### Customizations Available
* `Codor.Files.FunctionLength`
* `maxLength`: The maximum number of lines a function/method can be.
* `maxLength`: The maximum number of lines a function/method can be (default = 200).
* `Codor.Files.FunctionParameter`
* `maxParameters`: The maximum number of parameters a function/method can have.
* `maxParameters`: The maximum number of parameters a function/method can have (default = 3).
* `Codor.Classes.ClassLength`
* `maxLength`: The maximum number of lines a Class can be.
* `maxLength`: The maximum number of lines a Class can be (default = 20).
* `Codor.Files.IndentationLevel`
* `indentationLimit`: Cannot have more than or equal to this number of indentations (default = 2).

## Contributing ##
Please see [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
7 changes: 3 additions & 4 deletions src/Codor/Sniffs/Files/FunctionNameContainsAndOrSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
*/
protected function containsKeywords($string)
{
$contains = false;
foreach ($this->keywords as $keyword) {
if ($this->contains($keyword, $string)) {
return true;
}
$this->contains($keyword, $string) ? $contains = true : null;
}

return false;
return $contains;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Codor/Sniffs/Files/FunctionParameterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

$numberOfParameters = 0;
for ($index=$openParenIndex+1; $index <= $closedParenIndex; $index++) {
if ($tokens[$index]['type'] == 'T_VARIABLE') {
$numberOfParameters++;
}
$tokens[$index]['type'] == 'T_VARIABLE' ? $numberOfParameters++ : null;
}

if ($numberOfParameters > $this->maxParameters) {
Expand Down
107 changes: 107 additions & 0 deletions src/Codor/Sniffs/Files/IndentationLevelSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Codor\Sniffs\Files;

use PHP_CodeSniffer_Sniff;
use PHP_CodeSniffer_File;

class IndentationLevelSniff implements PHP_CodeSniffer_Sniff
{
/**
* Indentations cannot be >= to this number.
* @var integer
*/
public $indentationLimit = 2;

/**
* The highest indentation level found.
* @var integer
*/
protected $maxIndentationFound;

/**
* Returns the token types that this sniff is interested in.
* @return array
*/
public function register()
{
return [T_FUNCTION, T_CLOSURE, T_SWITCH];
}

/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param integer $stackPtr The position in the stack where
* the token was found.
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$this->maxIndentationFound = 0;

// Ignore functions with no body
if (isset($token['scope_opener']) === false) {
return;
}

$this->inspectScope($token, $tokens);

if ($this->maxIndentationFound <= $this->indentationLimit) {
return;
}

$phpcsFile->addError($this->getErrorMessage(), $stackPtr);
}

/**
* Inspect the tokens in the scope of the provided $token.
* @codingStandardsIgnoreStart
* @param array $token Token Data.
* @param array $tokens Tokens.
* @return void
*/
protected function inspectScope(array $token, array $tokens)
{
$start = $token['scope_opener'];
$end = $token['scope_closer'];
$this->relativeScopeLevel = $tokens[$start]['level'];
for ($index=$start; $index <= $end; $index++) {
$nestedToken = $tokens[$index];
if ($nestedToken['type'] === "T_SWITCH") {
return;
}
$this->adjustMaxIndentationFound($nestedToken);
}
}
// @codingStandardsIgnoreEnd

/**
* Adjust the maximum indentation level found value.
* @param array $nestedToken Token data.
* @return void
*/
protected function adjustMaxIndentationFound(array $nestedToken)
{
$tokenNestedLevel = $nestedToken['level'];
$nestedLevel = $tokenNestedLevel - $this->relativeScopeLevel;
$nestedLevel > $this->maxIndentationFound ? $this->maxIndentationFound = $nestedLevel : null;
}

/**
* Produce the error message.
* @return string
*/
protected function getErrorMessage()
{
// Hack to fix the output numbers for the
// indentation levels found and the
// indentation limit.
$indentationFound = $this->maxIndentationFound - 1;
$indentationLimit = $this->indentationLimit - 1;
return "{$indentationFound} indenation levels found. " .
"Maximum of {$indentationLimit} indenation levels allowed.";
}
}
3 changes: 2 additions & 1 deletion src/Codor/Sniffs/Files/ReturnNullSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function register()

/**
* Processes the tokens that this sniff is interested in.
*
* @codingStandardsIgnoreStart
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param integer $stackPtr The position in the stack where
* the token was found.
Expand All @@ -39,6 +39,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
break;
}
}
// @codingStandardsIgnoreEnd

if ($returnValueToken['type'] === 'T_NULL') {
$error = "Return null value found.";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$foo = function ()
{
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$foo = function ()
{
$oneIndentation;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$foo = function ()
{
$oneIndentation;
if (true) {
$twoIndentation;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$foo = function ()
{
$oneIndentation;
if (true) {
$twoIndentation;
if (true) {
$threeIndentation;
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function foo()
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function foo()
{
$oneIndentation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

function foo()
{
$oneIndentation;
if (true) {
$twoIndentation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

function foo()
{
$oneIndentation;
if (true) {
$twoIndentation;
if (true) {
$threeIndentation;
}
}
}
40 changes: 40 additions & 0 deletions tests/Sniffs/Files/Assets/IndentationLevelSniff/InvalidClass.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

class Foo
{

protected $baz;

public function foo()
{
return true;
}

public function bar()
{
$one = 1;
$two = 2;
$three = 3;
return true;
}

public function baz()
{
if (true) {
return true;
}

return false;
}

public function tooMuchIndentation()
{
if (true) {
if (true) {
return "Uh oh";
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class foo
{
public function bar()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class foo
{
public function bar()
{
$oneIndentation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class foo
{
public function bar()
{
$oneIndentation;
if (true) {
$twoIndentation;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class foo
{
public function bar()
{
$oneIndentation;
if (true) {
$twoIndentation;
if (true) {
$threeIndentation;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function foobar()
{
switch ($condition) {
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

function foobar()
{
switch ($condition) {
case true:
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

function foobar()
{
switch ($condition) {
case '1':
echo 'hi';
break;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function foobar()
{
switch ($condition) {
case '1':
if (true) {
$threeIndentation;
}
break;
}
}

Loading

0 comments on commit c1447a2

Please sign in to comment.