Skip to content

[9.x] Stringable exactly not match with other Stringable variable#41846

Merged
taylorotwell merged 5 commits into
laravel:9.xfrom
medeiroz:9.x
Apr 6, 2022
Merged

[9.x] Stringable exactly not match with other Stringable variable#41846
taylorotwell merged 5 commits into
laravel:9.xfrom
medeiroz:9.x

Conversation

@medeiroz
Copy link
Copy Markdown
Contributor

@medeiroz medeiroz commented Apr 6, 2022

  • Laravel Version: 9.5.1
  • PHP Version: 8.1
  • Database Driver & Version: N/A

Description:

When need compare two Stringable variables with exactly method. Then false is returned

Steps To Reproduce:

<?php

$first = \Str::of('name');
$second = \Str::of('name');
$first->exactly($second);
//  => false

// true is expected but false is returned

image

Solution

  • Cast argument to string when argument is Stringable

@SjorsO
Copy link
Copy Markdown
Contributor

SjorsO commented Apr 6, 2022

Doesn't make a lot of sense to have a method called exactly that does a loose comparison.

Might be a better idea to do this instead:

if ($value instanceof Stringable) {
    $value = $value->toString();
}

return $this->value === $value;

@driesvints driesvints closed this Apr 6, 2022
@driesvints driesvints reopened this Apr 6, 2022
@imanghafoori1
Copy link
Copy Markdown
Contributor

if (is_object($value) && method_exists('__toString', $value)) {
    $value = (string) $value;
}

So that there is no loose comparison will happen.

@dennisprudlo
Copy link
Copy Markdown
Contributor

dennisprudlo commented Apr 6, 2022

@imanghafoori1 I'm not sure every object that has a __toString() should be a valid parameter.

$model = new \App\Models\SomeModel;
str('[]')->exactly($model); // true

The type in the docblock says only string can be passed, so maybe just add Stringable (the class not the global interface) to the docblock and check like @SjorsO did.

Copy link
Copy Markdown
Collaborator

@GrahamCampbell GrahamCampbell left a comment

Choose a reason for hiding this comment

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

👎 You are incorrectly calling this function if you do not pass a string.

@GrahamCampbell
Copy link
Copy Markdown
Collaborator

If you want this function to support stringable objects, then we would have to widen its param type.

@GrahamCampbell GrahamCampbell changed the title Stringable exactly not match with other Stringable variable [9.x] Stringable exactly not match with other Stringable variable Apr 6, 2022
@driesvints
Copy link
Copy Markdown
Member

@GrahamCampbell that's what this PR tries to do 😅 I also misjudged this PR at first for wrong usage but I can see the use case here.

@medeiroz medeiroz requested a review from GrahamCampbell April 6, 2022 12:01
@pedroaff
Copy link
Copy Markdown

pedroaff commented Apr 6, 2022

The two itens has the exact values, so it should not return false.
It's makes sense when we need to compare two itens w/ the same value.

@taylorotwell taylorotwell merged commit 6ed23a5 into laravel:9.x Apr 6, 2022
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.

8 participants