Skip to content

Commit

Permalink
Check types when argument is passed by reference for userland functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmaskey authored Feb 24, 2024
1 parent ea95907 commit ef31b4f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function check(
if ($this->checkArgumentTypes) {
$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());

if (!$parameter->passedByReference()->createsNewVariable()) {
if (!$parameter->passedByReference()->createsNewVariable() || !$isBuiltin) {
$accepts = $this->ruleLevelHelper->acceptsWithReason($parameterType, $argumentValueType, $scope->isDeclareStrictTypes());

if (!$accepts->result) {
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ public function testPassingNonVariableToParameterPassedByReference(): void
'Parameter #1 $array of function reset expects array|object, null given.',
39,
],
[
'Parameter #1 $s of function PassedByReference\bar expects string, int given.',
48,
],
]);
}

Expand Down Expand Up @@ -1623,4 +1627,18 @@ public function testDiscussion10454(): void
]);
}

public function testBug10626(): void
{
$this->analyse([__DIR__ . '/data/bug-10626.php'], [
[
'Parameter #1 $value of function Bug10626\intByValue expects int, string given.',
16,
],
[
'Parameter #1 $value of function Bug10626\intByReference expects int, string given.',
17,
],
]);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10626.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug10626;

function intByValue(int $value): void
{

}

function intByReference(int &$value): void
{

}

$notAnInt = 'not-an-int';
intByValue($notAnInt);
intByReference($notAnInt);
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ public function testBug5781(): void
]);
}

public function testBug8296(): void
{
$this->checkThisOnly = false;
$this->analyse([__DIR__ . '/data/bug-8296.php'], [
[
'Parameter #1 $objects of static method Bug8296\VerifyLoginTask::continueDump() expects array<string, object>, array<string, Bug8296\stdClass|true> given.',
12,
],
[
'Parameter #1 $string of static method Bug8296\VerifyLoginTask::stringByRef() expects string, int given.',
15,
],
]);
}

public function testRequireExtends(): void
{
$this->checkThisOnly = false;
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-8296.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bug8296;

class VerifyLoginTask{

public static function dumpMemory() : void{
$dummy = [
"a" => new stdClass(),
"b" => true
];
self::continueDump($dummy);

$string = 12345;
self::stringByRef($string);
}

/**
* @phpstan-param array<string, object> $objects
* @phpstan-param-out array<string, object> $objects
*/
private static function continueDump(array &$objects) : void{

}

private static function stringByRef(string &$string) : void{

}
}

0 comments on commit ef31b4f

Please sign in to comment.