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

Fix compatibility with Laravel 10.30 — lowercase the $passthru array values #2661

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

Treggats
Copy link
Contributor

@Treggats Treggats commented Nov 1, 2023

fixes #2660
fixes #2656

As explained in the issue, Laravel updated the way Builder methods are called. The list of methods in the $passthru property now should be lowercase.

Currently they are not, and this causes the methods to not be called, but passed on and thus returning the Builder instance.

This PR makes them all lowercase, which will fix this issue. I have tested this in our application and it works.

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Hello @Treggats. Thank you for identifying and fixing this bug.
Could you add a test for each method to ensure we don't have regression?

src/Eloquent/Builder.php Outdated Show resolved Hide resolved
'insertGetId',
'insertOrIgnore',
'insertUsing',
'insertgetid',
Copy link
Member

@GromNaN GromNaN Nov 2, 2023

Choose a reason for hiding this comment

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

If #2659 is merged, this method will never be called. Would it be better to remove it? Possibly by leaving a comment.
Or maybe discard #2659 and keep this PR as a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What you said in the comment in #2659 makes sense, the latter part.
Personally I like to be specific/remove magic, but that's the way Laravel works and we need to align with that.

In short, close #2659 in favour of this one.

It seems to be fixed by #2661. Both solutions will work, yours removes a layer of magics but it is more fragile if the implementation changes in Laravel.

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

Hello @Treggats. Thank you for identifying and fixing this bug. Could you add a test for each method to ensure we don't have regression?

@GromNaN thanks for the review, I will add tests and address the review comments.

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

a little sidenote, locally I made the following change. I won't commit it as its a personal preference though it doesn't seem to impact the current codebase.

diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 36cc870..23bc44a 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -34,5 +34,7 @@
         <exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
         <exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
         <exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
+
+        <exclude name="Generic.Formatting.MultipleStatementAlignment" />
     </rule>
 </ruleset>

@GromNaN
Copy link
Member

GromNaN commented Nov 2, 2023

You can commit this change. It annoys me too.
It's excluded from mongodb-php-library.

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

You can commit this change. It annoys me too. It's excluded from mongodb-php-library.

I will follow suit then.

@hans-thomas
Copy link
Contributor

@GromNaN the #2656 issue is related to this PR too. After merging, don't forget to close it.

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

@GromNaN the #2656 issue is related to this PR too. After merging, don't forget to close it.

updated the description so that it closes after merging of this PR

@Treggats Treggats requested a review from GromNaN November 2, 2023 13:30
'max',
'min',
'pluck',
'pull',
'push',
'raw',
'sum',
'toSql',
'tomql',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced as it is not supported

self::assertNotInstanceOf(expected: $className, actual: $builder->{$method}(...$parameters));
}

public static function provideFunctionNames(): Generator
Copy link
Contributor Author

Choose a reason for hiding this comment

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

provide supported methods, which should not return a Builder instance.

$builder = User::query()->newQuery();
assert($builder instanceof Builder);

$this->expectException($exceptionClass);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

verify that certain methods are not supported, checking both the exception type and its message

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

Currently the MassPrunableTest::testPruneWithQuery test is failing locally, I'm trying to figure out why that is.

The $this->assertEquals(1, User::count()); assertion fails, two models were not pruned. Thus the count() query returns 3

@GromNaN
Copy link
Member

GromNaN commented Nov 2, 2023

While testing, I just realized that it breaks compatibility with illuminate/database < 10.30.0.

One strategy for maintaining compatibility would be to modify this list in __construct, adding only the specific methods, in lowercase and non-lowercase versions (leaving a comment that non-lowercase versions are kept for compatibility with Laravel < 10.30).

];

yield 'dd' => [
'dd',
Copy link
Member

Choose a reason for hiding this comment

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

We can create a separate issue to support this, once this PR get merged.

@GromNaN

This comment was marked as outdated.

@GromNaN GromNaN force-pushed the sync-casing-with-base-model branch 4 times, most recently from f80b7ac to abcba92 Compare November 2, 2023 16:16
@GromNaN GromNaN changed the base branch from 4.1 to 4.0 November 2, 2023 16:17
@codecov-commenter
Copy link

codecov-commenter commented Nov 2, 2023

Codecov Report

Merging #2661 (a14a822) into 4.0 (25bd203) will increase coverage by 0.20%.
The diff coverage is n/a.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@             Coverage Diff              @@
##                4.0    #2661      +/-   ##
============================================
+ Coverage     90.99%   91.20%   +0.20%     
  Complexity      757      757              
============================================
  Files            35       35              
  Lines          1933     1933              
============================================
+ Hits           1759     1763       +4     
+ Misses          174      170       -4     
Files Coverage Δ
src/Eloquent/Builder.php 89.33% <ø> (+2.66%) ⬆️

... and 1 file with indirect coverage changes

'max',
'min',
'pluck',
'pull',
'push',
'raw',
'sum',
'toSql',
'tomql',
// Kept for compatibility with Laravel < 10.3
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And here I was trying to conditionally set the method names casing 😆

Copy link
Member

Choose a reason for hiding this comment

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

Simple and verbose solutions are often good enough.

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Thank you @Treggats for the time you've devoted to correcting the breaking changes coming from Laravel.

@Treggats
Copy link
Contributor Author

Treggats commented Nov 2, 2023

Thank you @Treggats for the time you've devoted to correcting the breaking changes coming from Laravel.

@GromNaN You're welcome, we had to pin our laravel/framework version because of it. So we needed a fix for this.

@GromNaN GromNaN merged commit bd6f1c3 into mongodb:4.0 Nov 2, 2023
13 checks passed
@Treggats Treggats deleted the sync-casing-with-base-model branch November 2, 2023 16:23
@GromNaN GromNaN changed the title lowercase the $passthru array values Fix compatibility with Laravel 10.30 — lowercase the $passthru array values Nov 2, 2023
@GromNaN
Copy link
Member

GromNaN commented Nov 2, 2023

I'm going to make a release tomorrow.

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.

model id property returns the builder and not the id Call to undefined method compileWhereSub()
4 participants