Skip to content

Commit

Permalink
Added syntactic sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jan 11, 2015
1 parent 46ff963 commit 9eb2f4c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
22 changes: 8 additions & 14 deletions src/Passes/OptimizeControlStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function optimize(TokenStream $stream)
*/
protected function isControlStructure()
{
return in_array(
$this->stream->currentValue(),
[T_ELSE, T_ELSEIF, T_FOR, T_FOREACH, T_IF, T_WHILE],
true
);
return $this->stream->isAny([T_ELSE, T_ELSEIF, T_FOR, T_FOREACH, T_IF, T_WHILE]);
}

/**
Expand All @@ -75,7 +71,7 @@ protected function optimizeElse(array $structure)
$this->stream->seek($structure['offsetLeftBrace']);
$this->stream->next();
$this->stream->skipNoise();
if ($this->stream->currentValue() !== T_IF)
if (!$this->stream->is(T_IF))
{
return;
}
Expand Down Expand Up @@ -133,9 +129,9 @@ protected function optimizeStructures(array $structures)
protected function parseControlStructure()
{
$structure = [
'isElse' => ($this->stream->currentValue() === T_ELSE),
'isElseif' => ($this->stream->currentValue() === T_ELSEIF),
'isIf' => ($this->stream->currentValue() === T_IF),
'isElse' => $this->stream->is(T_ELSE),
'isElseif' => $this->stream->is(T_ELSEIF),
'isIf' => $this->stream->is(T_IF),
'offsetConstruct' => $this->stream->key(),
'offsetLeftBrace' => null,
'offsetRightBrace' => null,
Expand Down Expand Up @@ -181,8 +177,7 @@ protected function parseControlStructure()
}
elseif ($this->isControlStructure())
{
if ($this->stream->currentValue() !== T_ELSE
&& $this->stream->currentValue() !== T_ELSEIF)
if (!$this->stream->isAny([T_ELSE, T_ELSEIF]))
{
++$structure['statements'];
}
Expand Down Expand Up @@ -224,7 +219,7 @@ protected function markPreservedBraces($offset)
$this->stream->skipNoise();
if ($this->stream->valid())
{
$isFollowedByElse = in_array($this->stream->currentValue(), [T_ELSE, T_ELSEIF], true);
$isFollowedByElse = $this->stream->isAny([T_ELSE, T_ELSEIF]);
}
}

Expand Down Expand Up @@ -285,8 +280,7 @@ protected function removeWhitespaceBefore($offset)
}

$this->stream->seek($offset - 1);
if ($this->stream->currentValue() === T_WHITESPACE
&& $this->stream->canRemoveCurrentToken())
if ($this->stream->is(T_WHITESPACE) && $this->stream->canRemoveCurrentToken())
{
$this->stream->remove();
}
Expand Down
32 changes: 22 additions & 10 deletions src/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ public function canRemoveCurrentToken()
return (strpos($delimiters, $prevChar) !== false || strpos($delimiters, $nextChar) !== false);
}

/**
* Test whether current token is the given type of token
*
* @param integer $tokenValue
* @return bool
*/
public function is($tokenValue)
{
return ($this->tokens[$this->offset][0] === $tokenValue);
}

/**
* Test whether current token is any of the given types of token
*
* @param integer[] $tokenValues
* @return bool
*/
public function isAny(array $tokenValues)
{
return in_array($this->tokens[$this->offset][0], $tokenValues, true);
}

/**
* Test whether there's a token at given offset
*
Expand Down Expand Up @@ -168,16 +190,6 @@ public function currentText()
return (is_array($this->tokens[$this->offset])) ? $this->tokens[$this->offset][1] : $this->tokens[$this->offset];
}

/**
* Get current token's value
*
* @return integer|string Token's value if applicable, or the token's text otherwise
*/
public function currentValue()
{
return (is_array($this->tokens[$this->offset])) ? $this->tokens[$this->offset][0] : $this->tokens[$this->offset];
}

/**
* Test whether current token is noise (whitespace or comment)
*
Expand Down

0 comments on commit 9eb2f4c

Please sign in to comment.