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

Support calls to functions with variadic parameters #814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AJenbo
Copy link
Member

@AJenbo AJenbo commented Aug 24, 2020

Type: bugfix
Breaking change: no

Previously this would often fail on usage of sprintf() and other function that takes variadic parameters.

@tvbeek
Copy link
Member

tvbeek commented Sep 1, 2020

@AJenbo thanks for your PR, do you have the possibility to add a test that failed before this PR and is fixed with this PR?

I assign this PR to you, but feel free to say if you need help creating the tests.

@AJenbo
Copy link
Member Author

AJenbo commented Sep 1, 2020

Sure, will try and get around to it some time this week.

@tvbeek tvbeek added this to the Future milestone Jul 20, 2021
kylekatarnls
kylekatarnls previously approved these changes Jul 20, 2021
@kylekatarnls kylekatarnls modified the milestones: Future, 2.11.0 Jul 20, 2021
@kylekatarnls kylekatarnls modified the milestones: 2.11.0, 2.12.0 Dec 3, 2021
@AJenbo
Copy link
Member Author

AJenbo commented Dec 30, 2021

Oh, I forgot all about this one. I'll try come up with a same that show the issue.

@kylekatarnls kylekatarnls modified the milestones: 2.12.0, 2.13.0 Jun 19, 2022
@AJenbo
Copy link
Member Author

AJenbo commented Aug 18, 2022

No fair you guys merged another fix for this just a month later, and the didn't even trace down what the cause was 😂

#818

@codecov-commenter
Copy link

codecov-commenter commented Aug 18, 2022

Codecov Report

Merging #814 (ccdcc64) into master (87a0822) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##             master     #814   +/-   ##
=========================================
  Coverage     88.65%   88.65%           
- Complexity     1055     1056    +1     
=========================================
  Files            95       95           
  Lines          2661     2663    +2     
=========================================
+ Hits           2359     2361    +2     
  Misses          302      302           
Impacted Files Coverage Δ
src/main/php/PHPMD/Rule/AbstractLocalVariable.php 91.20% <100.00%> (+0.19%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@AJenbo
Copy link
Member Author

AJenbo commented Aug 18, 2022

Ok, I added a test, removed the "be quiet" fix, and made this one more explicit as to what it is doing. Hope that makes it good enough to merge :)

@kylekatarnls
Copy link
Member

kylekatarnls commented Aug 19, 2022

Hi, thanks to this I discovered that a parameter can be both variadic and reference 🤯

The test is good as it's what also missed in #818 but I did not succeed to find a case that would explain why we need specific stuff for PHP < 5.6 or why min() solution is better than isset()

I tried:

#973 (min)
PHP 5.3 result

#974 (isset)
PHP 5.3 result

By the way I added there a test for variadic+reference but current fixes still don't support it, so we need an other fix for that.

class Test
{
    public function foo(string $format, mixed &...$values): string
    {
        $values[1] = 9;

        return sprintf($format, ...$values);
    }

    public function bar()
    {
        $variadic1 = 1;
        $this->print("Text %d", $variadic1, $variadic2);

        return $variadic2;
    }
}

$t = new Test();

echo $t->bar();

@AJenbo
Copy link
Member Author

AJenbo commented Aug 19, 2022

Without PHP < 5.6 it will fail because isVeriadic() is only available from PHP 5.6. I chose to use || in case someone is trying to analyze PHP 5.6 code in 5.5. But we could also set it to fail in that case using && instead.

min() allows you to know that it was the variadic paramter, and will correctly let you know if it is by reference. isset() simply ignores it and says it's not by reference even if it is.

I think it also better allow you to realize why the count might be higher, rather then just ignoring a case that appears like it should never happen.

@kylekatarnls
Copy link
Member

kylekatarnls commented Aug 20, 2022

min() allows you to know that it was the variadic paramter, and will correctly let you know if it is by reference. isset() simply ignores it and says it's not by reference even if it is.

That's also what I believed reading the code but as you can see here it's still producing a violation: #973 (in both PHP <= 5.6 and > 5.6)

@kylekatarnls kylekatarnls removed this from the 2.13.0 milestone Sep 10, 2022
@kylekatarnls kylekatarnls added this to the 2.14.0 milestone Sep 10, 2022
@kylekatarnls kylekatarnls modified the milestones: 2.14.0, 2.15.0 Sep 27, 2023
@kylekatarnls kylekatarnls modified the milestones: 2.15.0, 2.16.0 Dec 10, 2023
@ravage84
Copy link
Member

@AJenbo can this be updated to be mergable into 3.0.0?

@AJenbo
Copy link
Member Author

AJenbo commented May 15, 2024

Sure, though there isn't anything BC in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants