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

Skip to content

Commit 1935af6

Browse files
minor #51121 More short closures + isset instead of null checks + etc. (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- More short closures + isset instead of null checks + etc. | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Some of the isset() checks are needed to make #51069 green on high-deps job. Commits ------- 3df6471 More short closures + isset instead of null checks + etc.
2 parents 7efa075 + 3df6471 commit 1935af6

File tree

68 files changed

+194
-226
lines changed

Some content is hidden

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

68 files changed

+194
-226
lines changed

.github/expected-missing-return-types.diff

+12-12
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ diff --git a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php b/src/Symfony/
13431343
diff --git a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php b/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
13441344
--- a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
13451345
+++ b/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
1346-
@@ -71,5 +71,5 @@ class MemcachedAdapter extends AbstractAdapter
1346+
@@ -72,5 +72,5 @@ class MemcachedAdapter extends AbstractAdapter
13471347
* @return bool
13481348
*/
13491349
- public static function isSupported()
@@ -1353,7 +1353,7 @@ diff --git a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php b/src/Symf
13531353
diff --git a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
13541354
--- a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
13551355
+++ b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
1356-
@@ -101,5 +101,5 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
1356+
@@ -102,5 +102,5 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
13571357
* @throws \DomainException When an unsupported PDO driver is used
13581358
*/
13591359
- public function createTable()
@@ -1505,7 +1505,7 @@ diff --git a/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php b/src/
15051505
+ protected function doUnlink(string $file): bool
15061506
{
15071507
return @unlink($file);
1508-
@@ -175,5 +175,5 @@ trait FilesystemCommonTrait
1508+
@@ -167,5 +167,5 @@ trait FilesystemCommonTrait
15091509
* @return void
15101510
*/
15111511
- public function __wakeup()
@@ -7313,14 +7313,14 @@ diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Comp
73137313
- public function setMethod(string $method)
73147314
+ public function setMethod(string $method): void
73157315
{
7316-
$this->method = null;
7316+
unset($this->method);
73177317
@@ -1301,5 +1301,5 @@ class Request
73187318
* @return void
73197319
*/
73207320
- public function setFormat(?string $format, string|array $mimeTypes)
73217321
+ public function setFormat(?string $format, string|array $mimeTypes): void
73227322
{
7323-
if (null === static::$formats) {
7323+
if (!isset(static::$formats)) {
73247324
@@ -1333,5 +1333,5 @@ class Request
73257325
* @return void
73267326
*/
@@ -7342,14 +7342,14 @@ diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Comp
73427342
+ public function setLocale(string $locale): void
73437343
{
73447344
$this->setPhpDefaultLocale($this->locale = $locale);
7345-
@@ -1756,5 +1756,5 @@ class Request
7345+
@@ -1744,5 +1744,5 @@ class Request
73467346
* @return string
73477347
*/
73487348
- protected function prepareRequestUri()
73497349
+ protected function prepareRequestUri(): string
73507350
{
73517351
$requestUri = '';
7352-
@@ -1927,5 +1927,5 @@ class Request
7352+
@@ -1915,5 +1915,5 @@ class Request
73537353
* @return void
73547354
*/
73557355
- protected static function initializeFormats()
@@ -11059,14 +11059,14 @@ diff --git a/src/Symfony/Component/Security/Core/Exception/AccountStatusExceptio
1105911059
diff --git a/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php b/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
1106011060
--- a/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
1106111061
+++ b/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
11062-
@@ -43,5 +43,5 @@ class AuthenticationException extends RuntimeException
11062+
@@ -34,5 +34,5 @@ class AuthenticationException extends RuntimeException
1106311063
* @return void
1106411064
*/
1106511065
- public function setToken(TokenInterface $token)
1106611066
+ public function setToken(TokenInterface $token): void
1106711067
{
1106811068
$this->token = $token;
11069-
@@ -94,5 +94,5 @@ class AuthenticationException extends RuntimeException
11069+
@@ -85,5 +85,5 @@ class AuthenticationException extends RuntimeException
1107011070
* @return string
1107111071
*/
1107211072
- public function getMessageKey()
@@ -11778,7 +11778,7 @@ diff --git a/src/Symfony/Component/String/Slugger/AsciiSlugger.php b/src/Symfony
1177811778
diff --git a/src/Symfony/Component/String/UnicodeString.php b/src/Symfony/Component/String/UnicodeString.php
1177911779
--- a/src/Symfony/Component/String/UnicodeString.php
1178011780
+++ b/src/Symfony/Component/String/UnicodeString.php
11781-
@@ -342,5 +342,5 @@ class UnicodeString extends AbstractUnicodeString
11781+
@@ -366,5 +366,5 @@ class UnicodeString extends AbstractUnicodeString
1178211782
* @return void
1178311783
*/
1178411784
- public function __wakeup()
@@ -12075,14 +12075,14 @@ diff --git a/src/Symfony/Component/Translation/Dumper/DumperInterface.php b/src/
1207512075
diff --git a/src/Symfony/Component/Translation/Dumper/FileDumper.php b/src/Symfony/Component/Translation/Dumper/FileDumper.php
1207612076
--- a/src/Symfony/Component/Translation/Dumper/FileDumper.php
1207712077
+++ b/src/Symfony/Component/Translation/Dumper/FileDumper.php
12078-
@@ -40,5 +40,5 @@ abstract class FileDumper implements DumperInterface
12078+
@@ -38,5 +38,5 @@ abstract class FileDumper implements DumperInterface
1207912079
* @return void
1208012080
*/
1208112081
- public function setRelativePathTemplate(string $relativePathTemplate)
1208212082
+ public function setRelativePathTemplate(string $relativePathTemplate): void
1208312083
{
1208412084
$this->relativePathTemplate = $relativePathTemplate;
12085-
@@ -48,5 +48,5 @@ abstract class FileDumper implements DumperInterface
12085+
@@ -46,5 +46,5 @@ abstract class FileDumper implements DumperInterface
1208612086
* @return void
1208712087
*/
1208812088
- public function dump(MessageCatalogue $messages, array $options = [])

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
4545
use Symfony\Component\WebLink\GenericLinkProvider;
4646
use Symfony\Component\WebLink\HttpHeaderSerializer;
47-
use Symfony\Component\WebLink\Link;
4847
use Symfony\Contracts\Service\Attribute\Required;
4948
use Symfony\Contracts\Service\ServiceSubscriberInterface;
5049
use Twig\Environment;
@@ -64,7 +63,7 @@ abstract class AbstractController implements ServiceSubscriberInterface
6463
#[Required]
6564
public function setContainer(ContainerInterface $container): ?ContainerInterface
6665
{
67-
$previous = $this->container;
66+
$previous = $this->container ?? null;
6867
$this->container = $container;
6968

7069
return $previous;

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(ContainerInterface $container, mixed $resource, arra
6161

6262
public function getRouteCollection(): RouteCollection
6363
{
64-
if (null === $this->collection) {
64+
if (!isset($this->collection)) {
6565
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
6666
$this->resolveParameters($this->collection);
6767
$this->collection->addResource(new ContainerParametersResource($this->collectedParameters));

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ public function addConfiguration(NodeDefinition $node): void
6868

6969
->beforeNormalization()
7070
->ifString()
71-
->then(static function (string $v): array { return ['id' => $v]; })
71+
->then(static fn ($v) => ['id' => $v])
7272
->end()
7373

7474
->beforeNormalization()
75-
->ifTrue(static function ($v) { return \is_array($v) && 1 < \count($v); })
76-
->then(static function () { throw new InvalidConfigurationException('You cannot configure multiple token handlers.'); })
75+
->ifTrue(static fn ($v) => \is_array($v) && 1 < \count($v))
76+
->then(static fn () => throw new InvalidConfigurationException('You cannot configure multiple token handlers.'))
7777
->end()
7878

7979
// "isRequired" must be set otherwise the following custom validation is not called
8080
->isRequired()
8181
->beforeNormalization()
82-
->ifTrue(static function ($v) { return \is_array($v) && !$v; })
83-
->then(static function () { throw new InvalidConfigurationException('You must set a token handler.'); })
82+
->ifTrue(static fn ($v) => \is_array($v) && !$v)
83+
->then(static fn () => throw new InvalidConfigurationException('You must set a token handler.'))
8484
->end()
8585

8686
->children()

src/Symfony/Component/BrowserKit/Cookie.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class Cookie
6161
public function __construct(string $name, ?string $value, string $expires = null, string $path = null, string $domain = '', bool $secure = false, bool $httponly = true, bool $encodedValue = false, string $samesite = null)
6262
{
6363
if ($encodedValue) {
64-
$this->value = urldecode($value);
65-
$this->rawValue = $value;
64+
$this->rawValue = $value ?? '';
65+
$this->value = urldecode($this->rawValue);
6666
} else {
67-
$this->value = $value;
68-
$this->rawValue = rawurlencode($value ?? '');
67+
$this->value = $value ?? '';
68+
$this->rawValue = rawurlencode($this->value);
6969
}
7070
$this->name = $name;
7171
$this->path = empty($path) ? '/' : $path;

src/Symfony/Component/Cache/Adapter/CouchbaseBucketAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
6464
throw new CacheException('Couchbase >= 2.6.0 < 3.0.0 is required.');
6565
}
6666

67-
set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
67+
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
6868

6969
$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
7070
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\?]+))(?:\?(?<options>.*))?$/i';

src/Symfony/Component/Cache/Adapter/CouchbaseCollectionAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
5757
throw new CacheException('Couchbase >= 3.0.0 < 4.0.0 is required.');
5858
}
5959

60-
set_error_handler(function ($type, $msg, $file, $line): bool { throw new \ErrorException($msg, 0, $type, $file, $line); });
60+
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
6161

6262
$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
6363
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))'

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
class DoctrineDbalAdapter extends AbstractAdapter implements PruneableInterface
3131
{
32-
protected $maxIdLength = 255;
32+
private const MAX_KEY_LENGTH = 255;
3333

3434
private MarshallerInterface $marshaller;
3535
private Connection $conn;
@@ -94,6 +94,7 @@ public function __construct(Connection|string $connOrDsn, string $namespace = ''
9494
$this->conn = DriverManager::getConnection($params, $config);
9595
}
9696

97+
$this->maxIdLength = self::MAX_KEY_LENGTH;
9798
$this->table = $options['db_table'] ?? $this->table;
9899
$this->idCol = $options['db_id_col'] ?? $this->idCol;
99100
$this->dataCol = $options['db_data_col'] ?? $this->dataCol;

src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class MemcachedAdapter extends AbstractAdapter
2929
*/
3030
private const RESERVED_MEMCACHED = " \n\r\t\v\f\0";
3131
private const RESERVED_PSR6 = '@()\{}/';
32-
33-
protected $maxIdLength = 250;
32+
private const MAX_KEY_LENGTH = 250;
3433

3534
private MarshallerInterface $marshaller;
3635
private \Memcached $client;
@@ -51,6 +50,8 @@ public function __construct(\Memcached $client, string $namespace = '', int $def
5150
if (!static::isSupported()) {
5251
throw new CacheException('Memcached > 3.1.5 is required.');
5352
}
53+
$this->maxIdLength = self::MAX_KEY_LENGTH;
54+
5455
if ('Memcached' === $client::class) {
5556
$opt = $client->getOption(\Memcached::OPT_SERIALIZER);
5657
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
@@ -96,7 +97,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
9697
if (!static::isSupported()) {
9798
throw new CacheException('Memcached > 3.1.5 is required.');
9899
}
99-
set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
100+
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
100101
try {
101102
$client = new \Memcached($options['persistent_id'] ?? null);
102103
$username = $options['username'] ?? null;

src/Symfony/Component/Cache/Adapter/PdoAdapter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class PdoAdapter extends AbstractAdapter implements PruneableInterface
2121
{
22-
protected $maxIdLength = 255;
22+
private const MAX_KEY_LENGTH = 255;
2323

2424
private MarshallerInterface $marshaller;
2525
private \PDO|Connection $conn;
@@ -75,6 +75,7 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin
7575
$this->dsn = $connOrDsn;
7676
}
7777

78+
$this->maxIdLength = self::MAX_KEY_LENGTH;
7879
$this->table = $options['db_table'] ?? $this->table;
7980
$this->idCol = $options['db_id_col'] ?? $this->idCol;
8081
$this->dataCol = $options['db_data_col'] ?? $this->dataCol;

src/Symfony/Component/Cache/LockRegistry.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
9797
}
9898

9999
self::$signalingException ??= unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}");
100-
self::$signalingCallback ??= function () { throw self::$signalingException; };
100+
self::$signalingCallback ??= fn () => throw self::$signalingException;
101101

102102
while (true) {
103103
try {

src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function doUnlink(string $file)
8787

8888
private function write(string $file, string $data, int $expiresAt = null): bool
8989
{
90-
set_error_handler(self::throwError(...));
90+
set_error_handler(static fn ($type, $message, $file, $line) => throw new \ErrorException($message, 0, $type, $file, $line));
9191
try {
9292
$tmp = $this->directory.$this->tmpSuffix ??= str_replace('/', '-', base64_encode(random_bytes(6)));
9393
try {
@@ -158,14 +158,6 @@ private function scanHashDir(string $directory): \Generator
158158
}
159159
}
160160

161-
/**
162-
* @internal
163-
*/
164-
public static function throwError(int $type, string $message, string $file, int $line): never
165-
{
166-
throw new \ErrorException($message, 0, $type, $file, $line);
167-
}
168-
169161
public function __sleep(): array
170162
{
171163
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ protected function getNodeBuilder(): NodeBuilder
348348

349349
protected function createNode(): NodeInterface
350350
{
351-
if (null === $this->prototype) {
351+
if (!isset($this->prototype)) {
352352
$node = new ArrayNode($this->name, $this->parent, $this->pathSeparator);
353353

354354
$this->validateConcreteNode($node);
@@ -382,7 +382,7 @@ protected function createNode(): NodeInterface
382382

383383
if (false !== $this->addDefaultChildren) {
384384
$node->setAddChildrenIfNoneSet($this->addDefaultChildren);
385-
if ($this->prototype instanceof static && null === $this->prototype->prototype) {
385+
if ($this->prototype instanceof static && !isset($this->prototype->prototype)) {
386386
$this->prototype->addDefaultsIfNotSet();
387387
}
388388
}
@@ -404,18 +404,18 @@ protected function createNode(): NodeInterface
404404
$node->setDeprecated($this->deprecation['package'], $this->deprecation['version'], $this->deprecation['message']);
405405
}
406406

407-
if (null !== $this->normalization) {
407+
if (isset($this->normalization)) {
408408
$node->setNormalizationClosures($this->normalization->before);
409409
$node->setNormalizedTypes($this->normalization->declaredTypes);
410410
$node->setXmlRemappings($this->normalization->remappings);
411411
}
412412

413-
if (null !== $this->merge) {
413+
if (isset($this->merge)) {
414414
$node->setAllowOverwrite($this->merge->allowOverwrite);
415415
$node->setAllowFalse($this->merge->allowFalse);
416416
}
417417

418-
if (null !== $this->validation) {
418+
if (isset($this->validation)) {
419419
$node->setFinalValidationClosures($this->validation->rules);
420420
}
421421

src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getNode(bool $forceRootNode = false): NodeInterface
105105
$this->parent = null;
106106
}
107107

108-
if (null !== $this->normalization) {
108+
if (isset($this->normalization)) {
109109
$allowedTypes = [];
110110
foreach ($this->normalization->before as $expr) {
111111
$allowedTypes[] = $expr->allowedTypes;
@@ -115,7 +115,7 @@ public function getNode(bool $forceRootNode = false): NodeInterface
115115
$this->normalization->declaredTypes = $allowedTypes;
116116
}
117117

118-
if (null !== $this->validation) {
118+
if (isset($this->validation)) {
119119
$this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
120120
}
121121

src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function buildTree(): NodeInterface
5353
public function setPathSeparator(string $separator)
5454
{
5555
// unset last built as changing path separator changes all nodes
56-
$this->tree = null;
56+
unset($this->tree);
5757

5858
$this->root->setPathSeparator($separator);
5959
}

src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ protected function createNode(): NodeInterface
3333
{
3434
$node = $this->instantiateNode();
3535

36-
if (null !== $this->normalization) {
36+
if (isset($this->normalization)) {
3737
$node->setNormalizationClosures($this->normalization->before);
3838
}
3939

40-
if (null !== $this->merge) {
40+
if (isset($this->merge)) {
4141
$node->setAllowOverwrite($this->merge->allowOverwrite);
4242
}
4343

@@ -55,7 +55,7 @@ protected function createNode(): NodeInterface
5555
$node->setDeprecated($this->deprecation['package'], $this->deprecation['version'], $this->deprecation['message']);
5656
}
5757

58-
if (null !== $this->validation) {
58+
if (isset($this->validation)) {
5959
$node->setFinalValidationClosures($this->validation->rules);
6060
}
6161

src/Symfony/Component/Config/Tests/Definition/Builder/ExprBuilderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function testEndThenPartNotSpecified()
198198
$this->expectException(\RuntimeException::class);
199199
$this->expectExceptionMessage('You must specify a then part.');
200200
$builder = $this->getTestBuilder();
201-
$builder->ifPart = 'test';
201+
$builder->ifPart = static fn () => false;
202202
$builder->end();
203203
}
204204

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private function set(string $type, string $id): void
561561

562562
private function createTypeNotFoundMessageCallback(TypedReference $reference, string $label): \Closure
563563
{
564-
if (null === $this->typesClone->container) {
564+
if (!isset($this->typesClone->container)) {
565565
$this->typesClone->container = new ContainerBuilder($this->container->getParameterBag());
566566
$this->typesClone->container->setAliases($this->container->getAliases());
567567
$this->typesClone->container->setDefinitions($this->container->getDefinitions());

0 commit comments

Comments
 (0)