Exemplo n.º 1
0
 /**
  * Performs the rule check
  *
  * @return void
  */
 protected function doCheck()
 {
     // exclude do ... while statements by putting them on the blacklist
     $pattern = new Pattern();
     $pattern->token(T_DO)->token(T_WHITESPACE)->token(T_OPEN_CURLY)->zeroOrMore(new PatternToken(T_ANY))->token(T_CLOSE_CURLY)->token(T_WHITESPACE)->token(T_WHILE);
     $this->blacklist = array();
     foreach (Finder::findPattern($this->file, $pattern) as $match) {
         if ($match[0]->getBlockLevel() == $match[sizeof($match) - 1]->getBlockLevel()) {
             $this->blacklist[] = $match[sizeof($match) - 1];
         }
     }
     $this->file->rewind();
     foreach ($this->controlTokens as $id) {
         while ($this->file->seekTokenId($id)) {
             $controlToken = $this->file->current();
             $name = $controlToken->getText();
             if (in_array($controlToken, $this->blacklist)) {
                 $this->file->seekToken($controlToken);
             } elseif ($this->file->seekTokenId(T_OPEN_CURLY)) {
                 $curlyToken = $this->file->current();
                 if (!$this->file->valid() || $controlToken->getLine() != $curlyToken->getLine()) {
                     if ($controlToken->getLine() == $curlyToken->getLine() - 1) {
                         $this->addViolation("Brace for `{$name}` statement on wrong line", $curlyToken);
                     } else {
                         $this->addViolation("No brace for `{$name}` statement", $controlToken);
                         $this->file->seekToken($controlToken);
                     }
                 }
             } else {
                 $this->addViolation("No brace for `{$name}` statement", $controlToken);
                 $this->file->seekToken($controlToken);
             }
             $this->file->next();
         }
         $this->file->rewind();
     }
 }
Exemplo n.º 2
0
 /**
  * Performs the rule check.
  *
  * @returns null
  */
 protected function doCheck()
 {
     // exclude do ... while statements by putting them on the blacklist
     $pattern = new Pattern();
     $pattern->token(T_DO)->token(T_WHITESPACE)->token(T_OPEN_CURLY)->zeroOrMore(new Token(T_ANY))->token(T_CLOSE_CURLY)->token(T_WHITESPACE)->token(T_WHILE);
     $this->blacklist = array();
     foreach (Finder::findPattern($this->file, $pattern) as $match) {
         if ($match[0]->getBlockLevel() == $match[sizeof($match) - 1]->getBlockLevel()) {
             $this->blacklist[] = $match[sizeof($match) - 1];
         }
     }
     $this->checkBraceLine(T_CLASS, 'class: curly brace on same line');
     $this->checkBraceLine(T_FUNCTION, 'function: curly brace on same line');
     $this->checkBraceLine(T_FOREACH, 'foreach: curly brace on different line', false);
     $this->checkBraceLine(T_FOR, 'for: curly brace on different line', false);
     $this->checkBraceLine(T_SWITCH, 'switch: curly brace on different line', false);
     $this->checkBraceLine(T_IF, 'if: curly brace on different line', false);
     $this->checkBraceLine(T_ELSE, 'else: curly brace on different line', false);
     $this->checkBraceLine(T_ELSEIF, 'elseif: curly brace on different line', false);
     $this->checkBraceLine(T_WHILE, 'while: curly brace on different line', false);
     $this->checkBraceLine(T_DO, 'do: curly brace on different line', false);
     // Namespaces have a dual syntax, either "namespace name;" or
     // "namespace name {}", so we need a different search pattern
     // to avoid wrong matches like "namespace ... class {".
     $pattern = new Pattern();
     $pattern->token(T_NAMESPACE)->token(T_WHITESPACE)->token(T_STRING)->token(T_WHITESPACE)->token(T_OPEN_CURLY);
     foreach (Finder::findPattern($this->file, $pattern) as $match) {
         $token = $match[0];
         $brace = $match[sizeof($match) - 1];
         $line = $token->getLine();
         $braceLine = $brace->getLine();
         // Generate error if namespace token and brace are on same line
         if ($line == $braceLine) {
             $this->addViolation('namespace: curly brace on same line', $token);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * @covers \spriebsch\PHPca\Pattern\Pattern::zeroOrMore
  */
 public function testZeroOrMoreMapsToRegExWithoutTrailingBlank()
 {
     $pattern = new Pattern();
     $pattern->zeroOrMore(new Token(T_OPEN_TAG));
     $this->assertEquals('(\\bT_OPEN_TAG\\b)*', $pattern->getRegEx());
 }
Exemplo n.º 4
0
 /**
  * Find all T_FUNCTION tokens in the file and make sure they have correct
  * line/column positions.
  *
  * @covers spriebsch\PHPca\Finder::findPattern
  */
 public function testBug0002()
 {
     $file = Tokenizer::tokenize('filename', file_get_contents(__DIR__ . '/_testdata/Finder/bug0002.php'));
     $pattern = new Pattern();
     $pattern->token(T_FUNCTION);
     $result = Finder::findPattern($file, $pattern);
     $this->assertEquals(5, sizeof($result));
     $this->assertEquals(1, sizeof($result[0]));
     $this->assertEquals('T_FUNCTION', $result[0][0]->getName());
     $this->assertEquals(8, $result[0][0]->getLine());
     $this->assertEquals(12, $result[0][0]->getColumn());
     $this->assertEquals(1, sizeof($result[1]));
     $this->assertEquals('T_FUNCTION', $result[1][0]->getName());
     $this->assertEquals(12, $result[1][0]->getLine());
     $this->assertEquals(19, $result[1][0]->getColumn());
     $this->assertEquals(1, sizeof($result[2]));
     $this->assertEquals('T_FUNCTION', $result[2][0]->getName());
     $this->assertEquals(16, $result[2][0]->getLine());
     $this->assertEquals(21, $result[2][0]->getColumn());
     $this->assertEquals(1, sizeof($result[3]));
     $this->assertEquals('T_FUNCTION', $result[3][0]->getName());
     $this->assertEquals(22, $result[3][0]->getLine());
     $this->assertEquals(1, $result[3][0]->getColumn());
     $this->assertEquals(1, sizeof($result[4]));
     $this->assertEquals('T_FUNCTION', $result[4][0]->getName());
     $this->assertEquals(26, $result[4][0]->getLine());
     $this->assertEquals(1, $result[4][0]->getColumn());
 }