-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
IfStatementAssignment thrown incorrectly #661
Comments
I tried to get a minimum code chunk to reproduce the bug and get it with: function bar()
{
if (true) {
array_filter(function ($foo) {
$bar = $foo;
echo $bar;
});
}
} Minimum tokens needed seem to be: a statement, a function/method call with a closure parameter, an assignation inside the closure. |
Is there any estimation on when 2.7.1 (fixes this) could be released? |
We just started to work on it: #668 |
…s-in-closures Fix #661 Exclude FunctionPostfix from IfStatementAssignment
Thanks for your help @leewillis77 it will be fixed in the next release, you can test it with the |
While the test cases posted here seem no longer to throw the error, I'm still seeing this error generated with my actual code. I'm going to see if I can produce an alternative test case, would you prefer to re-open this issue, or would you prefer me to open a fresh one? |
Examples below: Does NOT throw the error<?php
function bar($ids)
{
if ('CREATED' === 'CREATED') {
$ids->each(function () {
$allocation = find(1);
$allocation->status = 'paid';
$allocation->potato = 'round';
});
return true;
}
} Throws an error, but should not<?php
function bar($ids, $allocation)
{
if ('CREATED' === 'CREATED') {
$ids->each(function () use ($allocation) {
$allocation->status = 'paid';
$allocation->potato = 'round';
});
return true;
}
} Cut-down version of real code that throws error, but should not<?php
namespace App\Services;
use App\Models\Allocation;
class PayoutService
{
public function payoutAllocations($allocationIds)
{
if ('CREATED' === 'CREATED') {
$allocationIds->each(function ($allocationId) use ($payout) {
$allocation = Allocation::find($allocationId);
$allocation->status = 'paid';
});
}
}
} |
I found the actual problem. I will fix it. |
…s-in-closures Fix #661 Scan only the statement inner expression (first one)
@leewillis77 with thanks to @kylekatarnls there is a new fix on master. Can you verify it? |
Perfect - that no longer throws those errors on my test code, or the real code that triggered this initially. Thanks! |
Current Behavior
Sample code below throws two IfStatementAssignment errors.
The errors reported are:
test.php:3 Avoid assigning values to variables in if clauses and the like (line '13', column '7').
test.php:3 Avoid assigning values to variables in if clauses and the like (line '13', column '8').
Accounting for the incorrect line/column numbers (see #655), this is complaining about the two assignments within the closure.
Expected Behavior
No IfStatementAssignment errors should be thrown as is the case if the wrapping function is removed, e.g. the code below throws no IfStatementAssignment errors.
Steps To Reproduce:
See examples above.
Checks before submitting
The text was updated successfully, but these errors were encountered: