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

Add support of PHP8 static return type for withers #36636

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
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
Add support of PHP8 static return type for withers
  • Loading branch information
l-vo committed Apr 30, 2020
commit 04fdf05cff09458131fe2b94d8055d13e8425632
1 change: 1 addition & 0 deletions 1 .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Compiler/OptionalServiceClass.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ParentNotExists.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/BadClasses/MissingParent.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/WitherStaticReturnType.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/'):
case false !== strpos($file, '/src/Symfony/Component/ErrorHandler/Tests/Fixtures/'):
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php'):
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CHANGELOG
* deprecated `Definition::getDeprecationMessage()`, use `Definition::getDeprecation()` instead
* deprecated `Alias::getDeprecationMessage()`, use `Alias::getDeprecation()` instead
* deprecated PHP-DSL's `inline()` function, use `service()` instead
* added support of PHP8 static return type for withers

5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function processValue($value, bool $isRoot = false)
while (true) {
if (false !== $doc = $r->getDocComment()) {
if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
if (preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@return\s++static[\s\*]#i', $doc)) {
if ($this->isWither($reflectionMethod, $doc)) {
$withers[] = [$reflectionMethod->name, [], true];
} else {
$value->addMethodCall($reflectionMethod->name, []);
Expand Down Expand Up @@ -81,4 +81,20 @@ protected function processValue($value, bool $isRoot = false)

return $value;
}

private function isWither(\ReflectionMethod $reflectionMethod, string $doc): bool
{
$match = preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@return\s++(static|\$this)[\s\*]#i', $doc, $matches);
if ($match && 'static' === $matches[1]) {
return true;
}

if ($match && '$this' === $matches[1]) {
return false;
}

$reflectionType = $reflectionMethod->hasReturnType() ? $reflectionMethod->getReturnType() : null;

return $reflectionType instanceof \ReflectionNamedType && 'static' === $reflectionType->getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;

require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';

Expand Down Expand Up @@ -99,4 +100,28 @@ public function testWitherInjection()
];
$this->assertSame($expected, $methodCalls);
}

/**
* @requires PHP 8
*/
public function testWitherWithStaticReturnTypeInjection()
{
$container = new ContainerBuilder();
$container->register(Foo::class);

$container
->register('wither', WitherStaticReturnType::class)
->setAutowired(true);

(new ResolveClassPass())->process($container);
(new AutowireRequiredMethodsPass())->process($container);

$methodCalls = $container->getDefinition('wither')->getMethodCalls();

$expected = [
['withFoo', [], true],
['setFoo', []],
];
$this->assertSame($expected, $methodCalls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\ExpressionLanguage\Expression;

Expand Down Expand Up @@ -1624,6 +1625,25 @@ public function testWither()
$this->assertInstanceOf(Foo::class, $wither->foo);
}

/**
* @requires PHP 8
*/
public function testWitherWithStaticReturnType()
{
$container = new ContainerBuilder();
$container->register(Foo::class);

$container
->register('wither', WitherStaticReturnType::class)
->setPublic(true)
->setAutowired(true);

$container->compile();

$wither = $container->get('wither');
$this->assertInstanceOf(Foo::class, $wither->foo);
}

public function testAutoAliasing()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\DependencyInjection\Variable;
use Symfony\Component\ExpressionLanguage\Expression;
Expand Down Expand Up @@ -1362,6 +1363,31 @@ public function testWither()
$this->assertInstanceOf(Foo::class, $wither->foo);
}

/**
* @requires PHP 8
*/
public function testWitherWithStaticReturnType()
l-vo marked this conversation as resolved.
Show resolved Hide resolved
{
$container = new ContainerBuilder();
$container->register(Foo::class);

$container
->register('wither', WitherStaticReturnType::class)
->setPublic(true)
->setAutowired(true);

$container->compile();
$dumper = new PhpDumper($container);
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_WitherStaticReturnType']);
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_staticreturntype.php', $dump);
eval('?>'.$dump);

$container = new \Symfony_DI_PhpDumper_Service_WitherStaticReturnType();

$wither = $container->get('wither');
$this->assertInstanceOf(Foo::class, $wither->foo);
}

/**
* @group legacy
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

class WitherStaticReturnType
{
public $foo;

/**
* @required
*/
public function withFoo(Foo $foo): static
{
$new = clone $this;
$new->foo = $foo;

return $new;
}

/**
* @required
* @return $this
*/
public function setFoo(Foo $foo): static
{
$this->foo = $foo;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
*/
class Symfony_DI_PhpDumper_Service_WitherStaticReturnType extends Container
{
protected $parameters = [];

public function __construct()
{
$this->services = $this->privates = [];
$this->methodMap = [
'wither' => 'getWitherService',
];

$this->aliases = [];
}

public function compile(): void
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled(): bool
{
return true;
}

public function getRemovedIds(): array
{
return [
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true,
];
}

/**
* Gets the public 'wither' shared autowired service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\WitherStaticReturnType
*/
protected function getWitherService()
{
$instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\WitherStaticReturnType();

$a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();

$this->services['wither'] = $instance = $instance->withFoo($a);
$instance->setFoo($a);

return $instance;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.