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

Check types when argument is passed by reference #2941

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Member

Choose a reason for hiding this comment

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

This needs to have Bug8296 namespace


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{

}
}
Loading