/** * Will iterate tokens looking for comments and if found will determine the regex * to test the comment against. * * @param Testable $testable The testable object * @return void */ public function apply($testable, array $config = array()) { $tokens = $testable->tokens(); $comments = $testable->findAll(array(T_COMMENT)); foreach ($comments as $tokenId) { $token = $tokens[$tokenId]; $parentId = $tokens[$tokenId]['parent']; if ($parentId === -1 || $tokens[$parentId]['id'] !== T_FUNCTION) { $this->addViolation(array('message' => 'Inline comments should never appear.', 'line' => $token['line'])); } elseif (preg_match('/^test/', Parser::label($parentId, $tokens)) === 0) { $this->addViolation(array('message' => 'Inline comments should only appear in testing methods.', 'line' => $token['line'])); } } }
/** * Will iterate the tokens looking for functions validating they have the * correct camelBack naming style. * * @param Testable $testable The testable object * @return void */ public function apply($testable, array $config = array()) { $tokens = $testable->tokens(); $filtered = $testable->findAll(array(T_FUNCTION)); foreach ($filtered as $key) { $token = $tokens[$key]; $label = Parser::label($key, $tokens); $modifiers = Parser::modifiers($key, $tokens); $isClosure = Parser::closure($key, $tokens); if (in_array($label, $this->_magicMethods)) { continue; } if ($testable->findNext(array(T_PROTECTED), $modifiers) !== false) { $label = preg_replace('/^_/', '', $label); } if (!$isClosure && $label !== Inflector::camelize($label, false)) { $this->addViolation(array('message' => 'Function "' . $label . '" is not in camelBack style', 'line' => $tokens[$tokens[$key]['parent']]['line'])); } } }
/** * Will iterate the tokens looking for protected methods and variables, once * found it will validate the name of it's parent starts with an underscore. * * @param Testable $testable The testable object * @return void */ public function apply($testable, array $config = array()) { $message = 'Protected method {:name} must start with an underscore'; $tokens = $testable->tokens(); $filtered = $testable->findAll(array(T_PROTECTED)); foreach ($filtered as $tokenId) { $token = $tokens[$tokenId]; $parent = $testable->findNext(array(T_FUNCTION, T_VARIABLE), $tokenId); $parentLabel = Parser::label($parent, $tokens); if (substr($parentLabel, 0, 1) !== '_') { $classTokenId = $testable->findNext(array(T_STRING), $token['parent']); $classname = $tokens[$classTokenId]['content']; $params = array('message' => String::insert($message, array('name' => $parentLabel)), 'line' => $token['line']); if ($this->_strictMode($classname)) { $this->addViolation($params); } else { $this->addWarning($params); } } } }
public function testAnonymousClass() { $code = <<<EOD class { \treturn Parser::tokenize(); }; EOD; $tokenized = Parser::tokenize($code); $tokens = $tokenized['tokens']; $class = $tokens[0]; $this->assertIdentical(T_CLASS, $class['id']); $this->assertIdentical(null, Parser::label(0, $tokens)); }