Skip to content

Commit

Permalink
OptimizeControlStructures: added support for conditional class/functi…
Browse files Browse the repository at this point in the history
…on declarations
  • Loading branch information
JoshyPHP committed Jan 13, 2015
1 parent ad3a9c7 commit fca63a7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
12 changes: 11 additions & 1 deletion bin/source-optimizer
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,16 @@ class OptimizeControlStructures extends Pass
{
return($this->stream->current()==='{'||$this->stream->isAny([\T_CURLY_OPEN,\T_DOLLAR_OPEN_CURLY_BRACES]));
}
protected function isFollowedByDeclaraction()
{
$keywords=[\T_ABSTRACT,\T_CLASS,\T_FINAL,\T_FUNCTION,\T_INTERFACE,\T_TRAIT];
$offset=$this->stream->key();
$this->stream->next();
$this->stream->skipNoise();
$isFollowedByFunction=($this->stream->valid()&&$this->stream->isAny($keywords));
$this->stream->seek($offset);
return$isFollowedByFunction;
}
protected function optimizeElse(array$structure)
{
$this->stream->seek($structure['offsetLeftBrace']);
Expand Down Expand Up @@ -516,7 +526,7 @@ class OptimizeControlStructures extends Pass
else
$this->skipParenthesizedExpression();
$this->stream->skipNoise();
if($this->stream->current()!=='{')
if($this->stream->current()!=='{'||$this->isFollowedByDeclaraction())
return \false;
$braces=0;
$structure['offsetLeftBrace']=$this->stream->key();
Expand Down
20 changes: 19 additions & 1 deletion src/Passes/OptimizeControlStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ protected function isCurlyOpen()
return ($this->stream->current() === '{' || $this->stream->isAny([T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES]));
}

/**
* Test whether current token is followed by a function or class-related declaration
*
* @return bool
*/
protected function isFollowedByDeclaraction()
{
$keywords = [T_ABSTRACT, T_CLASS, T_FINAL, T_FUNCTION, T_INTERFACE, T_TRAIT];

$offset = $this->stream->key();
$this->stream->next();
$this->stream->skipNoise();
$isFollowedByFunction = ($this->stream->valid() && $this->stream->isAny($keywords));
$this->stream->seek($offset);

return $isFollowedByFunction;
}

/**
* Optimize given T_ELSE structure
*
Expand Down Expand Up @@ -174,7 +192,7 @@ protected function parseControlStructure()
}
$this->stream->skipNoise();

if ($this->stream->current() !== '{')
if ($this->stream->current() !== '{' || $this->isFollowedByDeclaraction())
{
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Passes/OptimizeControlStructures/037.optimized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
if (0)
{
function foo()
{
}
}
7 changes: 7 additions & 0 deletions tests/Passes/OptimizeControlStructures/037.original.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
if (0)
{
function foo()
{
}
}
17 changes: 17 additions & 0 deletions tests/Passes/OptimizeControlStructures/038.optimized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
if (0)
{
final class Foo {}
}
if (0)
{
abstract class Bar {}
}
if (0)
{
interface FooInterface {}
}
if (0)
{
trait FooTrait {}
}
17 changes: 17 additions & 0 deletions tests/Passes/OptimizeControlStructures/038.original.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
if (0)
{
final class Foo {}
}
if (0)
{
abstract class Bar {}
}
if (0)
{
interface FooInterface {}
}
if (0)
{
trait FooTrait {}
}

0 comments on commit fca63a7

Please sign in to comment.