Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -363,7 +363,7 @@ public function check(
!$parameter->passedByReference()->createsNewVariable()
|| (!$isBuiltin && !$argumentValueType instanceof ErrorType)
) {
$accepts = $this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes());
$accepts = $this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes() && !($isBuiltin && $parameterType->isCallable()->yes()));

if (!$accepts->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
Expand Down
6 changes: 3 additions & 3 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
return $type->isAcceptedBy($this, $strictTypes);
}

return $this->isSuperTypeOfInternal($type, true)->toAcceptsResult();
return $this->isSuperTypeOfInternal($type, true, $strictTypes)->toAcceptsResult();
}

public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
Expand All @@ -151,7 +151,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
return $this->isSuperTypeOfInternal($type, false);
}

private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSuperTypeOfResult
private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny, bool $strictTypes = true): IsSuperTypeOfResult
{
$isCallable = new IsSuperTypeOfResult($type->isCallable(), []);
if ($isCallable->no()) {
Expand Down Expand Up @@ -184,7 +184,7 @@ private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSup
if (!$variant instanceof CallableParametersAcceptor) {
return IsSuperTypeOfResult::createNo([]);
}
$isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny);
$isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny, $strictTypes);
if ($variantsResult === null) {
$variantsResult = $isSuperType;
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Type/CallableTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function isParametersAcceptorSuperTypeOf(
CallableParametersAcceptor $ours,
CallableParametersAcceptor $theirs,
bool $treatMixedAsAny,
bool $strictTypes = true,
): IsSuperTypeOfResult
{
$theirParameters = $theirs->getParameters();
Expand Down Expand Up @@ -72,7 +73,7 @@ public static function isParametersAcceptorSuperTypeOf(
}

if ($treatMixedAsAny) {
$isSuperType = $theirParameter->getType()->accepts($ourParameterType, true);
$isSuperType = $theirParameter->getType()->accepts($ourParameterType, $strictTypes);
$isSuperType = new IsSuperTypeOfResult($isSuperType->result, $isSuperType->reasons);
} else {
$isSuperType = $theirParameter->getType()->isSuperTypeOf($ourParameterType);
Expand Down
5 changes: 3 additions & 2 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
return $this->objectType->accepts($type, $strictTypes);
}

return $this->isSuperTypeOfInternal($type, true)->toAcceptsResult();
return $this->isSuperTypeOfInternal($type, true, $strictTypes)->toAcceptsResult();
}

public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
Expand All @@ -237,7 +237,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
return $this->isSuperTypeOfInternal($type, false);
}

private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSuperTypeOfResult
private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny, bool $strictTypes = true): IsSuperTypeOfResult
{
if ($type instanceof self) {
$parameterTypes = array_map(static fn ($parameter) => $parameter->getType(), $this->getParameters());
Expand All @@ -249,6 +249,7 @@ private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSup
$this,
$variant,
$treatMixedAsAny,
$strictTypes,
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,31 @@ public function testBug12363(): void
$this->analyse([__DIR__ . '/data/bug-12363.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug11619(): void
{
$this->analyse([__DIR__ . '/data/bug-11619.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug11619Strict(): void
{
$this->analyse([__DIR__ . '/data/bug-11619-strict.php'], [
[
'Parameter #1 $string1 of function strnatcasecmp expects string, Bug11619Strict\Foo given.',
31,
],
[
'Parameter #2 $string2 of function strnatcasecmp expects string, Bug11619Strict\Foo given.',
31,
],
[
'Parameter #2 $f of function Bug11619Strict\customUsort expects callable(Stringable, Stringable): int, \'strnatcasecmp\' given.',
54,
],
]);
Comment thread
staabm marked this conversation as resolved.
}

public function testBug13247(): void
{
$this->analyse([__DIR__ . '/data/bug-13247.php'], []);
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11619-strict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug11619Strict;

final class Foo implements \Stringable {

private function __construct(public readonly string $value) {
}

public static function fromString(string $string): self {
return new self($string);
}

public function __toString(): string {
return $this->value;
}

}

function test(): void
{
$options = [
Foo::fromString('c'),
Foo::fromString('b'),
Foo::fromString('a'),
];

uasort($options, 'strnatcasecmp');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

needs another test like

function userlandComparator(string $a, string $b): int {
	return strnatcasecmp($a, $b);
}

$options = [
	Foo::fromString('c'),
	Foo::fromString('b'),
	Foo::fromString('a'),
];


uasort($options, 'userlandComparator');
var_dump($options);

for both, strict-types 0 or 1. there should be no error for userland comparators, see https://3v4l.org/gEURZ#veol

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The background make tests task also completed successfully. All 11666 tests passed. The changes have already been committed and pushed.

usort($options, 'strnatcasecmp');

uasort($options, fn($a, $b) => strnatcasecmp($a, $b));
uasort($options, fn(string $a, string $b) => strnatcasecmp($a, $b));
}

/**
* @param array<\Stringable> $a
* @param callable(\Stringable, \Stringable): int $f
*/
function customUsort(array &$a, callable $f): void
{
for ($i = 1; $i < count($a); $i++)
for ($j = $i; $j > 0 && $f($a[$j-1], $a[$j]) > 0; $j--)
[$a[$j-1], $a[$j]] = [$a[$j], $a[$j-1]];
}

function test2(): void
{
$options = [
Foo::fromString('c'),
Foo::fromString('b'),
Foo::fromString('a'),
];

customUsort($options, 'strnatcasecmp');
}
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11619.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php // lint >= 8.1
Comment thread
staabm marked this conversation as resolved.

namespace Bug11619;

final class Foo implements \Stringable {

private function __construct(public readonly string $value) {
}

public static function fromString(string $string): self {
return new self($string);
}

public function __toString(): string {
return $this->value;
}

}

function test(): void
{
$options = [
Foo::fromString('c'),
Foo::fromString('b'),
Foo::fromString('a'),
];

uasort($options, 'strnatcasecmp');
usort($options, 'strnatcasecmp');

uasort($options, fn($a, $b) => strnatcasecmp($a, $b));
uasort($options, fn(string $a, string $b) => strnatcasecmp($a, $b));
}

/**
* @param array<\Stringable> $a
* @param callable(\Stringable, \Stringable): int $f
*/
function customUsort(array &$a, callable $f): void
{
for ($i = 1; $i < count($a); $i++)
for ($j = $i; $j > 0 && $f($a[$j-1], $a[$j]) > 0; $j--)
[$a[$j-1], $a[$j]] = [$a[$j], $a[$j-1]];
}

function test2(): void
{
$options = [
Foo::fromString('c'),
Foo::fromString('b'),
Foo::fromString('a'),
];

customUsort($options, 'strnatcasecmp');
}
Loading