/** * 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 all the tokens looking for tokens in inspectableTokens * The token needs an access modifier if it is a T_FUNCTION or T_VARIABLE * and is in the first level of T_CLASS. This prevents functions and variables * inside methods and outside classes to register violations. * * @param Testable $testable The testable object * @return void */ public function apply($testable, array $config = array()) { $message = '{:name} has no declared visibility.'; $tokens = $testable->tokens(); $classes = $testable->findAll(array(T_CLASS)); $filtered = $testable->findAll($this->inspectableTokens); foreach ($classes as $classId) { $children = $tokens[$classId]['children']; foreach ($children as $member) { if (!in_array($member, $filtered)) { continue; } $modifiers = Parser::modifiers($member, $tokens); $visibility = $testable->findNext($this->findTokens, $modifiers); if ($visibility === false) { $token = $tokens[$member]; $this->addViolation(array('modifiers' => $modifiers, 'message' => String::insert($message, $token), 'line' => $token['line'])); } } } }
public function testFindNextContentWithArray() { $code = <<<EOD class foo { \tprivate function bar() { \t} } EOD; $testable = $this->_testable(array('source' => $code)); $tokens = $testable->tokens(); $contentToFind = array('public', 'protected', 'private'); $id = $testable->findNext(array(T_FUNCTION), 0); // function bar $modifiers = Parser::modifiers($id, $tokens); $visibility = $testable->findNextContent($contentToFind, $modifiers); $this->assertIdentical('private', $tokens[$visibility]['content']); }
public function testNoModifiers() { $code = <<<EOD class Inflector { \tpublic static \$foo; \tfunction rules() { \t\tstatic::\${\$var} = null; \t} } EOD; $tokenized = Parser::tokenize($code); $tokens = $tokenized['tokens']; $modifiers = Parser::modifiers(13, $tokens); $this->assertIdentical(T_FUNCTION, $tokens[13]['id']); $this->assertIdentical(array(), $modifiers); }