Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18793b7

Browse files
committed
feature #31996 [5.0] Add return types in final classes (dFayet)
This PR was merged into the 5.0-dev branch. Discussion ---------- [5.0] Add return types in final classes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes/no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | #31981 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This is the first step for the issue #31981 I have some questions: - ~I have not added type for methods with `@inheritdoc` annotation, should I?~ - ~Don't we want to type also functions without `@return` annotation? (still in `final` classes)~ - ~If yes is the answer of the previous one, do we also want the `void` return type?~ - ~I have also added the return type in the `DependencyInjection` PhpDumper, but is it also wanted? (if yes, I will clean a bit the code changed)~ - ~Should we update the documentation's code samples when they display `final` classes?~ Todo: - [x] Adjust the PR, following the answers of the questions - [x] Add return type also when there is no `@return`, or with `@inheritdoc` - [x] [src/Symfony/Component/Debug/ErrorHandler.php#L383](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Debug/ErrorHandler.php#L383) `@return` annotation is not correct according to the return, investigate and adjust if needed - [x] [src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadataFactory.php#L50](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadataFactory.php#L50) `@return` annotation is not correct according to the return, investigate and adjust if needed - [x] Do a PR on documentation to add return type on code snippets with final classes => unneeded as they were already typed Commits ------- ca5ae19 Replace @return annotation by return type in final classes
2 parents 2f9e69e + ca5ae19 commit 18793b7

File tree

147 files changed

+326
-398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+326
-398
lines changed

src/Symfony/Bridge/Doctrine/Test/TestRepositoryFactory.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class TestRepositoryFactory implements RepositoryFactory
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function getRepository(EntityManagerInterface $entityManager, $entityName)
32+
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
3333
{
3434
$repositoryHash = $this->getRepositoryHash($entityManager, $entityName);
3535

@@ -47,10 +47,7 @@ public function setRepository(EntityManagerInterface $entityManager, $entityName
4747
$this->repositoryList[$repositoryHash] = $repository;
4848
}
4949

50-
/**
51-
* @return ObjectRepository
52-
*/
53-
private function createRepository(EntityManagerInterface $entityManager, $entityName)
50+
private function createRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
5451
{
5552
/* @var $metadata ClassMetadata */
5653
$metadata = $entityManager->getClassMetadata($entityName);
@@ -59,7 +56,7 @@ private function createRepository(EntityManagerInterface $entityManager, $entity
5956
return new $repositoryClassName($entityManager, $metadata);
6057
}
6158

62-
private function getRepositoryHash(EntityManagerInterface $entityManager, $entityName)
59+
private function getRepositoryHash(EntityManagerInterface $entityManager, $entityName): string
6360
{
6461
return $entityManager->getClassMetadata($entityName)->getName().spl_object_hash($entityManager);
6562
}

src/Symfony/Bridge/Monolog/Handler/ChromePhpHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function sendHeader($header, $content)
7373
/**
7474
* Override default behavior since we check it in onKernelResponse.
7575
*/
76-
protected function headersAccepted()
76+
protected function headersAccepted(): bool
7777
{
7878
return true;
7979
}

src/Symfony/Bridge/Monolog/Handler/FirePHPHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function sendHeader($header, $content)
7575
/**
7676
* Override default behavior since we check the user agent in onKernelResponse.
7777
*/
78-
protected function headersAccepted()
78+
protected function headersAccepted(): bool
7979
{
8080
return true;
8181
}

src/Symfony/Bridge/Monolog/Processor/RouteProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(bool $includeParams = true)
3535
$this->reset();
3636
}
3737

38-
public function __invoke(array $records)
38+
public function __invoke(array $records): array
3939
{
4040
if ($this->routeData && !isset($records['extra']['requests'])) {
4141
$records['extra']['requests'] = array_values($this->routeData);
@@ -78,7 +78,7 @@ public function removeRouteData(FinishRequestEvent $event)
7878
unset($this->routeData[$requestId]);
7979
}
8080

81-
public static function getSubscribedEvents()
81+
public static function getSubscribedEvents(): array
8282
{
8383
return [
8484
KernelEvents::REQUEST => ['addRouteData', 1],

src/Symfony/Bridge/Monolog/Processor/WebProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function onKernelRequest(ResponseEvent $event)
3939
}
4040
}
4141

42-
public static function getSubscribedEvents()
42+
public static function getSubscribedEvents(): array
4343
{
4444
return [
4545
KernelEvents::REQUEST => ['onKernelRequest', 4096],

src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function __construct(string $salt = '')
4040
/**
4141
* {@inheritdoc}
4242
*/
43-
public function isProxyCandidate(Definition $definition)
43+
public function isProxyCandidate(Definition $definition): bool
4444
{
4545
return ($definition->isLazy() || $definition->hasTag('proxy')) && $this->proxyGenerator->getProxifiedClass($definition);
4646
}
4747

4848
/**
4949
* {@inheritdoc}
5050
*/
51-
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null)
51+
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null): string
5252
{
5353
$instantiation = 'return';
5454

@@ -82,7 +82,7 @@ public function getProxyFactoryCode(Definition $definition, $id, $factoryCode =
8282
/**
8383
* {@inheritdoc}
8484
*/
85-
public function getProxyCode(Definition $definition)
85+
public function getProxyCode(Definition $definition): string
8686
{
8787
$code = $this->classGenerator->generate($this->generateProxyClass($definition));
8888

src/Symfony/Bridge/Twig/Extension/RoutingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getUrl($name, $parameters = [], $schemeRelative = false)
9393
*
9494
* @final
9595
*/
96-
public function isUrlGenerationSafe(Node $argsNode)
96+
public function isUrlGenerationSafe(Node $argsNode): array
9797
{
9898
// support named arguments
9999
$paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getTranslator(): ?TranslatorInterface
5858
/**
5959
* {@inheritdoc}
6060
*/
61-
public function getFilters()
61+
public function getFilters(): array
6262
{
6363
return [
6464
new TwigFilter('trans', [$this, 'trans']),
@@ -70,7 +70,7 @@ public function getFilters()
7070
*
7171
* @return AbstractTokenParser[]
7272
*/
73-
public function getTokenParsers()
73+
public function getTokenParsers(): array
7474
{
7575
return [
7676
// {% trans %}Symfony is great!{% endtrans %}
@@ -84,17 +84,17 @@ public function getTokenParsers()
8484
/**
8585
* {@inheritdoc}
8686
*/
87-
public function getNodeVisitors()
87+
public function getNodeVisitors(): array
8888
{
8989
return [$this->getTranslationNodeVisitor(), new TranslationDefaultDomainNodeVisitor()];
9090
}
9191

92-
public function getTranslationNodeVisitor()
92+
public function getTranslationNodeVisitor(): TranslationNodeVisitor
9393
{
9494
return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor();
9595
}
9696

97-
public function trans($message, array $arguments = [], $domain = null, $locale = null, $count = null)
97+
public function trans($message, array $arguments = [], $domain = null, $locale = null, $count = null): string
9898
{
9999
if (null !== $count) {
100100
$arguments['%count%'] = $count;
@@ -106,7 +106,7 @@ public function trans($message, array $arguments = [], $domain = null, $locale =
106106
/**
107107
* {@inheritdoc}
108108
*/
109-
public function getName()
109+
public function getName(): string
110110
{
111111
return 'translator';
112112
}

src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function attach(string $file, string $name = null, string $contentType =
6565
/**
6666
* @return $this
6767
*/
68-
public function setSubject(string $subject)
68+
public function setSubject(string $subject): object
6969
{
7070
$this->message->subject($subject);
7171

@@ -80,7 +80,7 @@ public function getSubject(): ?string
8080
/**
8181
* @return $this
8282
*/
83-
public function setReturnPath(string $address)
83+
public function setReturnPath(string $address): object
8484
{
8585
$this->message->returnPath($address);
8686

@@ -95,7 +95,7 @@ public function getReturnPath(): string
9595
/**
9696
* @return $this
9797
*/
98-
public function addFrom(string $address, string $name = null)
98+
public function addFrom(string $address, string $name = null): object
9999
{
100100
$this->message->addFrom($name ? new NamedAddress($address, $name) : new Address($address));
101101

@@ -113,7 +113,7 @@ public function getFrom(): array
113113
/**
114114
* @return $this
115115
*/
116-
public function addReplyTo(string $address)
116+
public function addReplyTo(string $address): object
117117
{
118118
$this->message->addReplyTo($address);
119119

@@ -131,7 +131,7 @@ public function getReplyTo(): array
131131
/**
132132
* @return $this
133133
*/
134-
public function addTo(string $address, string $name = null)
134+
public function addTo(string $address, string $name = null): object
135135
{
136136
$this->message->addTo($name ? new NamedAddress($address, $name) : new Address($address));
137137

@@ -149,7 +149,7 @@ public function getTo(): array
149149
/**
150150
* @return $this
151151
*/
152-
public function addCc(string $address, string $name = null)
152+
public function addCc(string $address, string $name = null): object
153153
{
154154
$this->message->addCc($name ? new NamedAddress($address, $name) : new Address($address));
155155

@@ -167,7 +167,7 @@ public function getCc(): array
167167
/**
168168
* @return $this
169169
*/
170-
public function addBcc(string $address, string $name = null)
170+
public function addBcc(string $address, string $name = null): object
171171
{
172172
$this->message->addBcc($name ? new NamedAddress($address, $name) : new Address($address));
173173

@@ -185,7 +185,7 @@ public function getBcc(): array
185185
/**
186186
* @return $this
187187
*/
188-
public function setPriority(int $priority)
188+
public function setPriority(int $priority): object
189189
{
190190
$this->message->setPriority($priority);
191191

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public function warmUp($cacheDir)
5757
*
5858
* @return bool always true
5959
*/
60-
public function isOptional()
60+
public function isOptional(): bool
6161
{
6262
return true;
6363
}
6464

6565
/**
6666
* {@inheritdoc}
6767
*/
68-
public static function getSubscribedServices()
68+
public static function getSubscribedServices(): array
6969
{
7070
return [
7171
'router' => RouterInterface::class,

src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private function hardCopy(string $originDir, string $targetDir): string
258258
return self::METHOD_COPY;
259259
}
260260

261-
private function getPublicDirectory(ContainerInterface $container)
261+
private function getPublicDirectory(ContainerInterface $container): string
262262
{
263263
$defaultPublicDir = 'public';
264264

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,9 @@ protected function validateInput(InputInterface $input)
208208
/**
209209
* Loads the ContainerBuilder from the cache.
210210
*
211-
* @return ContainerBuilder
212-
*
213211
* @throws \LogicException
214212
*/
215-
protected function getContainerBuilder()
213+
protected function getContainerBuilder(): ContainerBuilder
216214
{
217215
if ($this->containerBuilder) {
218216
return $this->containerBuilder;

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class ControllerResolver extends ContainerControllerResolver
2424
/**
2525
* {@inheritdoc}
2626
*/
27-
protected function instantiateController($class)
27+
protected function instantiateController($class): object
2828
{
2929
return $this->configureController(parent::instantiateController($class), $class);
3030
}
3131

32-
private function configureController($controller, string $class)
32+
private function configureController($controller, string $class): object
3333
{
3434
if ($controller instanceof ContainerAwareInterface) {
3535
$controller->setContainer($this->container);

src/Symfony/Bundle/SecurityBundle/Command/UserPasswordEncoderCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function createPasswordQuestion(): Question
180180
})->setHidden(true)->setMaxAttempts(20);
181181
}
182182

183-
private function generateSalt()
183+
private function generateSalt(): string
184184
{
185185
return base64_encode(random_bytes(30));
186186
}

src/Symfony/Bundle/SecurityBundle/Debug/WrappedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __call($method, $arguments)
5454
return $this->listener->{$method}(...$arguments);
5555
}
5656

57-
public function getWrappedListener()
57+
public function getWrappedListener(): callable
5858
{
5959
return $this->listener;
6060
}

src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(Environment $twig, bool $interceptRedirects = false,
5555
$this->cspHandler = $cspHandler;
5656
}
5757

58-
public function isEnabled()
58+
public function isEnabled(): bool
5959
{
6060
return self::DISABLED !== $this->mode;
6161
}
@@ -136,7 +136,7 @@ protected function injectToolbar(Response $response, Request $request, array $no
136136
}
137137
}
138138

139-
public static function getSubscribedEvents()
139+
public static function getSubscribedEvents(): array
140140
{
141141
return [
142142
KernelEvents::RESPONSE => ['onKernelResponse', -128],

src/Symfony/Component/BrowserKit/HttpBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(HttpClientInterface $client = null, History $history
4141
parent::__construct([], $history, $cookieJar);
4242
}
4343

44-
protected function doRequest($request)
44+
protected function doRequest($request): Response
4545
{
4646
$headers = $this->getHeaders($request);
4747
[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request);

src/Symfony/Component/BrowserKit/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(string $content = '', int $status = 200, array $head
4040
*
4141
* @return string The response with headers and content
4242
*/
43-
public function __toString()
43+
public function __toString(): string
4444
{
4545
$headers = '';
4646
foreach ($this->headers as $name => $value) {
@@ -61,7 +61,7 @@ public function __toString()
6161
*
6262
* @return string The response content
6363
*/
64-
public function getContent()
64+
public function getContent(): string
6565
{
6666
return $this->content;
6767
}
@@ -76,7 +76,7 @@ public function getStatusCode(): int
7676
*
7777
* @return array The response headers
7878
*/
79-
public function getHeaders()
79+
public function getHeaders(): array
8080
{
8181
return $this->headers;
8282
}

src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function filterResponse($response)
5959
return $response;
6060
}
6161

62-
protected function doRequest($request)
62+
protected function doRequest($request): Response
6363
{
6464
$response = parent::doRequest($request);
6565

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class CacheItem implements ItemInterface
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function getKey()
40+
public function getKey(): string
4141
{
4242
return $this->key;
4343
}
@@ -53,7 +53,7 @@ public function get()
5353
/**
5454
* {@inheritdoc}
5555
*/
56-
public function isHit()
56+
public function isHit(): bool
5757
{
5858
return $this->isHit;
5959
}
@@ -145,11 +145,9 @@ public function getMetadata(): array
145145
*
146146
* @param string $key The key to validate
147147
*
148-
* @return string
149-
*
150148
* @throws InvalidArgumentException When $key is not valid
151149
*/
152-
public static function validateKey($key)
150+
public static function validateKey($key): string
153151
{
154152
if (!\is_string($key)) {
155153
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', \is_object($key) ? \get_class($key) : \gettype($key)));

0 commit comments

Comments
 (0)