Skip to content

Commit 47ca77a

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 ------- ca5ae1989e Replace @return annotation by return type in final classes
2 parents 3223fb3 + 1dd13ed commit 47ca77a

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

CompiledRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __serialize(): array
6666
/**
6767
* @internal
6868
*/
69-
final public function serialize()
69+
final public function serialize(): string
7070
{
7171
return serialize($this->__serialize());
7272
}

Loader/Configurator/CollectionConfigurator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ public function __destruct()
4747

4848
/**
4949
* Creates a sub-collection.
50-
*
51-
* @return self
5250
*/
53-
final public function collection($name = '')
51+
final public function collection($name = ''): self
5452
{
5553
return new self($this->collection, $this->name.$name, $this, $this->prefixes);
5654
}
@@ -62,7 +60,7 @@ final public function collection($name = '')
6260
*
6361
* @return $this
6462
*/
65-
final public function prefix($prefix)
63+
final public function prefix($prefix): object
6664
{
6765
if (\is_array($prefix)) {
6866
if (null === $this->parentPrefixes) {

Loader/Configurator/ImportConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __destruct()
4141
*
4242
* @return $this
4343
*/
44-
final public function prefix($prefix, bool $trailingSlashOnRoot = true)
44+
final public function prefix($prefix, bool $trailingSlashOnRoot = true): object
4545
{
4646
if (!\is_array($prefix)) {
4747
$this->route->addPrefix($prefix);
@@ -84,7 +84,7 @@ final public function prefix($prefix, bool $trailingSlashOnRoot = true)
8484
*
8585
* @return $this
8686
*/
87-
final public function namePrefix(string $namePrefix)
87+
final public function namePrefix(string $namePrefix): object
8888
{
8989
$this->route->addNamePrefix($namePrefix);
9090

Loader/Configurator/RoutingConfigurator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
3333
$this->file = $file;
3434
}
3535

36-
/**
37-
* @return ImportConfigurator
38-
*/
39-
final public function import($resource, $type = null, $ignoreErrors = false)
36+
final public function import($resource, $type = null, $ignoreErrors = false): ImportConfigurator
4037
{
4138
$this->loader->setCurrentDir(\dirname($this->path));
4239
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
@@ -52,10 +49,7 @@ final public function import($resource, $type = null, $ignoreErrors = false)
5249
return new ImportConfigurator($this->collection, $mergedCollection);
5350
}
5451

55-
/**
56-
* @return CollectionConfigurator
57-
*/
58-
final public function collection($name = '')
52+
final public function collection($name = ''): CollectionConfigurator
5953
{
6054
return new CollectionConfigurator($this->collection, $name);
6155
}

Loader/Configurator/Traits/RouteTrait.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait RouteTrait
2626
*
2727
* @return $this
2828
*/
29-
final public function defaults(array $defaults)
29+
final public function defaults(array $defaults): object
3030
{
3131
$this->route->addDefaults($defaults);
3232

@@ -38,7 +38,7 @@ final public function defaults(array $defaults)
3838
*
3939
* @return $this
4040
*/
41-
final public function requirements(array $requirements)
41+
final public function requirements(array $requirements): object
4242
{
4343
$this->route->addRequirements($requirements);
4444

@@ -50,7 +50,7 @@ final public function requirements(array $requirements)
5050
*
5151
* @return $this
5252
*/
53-
final public function options(array $options)
53+
final public function options(array $options): object
5454
{
5555
$this->route->addOptions($options);
5656

@@ -62,7 +62,7 @@ final public function options(array $options)
6262
*
6363
* @return $this
6464
*/
65-
final public function utf8(bool $utf8 = true)
65+
final public function utf8(bool $utf8 = true): object
6666
{
6767
$this->route->addOptions(['utf8' => $utf8]);
6868

@@ -74,7 +74,7 @@ final public function utf8(bool $utf8 = true)
7474
*
7575
* @return $this
7676
*/
77-
final public function condition(string $condition)
77+
final public function condition(string $condition): object
7878
{
7979
$this->route->setCondition($condition);
8080

@@ -86,7 +86,7 @@ final public function condition(string $condition)
8686
*
8787
* @return $this
8888
*/
89-
final public function host(string $pattern)
89+
final public function host(string $pattern): object
9090
{
9191
$this->route->setHost($pattern);
9292

@@ -101,7 +101,7 @@ final public function host(string $pattern)
101101
*
102102
* @return $this
103103
*/
104-
final public function schemes(array $schemes)
104+
final public function schemes(array $schemes): object
105105
{
106106
$this->route->setSchemes($schemes);
107107

@@ -116,7 +116,7 @@ final public function schemes(array $schemes)
116116
*
117117
* @return $this
118118
*/
119-
final public function methods(array $methods)
119+
final public function methods(array $methods): object
120120
{
121121
$this->route->setMethods($methods);
122122

@@ -130,7 +130,7 @@ final public function methods(array $methods)
130130
*
131131
* @return $this
132132
*/
133-
final public function controller($controller)
133+
final public function controller($controller): object
134134
{
135135
$this->route->addDefaults(['_controller' => $controller]);
136136

@@ -142,7 +142,7 @@ final public function controller($controller)
142142
*
143143
* @return $this
144144
*/
145-
final public function locale(string $locale)
145+
final public function locale(string $locale): object
146146
{
147147
$this->route->addDefaults(['_locale' => $locale]);
148148

@@ -154,7 +154,7 @@ final public function locale(string $locale)
154154
*
155155
* @return $this
156156
*/
157-
final public function format(string $format)
157+
final public function format(string $format): object
158158
{
159159
$this->route->addDefaults(['_format' => $format]);
160160

Route.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __serialize(): array
8080
/**
8181
* @internal
8282
*/
83-
final public function serialize()
83+
final public function serialize(): string
8484
{
8585
return serialize($this->__serialize());
8686
}

0 commit comments

Comments
 (0)