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

Conversation

michaeltintiuc
Copy link
Contributor

Description

As per the PHP documentation

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

Usage

To break out of 2 nested loops:

@break(2) compiles to break 2;

Compatibility

  • Previous expressions like ($user->id == 2) are supported
  • Checks for formatting with varying white-space: @continue( 2 ) compiles to continue 2;
  • Check the minimum amount of structures to break out of: 1

As per the [PHP documentation](http://php.net/manual/en/control-structures.break.php)
>break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.
Minimum count of structures to break out of is 1
Allow for values greater than 9
@michaeltintiuc
Copy link
Contributor Author

Implementing a similar feature in versions 5.2+ is trivial, would be my pleasure to do so.

@GrahamCampbell GrahamCampbell changed the title Support numeric arguments in break and continue [5.4] Support numeric arguments in break and continue Jan 27, 2017
@michaeltintiuc
Copy link
Contributor Author

Just to make it a bit more clear:

@foreach ($users as $user)
    @foreach($user->posts as $post)
        @if ($post->id == 1)
            @php
                break 2;
            @endphp
        @endif
    @endforeach
@endforeach

Now becomes:

@foreach ($users as $user)
    @foreach($user->posts as $post)
        @if ($post->id == 1)
            @break(2)
        @endif
    @endforeach
@endforeach

I could also write some tests if there is a need for such.

@taylorotwell
Copy link
Member

Can you provide tests?

@michaeltintiuc
Copy link
Contributor Author

@taylorotwell Will do. Would you like a test for each case? For example:

  • @break(2)
  • @break( 2)
  • @break( 2 )
  • @break(2 )

Testing for a proper argument passed as well as white-spaced and a faulty (less than 1) ones
Arguments that are less than zero are now caught by the regexp
Testing for a proper argument passed as well as white-spaced and a faulty (less than 1) ones
@michaeltintiuc
Copy link
Contributor Author

michaeltintiuc commented Jan 30, 2017

@taylorotwell Please, have a look and let me know if there's anything I might've missed.

@taylorotwell taylorotwell merged commit b375161 into laravel:5.4 Jan 31, 2017
@michaeltintiuc michaeltintiuc deleted the michaeltintiuc-patch-1 branch July 14, 2022 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants