Skip to content
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
32 changes: 29 additions & 3 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace PHPStan\Type;

use PHPStan\Internal\CombinationsHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ExtendedMethodReflection;
Expand All @@ -16,6 +18,7 @@
use PHPStan\Reflection\MissingConstantFromReflectionException;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\MissingPropertyFromReflectionException;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\Reflection\Type\IntersectionTypeUnresolvedMethodPrototypeReflection;
use PHPStan\Reflection\Type\IntersectionTypeUnresolvedPropertyPrototypeReflection;
Expand Down Expand Up @@ -1124,11 +1127,34 @@

public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
{
if ($this->isCallable()->no()) {
throw new ShouldNotHappenException();
$yesAcceptors = [];

foreach ($this->types as $type) {
if (!$type->isCallable()->yes()) {

Check warning on line 1133 in src/Type/IntersectionType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $yesAcceptors = []; foreach ($this->types as $type) { - if (!$type->isCallable()->yes()) { + if ($type->isCallable()->no()) { continue; } $yesAcceptors[] = $type->getCallableParametersAcceptors($scope);

Check warning on line 1133 in src/Type/IntersectionType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $yesAcceptors = []; foreach ($this->types as $type) { - if (!$type->isCallable()->yes()) { + if ($type->isCallable()->no()) { continue; } $yesAcceptors[] = $type->getCallableParametersAcceptors($scope);
continue;
}
$yesAcceptors[] = $type->getCallableParametersAcceptors($scope);
}

if (count($yesAcceptors) === 0) {
if ($this->isCallable()->no()) {

Check warning on line 1140 in src/Type/IntersectionType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } if (count($yesAcceptors) === 0) { - if ($this->isCallable()->no()) { + if (!$this->isCallable()->yes()) { throw new ShouldNotHappenException(); }

Check warning on line 1140 in src/Type/IntersectionType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } if (count($yesAcceptors) === 0) { - if ($this->isCallable()->no()) { + if (!$this->isCallable()->yes()) { throw new ShouldNotHappenException(); }
throw new ShouldNotHappenException();
}

return [new TrivialParametersAcceptor()];
}

$result = [];
$combinations = CombinationsHelper::combinations($yesAcceptors);
foreach ($combinations as $combination) {
$combined = ParametersAcceptorSelector::combineAcceptors($combination);
if (!$combined instanceof CallableParametersAcceptor) {
throw new ShouldNotHappenException();
}
$result[] = $combined;
}

return [new TrivialParametersAcceptor()];
return $result;
}

public function isCloneable(): TrinaryLogic
Expand Down
84 changes: 84 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14362.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14362;

use function PHPStan\Testing\assertType;

interface A
{
public function __invoke(B $b): int;
}

interface B
{

}

class C {
public static function u(): A&B {
return new class() implements A, B {
public function __invoke(B $b): int {
return 1;
}
};
}
}

class D {
public static function u(): A {
return new class() implements A {
public function __invoke(B $b): int {
return 1;
}
};
}
}

interface E
{
public function __invoke(B $b, bool $option = true): int;
}

interface F
{

}

class G {
public static function u(): A&E {
return new class() implements A, E {
public function __invoke(B $b, bool $option = true): int {
return 1;
}
};
}
}

class H {
public static function u(): B&F {
return new class() implements B, F {
};
}
}

function doBar() : void {
assertType('Closure(Bug14362\B): int', C::u()(...));
assertType('Closure(Bug14362\B): int', D::u()(...));

// Intersection with two yes-callable compatible
assertType('Closure(Bug14362\B, bool=): int', G::u()(...));

// Intersection with only maybe-callable types (neither has __invoke)
assertType('Closure', H::u()(...));
}

function doFoo(string $c):void {
if (is_callable($c)) {
$a = $c;
} else {
$a = C::u()(...);
}
assertType('callable-string|(Closure(Bug14362\B): int)', $a);
}
Loading