Skip to content

Commit

Permalink
Blacklisting do..while constructs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuswilms committed Aug 25, 2010
1 parent c3b57a2 commit bff0622
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Rule/ControlStructuresBracesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace spriebsch\PHPca\Rule;

use spriebsch\PHPca\Token;
use spriebsch\PHPca\Pattern\Token as PatternToken;
use spriebsch\PHPca\Pattern\Pattern;
use spriebsch\PHPca\Finder;

/**
* Ensures that the control structures have a space before the parenthesis
* and a space between the parenthesis and the brace.
*/
class ControlStructuresBracesRule extends Rule
{
protected $blacklist = array();

protected $controlTokens = array(
T_IF,
Expand All @@ -26,12 +30,33 @@ class ControlStructuresBracesRule extends Rule
*/
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 ($this->file->seekTokenId(T_OPEN_CURLY)) {
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()) {
Expand Down

0 comments on commit bff0622

Please sign in to comment.