Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions src/Reflection/Callables/FunctionCallableVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ public function getAsserts(): Assertions
return $this->function->getAsserts();
}

public function isBuiltin(): TrinaryLogic
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.

implement this method on CallableParametersAcceptor instead

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.

Done. The changes:

  • Added isBuiltin(): TrinaryLogic to the CallableParametersAcceptor interface
  • Implemented it in all 7 implementors: ClosureType, CallableType, TrivialParametersAcceptor, InaccessibleMethod (all return TrinaryLogic::createNo()), ExtendedCallableFunctionVariant, ResolvedFunctionVariantWithCallable (propagate via constructor parameter), and FunctionCallableVariant (existing implementation)
  • Updated creation sites in GenericParametersAcceptorResolver and ParametersAcceptorSelector to propagate isBuiltin through
  • Simplified CallableType::isSuperTypeOfInternal() to use $variant->isBuiltin()->yes() instead of $variant instanceof FunctionCallableVariant && $variant->isBuiltin()->yes()

{
$isBuiltin = $this->function->isBuiltin();
if ($isBuiltin instanceof TrinaryLogic) {
return $isBuiltin;
}

return TrinaryLogic::createFromBoolean($isBuiltin);
}

}
4 changes: 3 additions & 1 deletion src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\PhpDocParser\Printer\Printer;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\FunctionCallableVariant;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
Expand Down Expand Up @@ -180,11 +181,12 @@ private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSup

$variantsResult = null;
foreach ($type->getCallableParametersAcceptors($scope) as $variant) {
$isBuiltinCallable = $variant instanceof FunctionCallableVariant && $variant->isBuiltin()->yes();
$variant = ParametersAcceptorSelector::selectFromTypes($parameterTypes, [$variant], false);
if (!$variant instanceof CallableParametersAcceptor) {
return IsSuperTypeOfResult::createNo([]);
}
$isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny);
$isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny, !$isBuiltinCallable);
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
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,18 @@ 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'], []);
}

public function testBug13247(): void
{
$this->analyse([__DIR__ . '/data/bug-13247.php'], []);
Expand Down
30 changes: 30 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,30 @@
<?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');
}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11619.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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');
}
Loading