Skip to content

Commit a72e39c

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Accept and describe callable-object/callable-string
1 parent d2227f8 commit a72e39c

7 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎src/PhpDoc/TypeNodeResolver.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
325325
case 'object':
326326
return new ObjectWithoutClassType();
327327

328+
case 'callable-object':
329+
return new IntersectionType([new ObjectWithoutClassType(), new CallableType()]);
330+
328331
case 'never':
329332
$type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope);
330333

‎src/Type/IntersectionType.php‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ function () use ($level): string {
200200

201201
private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes): string
202202
{
203+
$baseTypes = [];
203204
$typesToDescribe = [];
204205
$skipTypeNames = [];
205206

@@ -238,23 +239,39 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
238239
continue;
239240
}
240241

241-
if ($skipAccessoryTypes) {
242+
if ($type instanceof CallableType && $type->isCommonCallable()) {
243+
$typesToDescribe[] = $type;
244+
$skipTypeNames[] = 'object';
245+
$skipTypeNames[] = 'string';
242246
continue;
243247
}
244248

245249
if (!$type instanceof AccessoryType) {
250+
$baseTypes[] = $type;
251+
continue;
252+
}
253+
254+
if ($skipAccessoryTypes) {
246255
continue;
247256
}
248257

249258
$typesToDescribe[] = $type;
250259
}
251260

252261
$describedTypes = [];
253-
foreach ($this->getSortedTypes() as $type) {
254-
if ($type instanceof AccessoryType) {
255-
continue;
256-
}
262+
foreach ($baseTypes as $type) {
257263
$typeDescription = $type->describe($level);
264+
265+
if (in_array($typeDescription, ['object', 'string'], true) && in_array($typeDescription, $skipTypeNames, true)) {
266+
foreach ($typesToDescribe as $j => $typeToDescribe) {
267+
if ($typeToDescribe instanceof CallableType && $typeToDescribe->isCommonCallable()) {
268+
$describedTypes[] = 'callable-' . $typeDescription;
269+
unset($typesToDescribe[$j]);
270+
continue 2;
271+
}
272+
}
273+
}
274+
258275
if (
259276
substr($typeDescription, 0, strlen('array<')) === 'array<'
260277
&& in_array('array', $skipTypeNames, true)

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,9 @@ public function dataFileAsserts(): iterable
10961096
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7519.php');
10971097
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8087.php');
10981098
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5785.php');
1099+
1100+
yield from $this->gatherAssertTypes(__DIR__ . '/data/callable-object.php');
1101+
yield from $this->gatherAssertTypes(__DIR__ . '/data/callable-string.php');
10991102
}
11001103

11011104
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CallableObject;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param object $object
9+
* @param callable-object $callableObject
10+
*/
11+
function foo(callable $callable, $object, $callableObject)
12+
{
13+
assertType('callable(): mixed', $callable);
14+
assertType('object', $object);
15+
assertType('callable-object', $callableObject);
16+
17+
if (is_object($callable)) {
18+
assertType('callable-object', $callable);
19+
}
20+
21+
if (is_callable($object)) {
22+
assertType('callable-object', $object);
23+
}
24+
25+
if ($callable === $object) {
26+
assertType('callable-object', $callable);
27+
assertType('callable-object', $object);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace CallableString;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param callable-string $callableString
9+
*/
10+
function foo(callable $callable, string $string, string $callableString)
11+
{
12+
assertType('callable(): mixed', $callable);
13+
assertType('string', $string);
14+
assertType('callable-string', $callableString);
15+
16+
if (is_string($callable)) {
17+
assertType('callable-string', $callable);
18+
}
19+
20+
if (is_callable($string)) {
21+
assertType('callable-string', $string);
22+
}
23+
24+
if ($callable === $string) {
25+
assertType('callable-string', $callable);
26+
assertType('callable-string', $string);
27+
}
28+
}

‎tests/PHPStan/Analyser/data/param-out.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,5 +431,5 @@ function fooReplace() {
431431
function fooIsCallable($x, bool $b)
432432
{
433433
is_callable($x, $b, $name);
434-
assertType('callable(): mixed&string', $name);
434+
assertType('callable-string', $name);
435435
}

‎tests/PHPStan/Type/TypeCombinatorTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,15 +1522,15 @@ public function dataUnion(): iterable
15221522
new ConstantStringType('test_function'),
15231523
],
15241524
UnionType::class,
1525-
'\'test_function\'|(callable(): mixed&string)',
1525+
'\'test_function\'|callable-string',
15261526
],
15271527
[
15281528
[
15291529
new IntersectionType([new StringType(), new CallableType()]),
15301530
new IntegerType(),
15311531
],
15321532
UnionType::class,
1533-
'(callable(): mixed&string)|int',
1533+
'callable-string|int',
15341534
],
15351535
[
15361536
[

0 commit comments

Comments
 (0)