Skip to content

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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.78%. Comparing base (0a3060e) to head (090d480).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #814      +/-   ##
============================================
- Coverage     92.84%   92.78%   -0.06%     
- Complexity     1277     1278       +1     
============================================
  Files           112      112              
  Lines          3478     3257     -221     
============================================
- Hits           3229     3022     -207     
+ Misses          249      235      -14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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.

$parameters = $reflectionFunction->getParameters();

return isset($parameters[$argumentPosition]) && $parameters[$argumentPosition]->isPassedByReference();
if (version_compare(PHP_VERSION, '5.6', '<') || $reflectionFunction->isVariadic()) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (version_compare(PHP_VERSION, '5.6', '<') || $reflectionFunction->isVariadic()) {
if (PHP_VERSION_ID < 50600 || $reflectionFunction->isVariadic()) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Pulling this in to the merge in to 3.x

@AJenbo AJenbo merged commit d21ca9a into phpmd:master Dec 21, 2025
42 of 43 checks passed
@AJenbo AJenbo deleted the patch-1 branch December 21, 2025 15:26
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