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
3 changes: 3 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
case 'object':
return new ObjectWithoutClassType();

case 'callable-object':
return new IntersectionType([new ObjectWithoutClassType(), new CallableType()]);

case 'never':
$type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope);

Expand Down
46 changes: 33 additions & 13 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use function count;
use function implode;
use function in_array;
use function ksort;
use function sprintf;
use function strlen;
use function substr;
Expand Down Expand Up @@ -200,12 +201,13 @@ function () use ($level): string {

private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes): string
{
$baseTypes = [];
$typesToDescribe = [];
$skipTypeNames = [];

$nonEmptyStr = false;
$nonFalsyStr = false;
foreach ($this->getSortedTypes() as $type) {
foreach ($this->getSortedTypes() as $i => $type) {
if ($type instanceof AccessoryNonEmptyStringType
|| $type instanceof AccessoryLiteralStringType
|| $type instanceof AccessoryNumericStringType
Expand All @@ -228,33 +230,49 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
}
}

$typesToDescribe[] = $type;
$typesToDescribe[$i] = $type;
$skipTypeNames[] = 'string';
continue;
}
if ($type instanceof NonEmptyArrayType || $type instanceof AccessoryArrayListType) {
$typesToDescribe[] = $type;
$typesToDescribe[$i] = $type;
$skipTypeNames[] = 'array';
continue;
}

if ($skipAccessoryTypes) {
if ($type instanceof CallableType && $type->isCommonCallable()) {
$typesToDescribe[$i] = $type;
$skipTypeNames[] = 'object';
$skipTypeNames[] = 'string';
continue;
}

if (!$type instanceof AccessoryType) {
$baseTypes[$i] = $type;
continue;
}

if ($skipAccessoryTypes) {
continue;
}

$typesToDescribe[] = $type;
$typesToDescribe[$i] = $type;
}

$describedTypes = [];
foreach ($this->getSortedTypes() as $type) {
if ($type instanceof AccessoryType) {
continue;
}
foreach ($baseTypes as $i => $type) {
$typeDescription = $type->describe($level);

if (in_array($typeDescription, ['object', 'string'], true) && in_array($typeDescription, $skipTypeNames, true)) {
foreach ($typesToDescribe as $j => $typeToDescribe) {
if ($typeToDescribe instanceof CallableType && $typeToDescribe->isCommonCallable()) {
$describedTypes[$i] = 'callable-' . $typeDescription;
unset($typesToDescribe[$j]);
continue 2;
}
}
}

if (
substr($typeDescription, 0, strlen('array<')) === 'array<'
&& in_array('array', $skipTypeNames, true)
Expand All @@ -281,21 +299,23 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
$typeName = 'non-empty-' . $typeName;
}

$describedTypes[] = $typeName . '<' . substr($typeDescription, strlen('array<'));
$describedTypes[$i] = $typeName . '<' . substr($typeDescription, strlen('array<'));
continue;
}

if (in_array($typeDescription, $skipTypeNames, true)) {
continue;
}

$describedTypes[] = $type->describe($level);
$describedTypes[$i] = $type->describe($level);
}

foreach ($typesToDescribe as $typeToDescribe) {
$describedTypes[] = $typeToDescribe->describe($level);
foreach ($typesToDescribe as $i => $typeToDescribe) {
$describedTypes[$i] = $typeToDescribe->describe($level);
}

ksort($describedTypes);

return implode('&', $describedTypes);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,9 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7519.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8087.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5785.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/callable-object.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/callable-string.php');
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/data/callable-object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace CallableObject;

use Iterator;
use function PHPStan\Testing\assertType;

/**
* @param object $object
* @param callable-object $callableObject
*/
function foo(callable $callable, $object, $callableObject)
{
assertType('callable(): mixed', $callable);
assertType('object', $object);
assertType('callable-object', $callableObject);

if (is_object($callable)) {
assertType('callable-object', $callable);
}

if (is_callable($object)) {
assertType('callable-object', $object);
}

if ($callable === $object) {
assertType('callable-object', $callable);
assertType('callable-object', $object);
}
}

/**
* @param Iterator&callable $object
*/
function bar($object)
{
assertType('callable(): mixed&Iterator', $object);
}
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/data/callable-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CallableString;

use function PHPStan\Testing\assertType;

/**
* @param callable-string $callableString
*/
function foo(callable $callable, string $string, string $callableString)
{
assertType('callable(): mixed', $callable);
assertType('string', $string);
assertType('callable-string', $callableString);

if (is_string($callable)) {
assertType('callable-string', $callable);
}

if (is_callable($string)) {
assertType('callable-string', $string);
}

if ($callable === $string) {
assertType('callable-string', $callable);
assertType('callable-string', $string);
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/param-out.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,5 @@ function fooReplace() {
function fooIsCallable($x, bool $b)
{
is_callable($x, $b, $name);
assertType('callable(): mixed&string', $name);
assertType('callable-string', $name);
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1522,15 +1522,15 @@ public function dataUnion(): iterable
new ConstantStringType('test_function'),
],
UnionType::class,
'\'test_function\'|(callable(): mixed&string)',
'\'test_function\'|callable-string',
],
[
[
new IntersectionType([new StringType(), new CallableType()]),
new IntegerType(),
],
UnionType::class,
'(callable(): mixed&string)|int',
'callable-string|int',
],
[
[
Expand Down