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

Skip to content

Commit 650b39a

Browse files
More short closures + isset instead of null checks + etc.
1 parent fd641de commit 650b39a

File tree

68 files changed

+345
-377
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

+345
-377
lines changed

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

+163-163
Large diffs are not rendered by default.

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());

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ public function setParameter(string $name, $value): void
16391639
16401640
public function getParameterBag(): ParameterBagInterface
16411641
{
1642-
if (null === $this->parameterBag) {
1642+
if (!isset($this->parameterBag)) {
16431643
$parameters = $this->parameters;
16441644
foreach ($this->loadedDynamicParameters as $name => $loaded) {
16451645
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class AbstractConfigurator
2727
public const FACTORY = 'unknown';
2828

2929
/**
30-
* @var callable(mixed, bool)|null
30+
* @var \Closure(mixed, bool):mixed|null
3131
*/
3232
public static $valuePreProcessor;
3333

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function setParameter(string $name, $value): void
7272

7373
public function getParameterBag(): ParameterBagInterface
7474
{
75-
if (null === $this->parameterBag) {
75+
if (!isset($this->parameterBag)) {
7676
$parameters = $this->parameters;
7777
foreach ($this->loadedDynamicParameters as $name => $loaded) {
7878
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function setParameter(string $name, $value): void
7272

7373
public function getParameterBag(): ParameterBagInterface
7474
{
75-
if (null === $this->parameterBag) {
75+
if (!isset($this->parameterBag)) {
7676
$parameters = $this->parameters;
7777
foreach ($this->loadedDynamicParameters as $name => $loaded) {
7878
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function setParameter(string $name, $value): void
8787

8888
public function getParameterBag(): ParameterBagInterface
8989
{
90-
if (null === $this->parameterBag) {
90+
if (!isset($this->parameterBag)) {
9191
$parameters = $this->parameters;
9292
foreach ($this->loadedDynamicParameters as $name => $loaded) {
9393
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function setParameter(string $name, $value): void
8383

8484
public function getParameterBag(): ParameterBagInterface
8585
{
86-
if (null === $this->parameterBag) {
86+
if (!isset($this->parameterBag)) {
8787
$parameters = $this->parameters;
8888
foreach ($this->loadedDynamicParameters as $name => $loaded) {
8989
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function setParameter(string $name, $value): void
5959

6060
public function getParameterBag(): ParameterBagInterface
6161
{
62-
if (null === $this->parameterBag) {
62+
if (!isset($this->parameterBag)) {
6363
$parameters = $this->parameters;
6464
foreach ($this->loadedDynamicParameters as $name => $loaded) {
6565
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class ProjectServiceContainer extends Container
678678

679679
public function getParameterBag(): ParameterBagInterface
680680
{
681-
if (null === $this->parameterBag) {
681+
if (!isset($this->parameterBag)) {
682682
$parameters = $this->parameters;
683683
foreach ($this->loadedDynamicParameters as $name => $loaded) {
684684
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public function setParameter(string $name, $value): void
462462

463463
public function getParameterBag(): ParameterBagInterface
464464
{
465-
if (null === $this->parameterBag) {
465+
if (!isset($this->parameterBag)) {
466466
$parameters = $this->parameters;
467467
foreach ($this->loadedDynamicParameters as $name => $loaded) {
468468
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ class ProjectServiceContainer extends Container
521521

522522
public function getParameterBag(): ParameterBagInterface
523523
{
524-
if (null === $this->parameterBag) {
524+
if (!isset($this->parameterBag)) {
525525
$parameters = $this->parameters;
526526
foreach ($this->loadedDynamicParameters as $name => $loaded) {
527527
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class ProjectServiceContainer extends Container
114114

115115
public function getParameterBag(): ParameterBagInterface
116116
{
117-
if (null === $this->parameterBag) {
117+
if (!isset($this->parameterBag)) {
118118
$parameters = $this->parameters;
119119
foreach ($this->loadedDynamicParameters as $name => $loaded) {
120120
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function setParameter(string $name, $value): void
7676

7777
public function getParameterBag(): ParameterBagInterface
7878
{
79-
if (null === $this->parameterBag) {
79+
if (!isset($this->parameterBag)) {
8080
$parameters = $this->parameters;
8181
foreach ($this->loadedDynamicParameters as $name => $loaded) {
8282
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);

0 commit comments

Comments
 (0)