Skip to content

Commit

Permalink
Merge pull request #7 from SocialEngine/feature/allow-snakecase-confi…
Browse files Browse the repository at this point in the history
…gurable-arrays

Allow test method prefix and test class suffix to be configurable arrays
  • Loading branch information
Danno040 committed Oct 14, 2014
2 parents ed39532 + 10d80a9 commit 6bf8113
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
<?php

/**
* Warehouse_Sniffs_Methods_MethodNameSniff.
*
Expand All @@ -8,8 +9,16 @@
class SocialEngine_Sniffs_Methods_MethodNameSniff extends
PSR1_Sniffs_Methods_CamelCapsMethodNameSniff
{
protected $testMethodPrefix = 'test';
protected $testClassSuffix = 'Test';
protected $allowSnakeCaseMethodName = [];

/**
* Constructs a SocialEngine_Sniffs_Methods_MethodNameSniff.
*/
public function __construct()
{
parent::__construct();
$this->allowSnakeCaseMethodName = \Config::get('sniffer-rules::allowSnakeCaseMethodName', []);
}

/**
* Processes the tokens within the scope.
Expand Down Expand Up @@ -37,9 +46,8 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta

$testName = ltrim($methodName, '_');
$className = $phpcsFile->getDeclarationName($currScope);
$testClassSuffixPos = strrpos($className, $this->testClassSuffix);
$isTestClass = $testClassSuffixPos === (strlen($className) - strlen($this->testClassSuffix));
if ($isTestClass && strpos($methodName, $this->testMethodPrefix) === 0) {

if ($this->isAllowSnakeCaseMethodName($className, $testName)) {
if ($this->isUnderscoreName($testName) === false) {
$error = 'Test Method name "%s" is not in underscore format';
$errorData = array($className . '::' . $methodName);
Expand All @@ -51,7 +59,55 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
}


private function isAllowSnakeCaseMethodName($className, $methodName)
{
foreach ($this->allowSnakeCaseMethodName as $classMethodPair) {
$classOk = $this->checkClassHasValidSuffix($className, $classMethodPair['classSuffix']);
$methodOk = $this->checkMethodHasValidPrefix($methodName, $classMethodPair['methodPrefix']);
if ($classOk && $methodOk) {
return true;
}
}
}

/**
* Returns true if the specified class suffix is in the class name.
*
* @param string $className
* @param string $classSuffix
*
* @return boolean
*/
private function checkClassHasValidSuffix($className, $classSuffix = null)
{
if ($classSuffix === '*') {
return true;
}
$classSuffixPos = strrpos($className, $classSuffix);
return $classSuffixPos === (strlen($className) - strlen($classSuffix));
}

/**
* Returns true if the specified method prefix is in the method name.
*
* @param string $methodName
* @param string $methodPrefix
*
* @return boolean
*/
private function checkMethodHasValidPrefix($methodName, $methodPrefix = [])
{
if (in_array('*', $methodPrefix)) {
return true;
}
foreach ($methodPrefix as $prefix) {
if (strpos($methodName, $prefix) === 0) {
return true;
}
}
}

/**
* Returns true if the specified string is in the underscore caps format.
*
Expand All @@ -67,6 +123,5 @@ private function isUnderscoreName($string)
$validName = false;
}
return $validName;

}
}
23 changes: 23 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,27 @@
'ignoreNamespace' => [
'app/database',
],

/*
|--------------------------------------------------------------------------
| Allow snake case for method names of classes
|--------------------------------------------------------------------------
| Below you can configure that which methods of which classes should have snake cased
| names. If you want to define this rule for all methods of a class with a suffixed name,
| then set the 'methodPrefix' array value to `*`. If you want a particular prefixed
| method to be snake cased in all classes, then set the 'classSuffix' value to `*`.
|
| Ex: 'allowSnakeCaseMethodName' => [
| [
| 'classSuffix' => 'Test',
| 'methodPrefix' => ['test'],
| ]
|
*/
'allowSnakeCaseMethodName' => [
[
'classSuffix' => 'Test',
'methodPrefix' => ['test'],
]
]
];

0 comments on commit 6bf8113

Please sign in to comment.