Skip to content

Commit

Permalink
refactored ControllersFileNameSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
venkatrs committed Aug 8, 2010
1 parent 77629ec commit 5c1e6e0
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions Cake/Sniffs/Files/ControllersFileNameSniff.php
Expand Up @@ -8,7 +8,7 @@
*
*/
class Cake_Sniffs_Files_ControllersFileNameSniff implements PHP_CodeSniffer_Sniff

{

/**
Expand Down Expand Up @@ -37,26 +37,22 @@ public function register()
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$path = $phpcsFile->getFileName();
if(!preg_match("/(controllers)/i", $path)) return;

$tokens = $phpcsFile->getTokens();
$classname_token = $phpcsFile->findNext(T_STRING, $stackPtr);
$classname = $tokens[$classname_token]['content'];
$classname = $tokens[$phpcsFile->findNext(T_STRING, $stackPtr)]['content'];

if(preg_match("/(components)/i", $path)) {
$final_classname = $this->classname_without_type($classname, "Component");
$msg_on_error = "Cake convention expects the component class name to end with 'Component'";
} else {
$final_classname = $this->classname_with_type($classname, "Controller");
$msg_on_error = "Cake convention expects the controller class name to end with 'Controller'";
}

if(preg_match("/(controllers)/i", $path)) {
if(preg_match("/(components)/i", $path)) {
$final_classname = $this->classname_without_type($classname, "Component");
if(is_null($final_classname)) {
$error = "Cake convention expects the component class name to end with 'Component'";
$phpcsFile->addError($error, $stackPtr);
return;
}
} else {
$final_classname = $this->classname_with_type($classname, "Controller");
if(is_null($final_classname)) {
$error = "Cake convention expects the controller class name to end with 'Controller'";
$phpcsFile->addError($error, $stackPtr);
return;
}
}
if(is_null($final_classname)) {
$phpcsFile->addError($msg_on_error, $stackPtr);
return;
}

$expected_file_name = preg_replace('/([A-Z])/', '_${1}', $final_classname);
Expand Down Expand Up @@ -85,7 +81,6 @@ private function classname_with_type($class_name, $type_name) {
return NULL;
}


}//end class

?>

0 comments on commit 5c1e6e0

Please sign in to comment.