Skip to content

Commit

Permalink
OptimizeControlStructures: always mark braces that need to be preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jan 12, 2015
1 parent a725e8d commit 0a04c87
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/Passes/OptimizeControlStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ protected function optimizeStructure(array $structure)
$this->optimizeStructures($structure['structures']);
}

if ($structure['isIf'] || $structure['isElseif'])
{
$this->markPreservedBraces($structure['offsetRightBrace']);
}

if ($structure['statements'] <= 1
&& isset($structure['offsetRightBrace'])
&& !isset($this->preservedBraces[$structure['offsetRightBrace']]))
Expand Down Expand Up @@ -237,11 +242,6 @@ protected function markPreservedBraces($offset)
*/
protected function removeBraces(array $structure)
{
if ($structure['isIf'] || $structure['isElseif'])
{
$this->markPreservedBraces($structure['offsetRightBrace']);
}

// Replace the opening brace with a semicolon if the control structure is empty, remove the
// brace if possible or replace it with whitespace otherwise (e.g. in "else foreach")
$this->stream->seek($structure['offsetLeftBrace']);
Expand Down
4 changes: 2 additions & 2 deletions src/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ public function offsetUnset($offset)
/**
* Return the current token
*
* @return array|string
* @return array|string|null
*/
public function current()
{
return $this->tokens[$this->offset];
return (isset($this->tokens[$this->offset])) ? $this->tokens[$this->offset] : null;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Passes/OptimizeControlStructures/033.optimized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
if (1)
{
if (0)
{
echo __LINE__, "\n";
echo __LINE__, "\n";
}
}
else
echo __LINE__, "\n";
13 changes: 13 additions & 0 deletions tests/Passes/OptimizeControlStructures/033.original.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
if (1)
{
if (0)
{
echo __LINE__, "\n";
echo __LINE__, "\n";
}
}
else
{
echo __LINE__, "\n";
}

0 comments on commit 0a04c87

Please sign in to comment.