Skip to content

Commit

Permalink
bug: Prevent breaking functions returning by reference (#6487)
Browse files Browse the repository at this point in the history
Prevent breaking functions returning by reference
  • Loading branch information
julienfalque authored Jul 15, 2022
1 parent d29ea1d commit f403e7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Fixer/ReturnNotation/ReturnAssignmentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$next = $tokens->getNextMeaningfulToken($index);
if ($tokens[$next]->isGivenKind(CT::T_RETURN_REF)) {
continue;
}

$functionOpenIndex = $tokens->getNextTokenOfKind($index, ['{', ';']);
if ($tokens[$functionOpenIndex]->equals(';')) { // abstract function
$index = $functionOpenIndex - 1;
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixer/ReturnNotation/ReturnAssignmentFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,22 @@ public function __destruct()
var_dump($a); // $a = 2 here _╯°□°╯︵┻━┻
',
],
'variable returned by reference in function' => [
'<?php
function &foo() {
$var = 1;
return $var;
}',
],
'variable returned by reference in method' => [
'<?php
class Foo {
public function &bar() {
$var = 1;
return $var;
}
}',
],
];
}

Expand Down

0 comments on commit f403e7f

Please sign in to comment.