Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Support for intersection types #42099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2021
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
1 change: 1 addition & 0 deletions 1 .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php'):
case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php'):
continue 2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNes
$prefix.'allowsNull' => $c->allowsNull(),
$prefix.'isBuiltin' => $c->isBuiltin(),
];
} elseif ($c instanceof \ReflectionUnionType) {
} elseif ($c instanceof \ReflectionUnionType || $c instanceof \ReflectionIntersectionType) {
$a[$prefix.'allowsNull'] = $c->allowsNull();
self::addMap($a, $c, [
'types' => 'getTypes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionIntersectionTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;

Expand Down Expand Up @@ -78,7 +79,7 @@ public function testClosureCaster()
$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "71 to 71"
line: "72 to 72"
}
EOTXT
, $var
Expand Down Expand Up @@ -228,6 +229,26 @@ public function testReflectionParameterNullableUnion()
);
}

/**
* @requires PHP 8.1
*/
public function testReflectionParameterIntersection()
{
$f = eval('return function (Traversable&Countable $a) {};');
$var = new \ReflectionParameter($f, 0);

$this->assertDumpMatchesFormat(
<<<'EOTXT'
ReflectionParameter {
+name: "a"
position: 0
typeHint: "Traversable&Countable"
}
EOTXT
, $var
);
}

/**
* @requires PHP 7.4
*/
Expand Down Expand Up @@ -292,6 +313,34 @@ public function testReflectionUnionType()
);
}

/**
* @requires PHP 8.1
*/
public function testReflectionIntersectionType()
{
$var = (new \ReflectionProperty(ReflectionIntersectionTypeFixture::class, 'a'))->getType();
$this->assertDumpMatchesFormat(
<<<'EOTXT'
ReflectionIntersectionType {
allowsNull: false
types: array:2 [
0 => ReflectionNamedType {
name: "Traversable"
allowsNull: false
isBuiltin: false
}
1 => ReflectionNamedType {
name: "Countable"
allowsNull: false
isBuiltin: false
}
]
Comment on lines +326 to +337
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to match the generated output for ReflectionUnionType here. This is a bit verbose though because the sub-types of an intersection are guaranteed to be a ReflectionNamedType with $allowsNull === false and $isBuiltin === false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, nullable intersection types are possible, see https://3v4l.org/aURFN/rfc#vgit.master

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well. It's not like I haven't told them. 🤷🏻 php/php-src#6799 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loophole was closed. php/php-src#7254

}
EOTXT
, $var
);
}

/**
* @requires PHP 8
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class ReflectionIntersectionTypeFixture
{
public \Traversable&\Countable $a;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.