Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Fix fail tests with eval'd
Browse files Browse the repository at this point in the history
  • Loading branch information
blanchonvincent committed Oct 28, 2013
1 parent 59c3388 commit 8c351a7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 40 deletions.
26 changes: 22 additions & 4 deletions library/Zend/Code/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,19 @@ public function getContents($includeDocBlock = true)
);
}

$startLine = $this->getStartLine();
$endLine = $this->getEndLine();

// eval'd protect
if (preg_match('#\((\d+)\) : eval\(\)\'d code$#', $fileName, $matches)) {
$fileName = preg_replace('#\(\d+\) : eval\(\)\'d code$#', '', $fileName);
$startLine = $endLine = $matches[1];
}

$lines = array_slice(
file($fileName, FILE_IGNORE_NEW_LINES),
$this->getStartLine() - 1,
($this->getEndLine() - ($this->getStartLine() - 1)),
$startLine - 1,
($endLine - ($startLine - 1)),
true
);

Expand Down Expand Up @@ -147,10 +156,19 @@ public function getBody()
);
}

$startLine = $this->getStartLine();
$endLine = $this->getEndLine();

// eval'd protect
if (preg_match('#\((\d+)\) : eval\(\)\'d code$#', $fileName, $matches)) {
$fileName = preg_replace('#\(\d+\) : eval\(\)\'d code$#', '', $fileName);
$startLine = $endLine = $matches[1];
}

$lines = array_slice(
file($fileName, FILE_IGNORE_NEW_LINES),
$this->getStartLine() - 1,
($this->getEndLine() - ($this->getStartLine() - 1)),
$startLine - 1,
($endLine - ($startLine - 1)),
true
);

Expand Down
3 changes: 2 additions & 1 deletion library/Zend/Code/Reflection/MethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public function getBody()
);

$functionLine = implode("\n", $lines);
preg_match('#[(public|protected|private|abstract|final|static)\s*]+function\s+' . $this->getName() . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)}#s', $functionLine, $matches);
$name = preg_quote($this->getName());
preg_match('#[(public|protected|private|abstract|final|static)\s*]*function\s+' . $name . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)}#s', $functionLine, $matches);

if (!isset($matches[1])) {
return false;
Expand Down
12 changes: 4 additions & 8 deletions tests/ZendTest/Code/Reflection/FunctionReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public function testFunctionBodyReturn()
$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function12');
$body = $function->getBody();
$this->assertEquals("", trim($body));

$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function13');
$body = $function->getBody();
$this->assertEquals('return "function13";', trim($body));
}

public function testFunctionClosureBodyReturn()
Expand Down Expand Up @@ -190,10 +186,6 @@ public function testFunctionContentsReturnWithoutDocBlock()
$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function12');
$content = $function->getContents(false);
$this->assertEquals("function function12() {}", trim($content));

$function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function13');
$content = $function->getContents(false);
$this->assertEquals('function function13() { return "function13"; }', trim($content));
}

public function testFunctionClosureContentsReturnWithoutDocBlock()
Expand All @@ -207,6 +199,10 @@ public function testFunctionClosureContentsReturnWithoutDocBlock()
$function = new FunctionReflection($function9);
$content = $function->getContents(false);
$this->assertEquals("function() {}", trim($content));

$function = new FunctionReflection($function10);
$content = $function->getContents(false);
$this->assertEquals("function() { return 'function10'; }", trim($content));
}

public function testFunctionContentsReturnWithDocBlock()
Expand Down
64 changes: 40 additions & 24 deletions tests/ZendTest/Code/Reflection/MethodReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testGetBodyReturnsCorrectBody()

$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doSomethingAgain');
$body = $reflectionMethod->getBody();
$this->assertEquals(trim($body), "\$closure = function(\$foo) { return \$foo; };\n return 'doSomethingAgain';");
$this->assertEquals(trim($body), "\$closure = function(\$foo) { return \$foo; };\n\n return 'doSomethingAgain';");

$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doStaticSomething');
$body = $reflectionMethod->getBody();
Expand Down Expand Up @@ -109,23 +109,27 @@ public function testInternalMethodContentsReturn()

public function testMethodContentsReturnWithoutDocBlock()
{
$contents =
' public function doSomething() {' . "\n"
. ' return \'doSomething\';' . "\n"
. ' }';
$contents = <<<CONTENTS
public function doSomething()
{
return 'doSomething';
}
CONTENTS;
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doSomething');
$this->assertEquals($contents, $reflectionMethod->getContents(false));

$contents = ' public function doSomethingElse($one, $two = 2, $three = \'three\') { return \'doSomethingElse\'; }';
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doSomethingElse');
$this->assertEquals($contents, $reflectionMethod->getContents(false));

$contents =
' public function doSomethingAgain()' . "\n"
. ' {' . "\n"
. ' $closure = function($foo) { return $foo; };' . "\n"
. ' return \'doSomethingAgain\';' . "\n"
. ' }';
$contents = <<<'CONTENTS'
public function doSomethingAgain()
{
$closure = function($foo) { return $foo; };
return 'doSomethingAgain';
}
CONTENTS;
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doSomethingAgain');
$this->assertEquals($contents, $reflectionMethod->getContents(false));

Expand All @@ -140,27 +144,39 @@ public function testMethodContentsReturnWithoutDocBlock()
$contents = ' public function inline3() { return \'inline3\'; }';
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'inline3');
$this->assertEquals($contents, $reflectionMethod->getContents(false));

$contents = <<<'CONTENTS'
public function visibility()
{
return 'visibility';
}
CONTENTS;
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'visibility');
$this->assertEquals($contents, $reflectionMethod->getContents(false));
}

public function testFunctionContentsReturnWithDocBlock()
{
$contents =
'/**' . "\n"
. ' * Doc block doSomething' . "\n"
. ' * @return string' . "\n"
. ' */' . "\n"
. ' public function doSomething() {' . "\n"
. ' return \'doSomething\';' . "\n"
. ' }';
$contents = <<<'CONTENTS'
/**
* Doc block doSomething
* @return string
*/
public function doSomething()
{
return 'doSomething';
}
CONTENTS;
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'doSomething');
$this->assertEquals($contents, $reflectionMethod->getContents(true));
$this->assertEquals($contents, $reflectionMethod->getContents());

$contents =
'/**' . "\n"
. ' * Awesome doc block' . "\n"
. ' */' . "\n"
. ' public function emptyFunction() {}';
$contents = <<<'CONTENTS'
/**
* Awesome doc block
*/
public function emptyFunction() {}
CONTENTS;
$reflectionMethod = new MethodReflection('ZendTest\Code\Reflection\TestAsset\TestSampleClass11', 'emptyFunction');
$this->assertEquals($contents, $reflectionMethod->getContents(true));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ZendTest/Code/Reflection/TestAsset/closures.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
*/
$function9 = function() {};

eval('$function10 = function() { return \'function10\'; };');
eval("\$function10 = function() { return 'function10'; };");
2 changes: 0 additions & 2 deletions tests/ZendTest/Code/Reflection/TestAsset/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,3 @@ function function8() { return 'function8'; } function function9() { return 'func
function function10() { $closure = function() { return 'function10'; }; return $closure(); } function function11() { return 'function11'; }

function function12() {}

eval('function function13() { return "function13"; }');

0 comments on commit 8c351a7

Please sign in to comment.