Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 29, 2016
1 parent a0cd0ae commit a8df2de
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ protected function isUnique($key, $message)
}

/**
* Determine if messages exist for a given key(s).
* Determine if messages exist for all of the given keys.
*
* @param array|string $key
* @return bool
*/
public function has($key = null)
{
foreach ((array) $key as $k) {
if ($this->first($k) === '') {
$keys = is_array($key) ? $key : func_get_args();

foreach ($keys as $key) {
if ($this->first($key) === '') {
return false;
}
}
Expand Down

9 comments on commit a8df2de

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not working anymore. $errors->has() always return true when $keys is empty.

@JoostK
Copy link
Contributor

@JoostK JoostK commented on a8df2de Jul 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeweb11 Looking at the code, hasn't that always been the behaviour?

I'm not saying the current behaviour is correct, but just wondering how you think it changed.

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoostK The problem is that if I use errors- $>has () without passing any parameters, the behavior as it was before, should be returns true or false if there are errors, which is no more, even if there is no error, this method still returns true.

@crynobone
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't you be using $errors->any()?

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me without problems, now I started to use $ erros->count (), but the problem is that it commits not maintained compatibility in code, many other people who used $ errors->has() had the same problem.

@GrahamCampbell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour looks correct. If you perform an assertion on the elements of something empty, the convention is normally that it passes.

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that in any case this is wrong, since the parameter $keys is optional. It is expected that the call method $erros->has() when there is no error it returns false.

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct me if I'm wrong:

public function has($key = null)
{
        $keys = is_array($key) ? $key : func_get_args();
        foreach ($keys as $key) {
            if ($this->first($key) === '') {
                return false;
            }
        }
        return true;
}

if $keys is an empty array, the method will immediately return true.

func_get_args() in this case returns an empty array, then $errors->has() returns true.

@feralc
Copy link

@feralc feralc commented on a8df2de Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this new behavior is correct, then before that alteration, he was wrong, because this method always returned false when there were no errors and when no parameter was passed.

Please sign in to comment.