8000 bug #50560 [DependencyInjection] Support PHP 8.2 `true` and `null` ty… · symfony/symfony@88a20a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88a20a6

Browse files
bug #50560 [DependencyInjection] Support PHP 8.2 true and null type (ruudk)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Support PHP 8.2 `true` and `null` type | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? |yes | New feature? |no | Deprecations? |no | Tickets | | License | MIT | Doc PR | https://stitcher.io/blog/new-in-php-82#null,--true,-and-false-as-standalone-types-rfc Commits ------- 3fb90e2 [DependencyInjection] Support PHP 8.2 `true` type
2 parents 09e9cfe + 3fb90e2 commit 88a20a6

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
308308
if (false === $value) {
309309
return;
310310
}
311+
} elseif ('true' === $type) {
312+
if (true === $value) {
313+
return;
314+
}
311315
} elseif ($reflectionType->isBuiltin()) {
312316
$checkFunction = sprintf('is_%s', $type);
313317
if ($checkFunction($value)) {

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\FooObject;
3333
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\IntersectionConstructor;
3434
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructor;
35+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructorPHP82;
3536
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo;
3637
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\WaldoFoo;
3738
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Wobble;
@@ -868,6 +869,26 @@ public function testUnionTypePassesWithFalse()
868869
$this->addToAssertionCount(1);
869870
}
870871

872+
/**
873+
* @requires PHP 8.2
874+
*/
875+
public function testUnionTypePassesWithTrue()
876+
{
877+
$container = new ContainerBuilder();
878+
879+
$container->register('unionTrue', UnionConstructorPHP82::class)
880+
->setFactory([UnionConstructorPHP82::class, 'createTrue'])
881+
->setArguments([true]);
882+
883+
$container->register('unionNull', UnionConstructorPHP82::class)
884+
->setFactory([UnionConstructorPHP82::class, 'createNull'])
885+
->setArguments([null]);
886+
887+
(new CheckTypeDeclarationsPass(true))->process($container);
888+
889+
$this->addToAssertionCount(1);
890+
}
891+
871892
/**
872893
* @requires PHP 8
873894
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass;
4+
5+
class UnionConstructorPHP82
6+
{
7+
public static function createTrue(array|true $arg): static
8+
{
9+
return new static(0);
10+
}
11+
12+
public static function createNull(null $arg): static
13+
{
14+
return new static(0);
15+
}
16+
}

0 commit comments

Comments
 (0)
0