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

Skip to content

More short closures + isset instead of null checks + etc. #51121

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 1 commit into from
Jul 27, 2023
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
24 changes: 12 additions & 12 deletions .github/expected-missing-return-types.diff
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ diff --git a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php b/src/Symfony/
diff --git a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php b/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
--- a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
+++ b/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
@@ -71,5 +71,5 @@ class MemcachedAdapter extends AbstractAdapter
@@ -72,5 +72,5 @@ class MemcachedAdapter extends AbstractAdapter
* @return bool
*/
- public static function isSupported()
Expand All @@ -1353,7 +1353,7 @@ diff --git a/src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php b/src/Symf
diff --git a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
--- a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
+++ b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php
@@ -101,5 +101,5 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
@@ -102,5 +102,5 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
* @throws \DomainException When an unsupported PDO driver is used
*/
- public function createTable()
Expand Down Expand Up @@ -1505,7 +1505,7 @@ diff --git a/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php b/src/
+ protected function doUnlink(string $file): bool
{
return @unlink($file);
@@ -175,5 +175,5 @@ trait FilesystemCommonTrait
@@ -167,5 +167,5 @@ trait FilesystemCommonTrait
* @return void
*/
- public function __wakeup()
Expand Down Expand Up @@ -7313,14 +7313,14 @@ diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Comp
- public function setMethod(string $method)
+ public function setMethod(string $method): void
{
$this->method = null;
unset($this->method);
@@ -1301,5 +1301,5 @@ class Request
* @return void
*/
- public function setFormat(?string $format, string|array $mimeTypes)
+ public function setFormat(?string $format, string|array $mimeTypes): void
{
if (null === static::$formats) {
if (!isset(static::$formats)) {
@@ -1333,5 +1333,5 @@ class Request
* @return void
*/
Expand All @@ -7342,14 +7342,14 @@ diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Comp
+ public function setLocale(string $locale): void
{
$this->setPhpDefaultLocale($this->locale = $locale);
@@ -1756,5 +1756,5 @@ class Request
@@ -1744,5 +1744,5 @@ class Request
* @return string
*/
- protected function prepareRequestUri()
+ protected function prepareRequestUri(): string
{
$requestUri = '';
@@ -1927,5 +1927,5 @@ class Request
@@ -1915,5 +1915,5 @@ class Request
* @return void
*/
- protected static function initializeFormats()
Expand Down Expand Up @@ -11059,14 +11059,14 @@ diff --git a/src/Symfony/Component/Security/Core/Exception/AccountStatusExceptio
diff --git a/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php b/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
--- a/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
+++ b/src/Symfony/Component/Security/Core/Exception/AuthenticationException.php
@@ -43,5 +43,5 @@ class AuthenticationException extends RuntimeException
@@ -34,5 +34,5 @@ class AuthenticationException extends RuntimeException
* @return void
*/
- public function setToken(TokenInterface $token)
+ public function setToken(TokenInterface $token): void
{
$this->token = $token;
@@ -94,5 +94,5 @@ class AuthenticationException extends RuntimeException
@@ -85,5 +85,5 @@ class AuthenticationException extends RuntimeException
* @return string
*/
- public function getMessageKey()
Expand Down Expand Up @@ -11778,7 +11778,7 @@ diff --git a/src/Symfony/Component/String/Slugger/AsciiSlugger.php b/src/Symfony
diff --git a/src/Symfony/Component/String/UnicodeString.php b/src/Symfony/Component/String/UnicodeString.php
--- a/src/Symfony/Component/String/UnicodeString.php
+++ b/src/Symfony/Component/String/UnicodeString.php
@@ -342,5 +342,5 @@ class UnicodeString extends AbstractUnicodeString
@@ -366,5 +366,5 @@ class UnicodeString extends AbstractUnicodeString
* @return void
*/
- public function __wakeup()
Expand Down Expand Up @@ -12075,14 +12075,14 @@ diff --git a/src/Symfony/Component/Translation/Dumper/DumperInterface.php b/src/
diff --git a/src/Symfony/Component/Translation/Dumper/FileDumper.php b/src/Symfony/Component/Translation/Dumper/FileDumper.php
--- a/src/Symfony/Component/Translation/Dumper/FileDumper.php
+++ b/src/Symfony/Component/Translation/Dumper/FileDumper.php
@@ -40,5 +40,5 @@ abstract class FileDumper implements DumperInterface
@@ -38,5 +38,5 @@ abstract class FileDumper implements DumperInterface
* @return void
*/
- public function setRelativePathTemplate(string $relativePathTemplate)
+ public function setRelativePathTemplate(string $relativePathTemplate): void
{
$this->relativePathTemplate = $relativePathTemplate;
@@ -48,5 +48,5 @@ abstract class FileDumper implements DumperInterface
@@ -46,5 +46,5 @@ abstract class FileDumper implements DumperInterface
* @return void
*/
- public function dump(MessageCatalogue $messages, array $options = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
use Symfony\Component\WebLink\GenericLinkProvider;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\WebLink\Link;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Environment;
Expand All @@ -64,7 +63,7 @@ abstract class AbstractController implements ServiceSubscriberInterface
#[Required]
public function setContainer(ContainerInterface $container): ?ContainerInterface
{
$previous = $this->container;
$previous = $this->container ?? null;
$this->container = $container;

return $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(ContainerInterface $container, mixed $resource, arra

public function getRouteCollection(): RouteCollection
{
if (null === $this->collection) {
if (!isset($this->collection)) {
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
$this->resolveParameters($this->collection);
$this->collection->addResource(new ContainerParametersResource($this->collectedParameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ public function addConfiguration(NodeDefinition $node): void

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

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

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

->children()
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/BrowserKit/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class Cookie
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)
{
if ($encodedValue) {
$this->value = urldecode($value);
$this->rawValue = $value;
$this->rawValue = $value ?? '';
$this->value = urldecode($this->rawValue);
} else {
$this->value = $value;
$this->rawValue = rawurlencode($value ?? '');
$this->value = $value ?? '';
$this->rawValue = rawurlencode($this->value);
}
$this->name = $name;
$this->path = empty($path) ? '/' : $path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
throw new CacheException('Couchbase >= 2.6.0 < 3.0.0 is required.');
}

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

$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\?]+))(?:\?(?<options>.*))?$/i';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
throw new CacheException('Couchbase >= 3.0.0 < 4.0.0 is required.');
}

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

$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))'
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class DoctrineDbalAdapter extends AbstractAdapter implements PruneableInterface
{
protected $maxIdLength = 255;
private const MAX_KEY_LENGTH = 255;

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

$this->maxIdLength = self::MAX_KEY_LENGTH;
$this->table = $options['db_table'] ?? $this->table;
$this->idCol = $options['db_id_col'] ?? $this->idCol;
$this->dataCol = $options['db_data_col'] ?? $this->dataCol;
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class MemcachedAdapter extends AbstractAdapter
*/
private const RESERVED_MEMCACHED = " \n\r\t\v\f\0";
private const RESERVED_PSR6 = '@()\{}/';

protected $maxIdLength = 250;
private const MAX_KEY_LENGTH = 250;

private MarshallerInterface $marshaller;
private \Memcached $client;
Expand All @@ -51,6 +50,8 @@ public function __construct(\Memcached $client, string $namespace = '', int $def
if (!static::isSupported()) {
throw new CacheException('Memcached > 3.1.5 is required.');
}
$this->maxIdLength = self::MAX_KEY_LENGTH;

if ('Memcached' === $client::class) {
$opt = $client->getOption(\Memcached::OPT_SERIALIZER);
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
Expand Down Expand Up @@ -96,7 +97,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
if (!static::isSupported()) {
throw new CacheException('Memcached > 3.1.5 is required.');
}
set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
try {
$client = new \Memcached($options['persistent_id'] ?? null);
$username = $options['username'] ?? null;
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class PdoAdapter extends AbstractAdapter implements PruneableInterface
{
protected $maxIdLength = 255;
private const MAX_KEY_LENGTH = 255;

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

$this->maxIdLength = self::MAX_KEY_LENGTH;
$this->table = $options['db_table'] ?? $this->table;
$this->idCol = $options['db_id_col'] ?? $this->idCol;
$this->dataCol = $options['db_data_col'] ?? $this->dataCol;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
}

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

while (true) {
try {
Expand Down
10 changes: 1 addition & 9 deletions src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function doUnlink(string $file)

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

/**
* @internal
*/
public static function throwError(int $type, string $message, string $file, int $line): never
{
throw new \ErrorException($message, 0, $type, $file, $line);
}

public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ protected function getNodeBuilder(): NodeBuilder

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

$this->validateConcreteNode($node);
Expand Down Expand Up @@ -382,7 +382,7 @@ protected function createNode(): NodeInterface

if (false !== $this->addDefaultChildren) {
$node->setAddChildrenIfNoneSet($this->addDefaultChildren);
if ($this->prototype instanceof static && null === $this->prototype->prototype) {
if ($this->prototype instanceof static && !isset($this->prototype->prototype)) {
$this->prototype->addDefaultsIfNotSet();
}
}
Expand All @@ -404,18 +404,18 @@ protected function createNode(): NodeInterface
$node->setDeprecated($this->deprecation['package'], $this->deprecation['version'], $this->deprecation['message']);
}

if (null !== $this->normalization) {
if (isset($this->normalization)) {
$node->setNormalizationClosures($this->normalization->before);
$node->setNormalizedTypes($this->normalization->declaredTypes);
$node->setXmlRemappings($this->normalization->remappings);
}

if (null !== $this->merge) {
if (isset($this->merge)) {
$node->setAllowOverwrite($this->merge->allowOverwrite);
$node->setAllowFalse($this->merge->allowFalse);
}

if (null !== $this->validation) {
if (isset($this->validation)) {
$node->setFinalValidationClosures($this->validation->rules);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getNode(bool $forceRootNode = false): NodeInterface
$this->parent = null;
}

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

if (null !== $this->validation) {
if (isset($this->validation)) {
$this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function buildTree(): NodeInterface
public function setPathSeparator(string $separator)
{
// unset last built as changing path separator changes all nodes
$this->tree = null;
unset($this->tree);

$this->root->setPathSeparator($separator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ protected function createNode(): NodeInterface
{
$node = $this->instantiateNode();

if (null !== $this->normalization) {
if (isset($this->normalization)) {
$node->setNormalizationClosures($this->normalization->before);
}

if (null !== $this->merge) {
if (isset($this->merge)) {
$node->setAllowOverwrite($this->merge->allowOverwrite);
}

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

if (null !== $this->validation) {
if (isset($this->validation)) {
$node->setFinalValidationClosures($this->validation->rules);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function testEndThenPartNotSpecified()
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You must specify a then part.');
$builder = $this->getTestBuilder();
$builder->ifPart = 'test';
$builder->ifPart = static fn () => false;
$builder->end();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ private function set(string $type, string $id): void

private function createTypeNotFoundMessageCallback(TypedReference $reference, string $label): \Closure
{
if (null === $this->typesClone->container) {
if (!isset($this->typesClone->container)) {
$this->typesClone->container = new ContainerBuilder($this->container->getParameterBag());
$this->typesClone->container->setAliases($this->container->getAliases());
$this->typesClone->container->setDefinitions($this->container->getDefinitions());
Expand Down
Loading