Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Support numeric arguments in break and continue #17603

Merged
merged 9 commits into from
Jan 31, 2017
16 changes: 14 additions & 2 deletions src/Illuminate/View/Compilers/Concerns/CompilesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ protected function compileForeach($expression)
*/
protected function compileBreak($expression)
{
return $expression ? "<?php if{$expression} break; ?>" : '<?php break; ?>';
if ($expression) {
preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);

return $matches ? '<?php break '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} break; ?>";
}

return '<?php break; ?>';
}

/**
Expand All @@ -107,7 +113,13 @@ protected function compileBreak($expression)
*/
protected function compileContinue($expression)
{
return $expression ? "<?php if{$expression} continue; ?>" : '<?php continue; ?>';
if ($expression) {
preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);

return $matches ? '<?php continue '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} continue; ?>";
}

return '<?php continue; ?>';
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/View/Blade/BladeBreakStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,48 @@ public function testBreakStatementsWithExpressionAreCompiled()
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testBreakStatementsWithArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@break(2)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php break 2; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testBreakStatementsWithSpacedArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@break( 2 )
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php break 2; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testBreakStatementsWithFaultyArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@break(-2)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php break 1; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
Expand Down
42 changes: 42 additions & 0 deletions tests/View/Blade/BladeContinueStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,48 @@ public function testContinueStatementsWithExpressionAreCompiled()
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testContinueStatementsWithArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@continue(2)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php continue 2; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testContinueStatementsWithSpacedArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@continue( 2 )
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php continue 2; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testContinueStatementsWithFaultyArgumentAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@continue(-2)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php continue 1; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
Expand Down