Skip to content
Open
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: 2 additions & 0 deletions src/Reflection/Callables/CallableParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public function mustUseReturnValue(): TrinaryLogic;

public function getAsserts(): Assertions;

public function isBuiltin(): TrinaryLogic;

}
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);
}

}
6 changes: 6 additions & 0 deletions src/Reflection/ExtendedCallableFunctionVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(
private array $usedVariables,
private TrinaryLogic $acceptsNamedArguments,
private TrinaryLogic $mustUseReturnValue,
private TrinaryLogic $isBuiltinCallable,
private ?Assertions $assertions = null,
)
{
Expand Down Expand Up @@ -92,4 +93,9 @@ public function getAsserts(): Assertions
return $this->assertions ?? Assertions::createEmpty();
}

public function isBuiltin(): TrinaryLogic
{
return $this->isBuiltinCallable;
}

}
1 change: 1 addition & 0 deletions src/Reflection/GenericParametersAcceptorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
$originalParametersAcceptor->getUsedVariables(),
$originalParametersAcceptor->acceptsNamedArguments(),
$originalParametersAcceptor->mustUseReturnValue(),
$originalParametersAcceptor->isBuiltin(),
$originalParametersAcceptor->getAsserts(),
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/InaccessibleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function isBuiltin(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

}
4 changes: 4 additions & 0 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
$usedVariables = [];
$acceptsNamedArguments = TrinaryLogic::createNo();
$mustUseReturnValue = TrinaryLogic::createMaybe();
$isBuiltin = TrinaryLogic::createMaybe();

foreach ($acceptors as $acceptor) {
$returnTypes[] = $acceptor->getReturnType();
Expand All @@ -753,6 +754,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
$usedVariables = array_merge($usedVariables, $acceptor->getUsedVariables());
$acceptsNamedArguments = $acceptsNamedArguments->or($acceptor->acceptsNamedArguments());
$mustUseReturnValue = $mustUseReturnValue->or($acceptor->mustUseReturnValue());
$isBuiltin = $isBuiltin->or($acceptor->isBuiltin());
}
$isVariadic = $isVariadic || $acceptor->isVariadic();

Expand Down Expand Up @@ -860,6 +862,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
$usedVariables,
$acceptsNamedArguments,
$mustUseReturnValue,
$isBuiltin,
);
}

Expand Down Expand Up @@ -897,6 +900,7 @@ private static function wrapAcceptor(ParametersAcceptor $acceptor): ExtendedPara
$acceptor->getUsedVariables(),
$acceptor->acceptsNamedArguments(),
$acceptor->mustUseReturnValue(),
$acceptor->isBuiltin(),
$acceptor->getAsserts(),
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/ResolvedFunctionVariantWithCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(
private array $usedVariables,
private TrinaryLogic $acceptsNamedArguments,
private TrinaryLogic $mustUseReturnValue,
private TrinaryLogic $isBuiltinCallable,
private ?Assertions $assertions = null,
)
{
Expand Down Expand Up @@ -124,4 +125,9 @@ public function getAsserts(): Assertions
return $this->assertions ?? Assertions::createEmpty();
}

public function isBuiltin(): TrinaryLogic
{
return $this->isBuiltinCallable;
}

}
5 changes: 5 additions & 0 deletions src/Reflection/TrivialParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function isBuiltin(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

}
8 changes: 7 additions & 1 deletion src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSup

$variantsResult = null;
foreach ($type->getCallableParametersAcceptors($scope) as $variant) {
$isBuiltinCallable = $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 Expand Up @@ -404,6 +405,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function isBuiltin(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function toNumber(): Type
{
return new ErrorType();
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: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public function getAsserts(): Assertions
return $this->assertions;
}

public function isBuiltin(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

/**
* @return array<non-empty-string, TemplateTag>
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,39 @@ 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'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug11619Error(): void
{
$this->analyse([__DIR__ . '/data/bug-11619-error.php'], [
[
'Parameter #1 $string1 of function strnatcasecmp expects string, Bug11619Error\Foo given.',
32,
],
[
'Parameter #2 $string2 of function strnatcasecmp expects string, Bug11619Error\Foo given.',
32,
],
]);
}

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

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

namespace Bug11619Error;

final class Foo implements \Stringable {

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

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

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

}

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


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

var_export($options);
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');
usort($options, 'strnatcasecmp');
}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11619-typed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Bug11619Typed;

final class Foo implements \Stringable {

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

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

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

}

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


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

var_export($options);
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

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