Skip to content

Commit 53e80b4

Browse files
committed
minor symfony#54447 Remove unnecessary empty usages (ostrolucky)
This PR was merged into the 7.1 branch. Discussion ---------- Remove unnecessary empty usages | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? |no | Deprecations? | no | Issues | | License | MIT I've written a rector for this rectorphp/rector-src#5783 and decided to apply it on Symfony src here. As for motivation for removing empty where possible, in my and several other prominent devs opinions, using `empty()` is a bad practice for several reasons: - it's misleading. It doesn't work with collection or stringable objects. Its meaning is more like `isset($foo) && (bool) $foo` rather than `count($foo) === 0` or `strlen($foo) === 0`, contrary to name of this function. - it decreases safety of the code, because it's doing implicit `isset` call, so once you remove variable assignment, `empty()` call that references it continues silently working - it increases complexity because of having to negate the call in most cases (there is no `nonempty()` function) - it's unnecessary in most cases and hence adds extra opcodes and increases length of code for no good reason There were also several articles written on this topic, I know about these 2: - https://www.contextualcode.com/Blog/php-micro-optimization.-variable-boolean-cast-vs-!empty - https://www.beberlei.de/2021/02/19/when_to_use_empty_in_php_i_say_never.html (this one is from `@beberlei`) However, personally I'm not in a super strict camp. I like that I don't have to explicitly specify `[] === $foo` or `'' === $foo` or `null === $foo` (and so on and on) and I think Symfony shares the similar opinion. Commits ------- ccc813c Remove unnecessary empty usages
2 parents 590972f + ccc813c commit 53e80b4

File tree

59 files changed

+84
-86
lines changed

Some content is hidden

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

59 files changed

+84
-86
lines changed

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function validate(mixed $entity, Constraint $constraint): void
116116

117117
// skip validation if there are no criteria (this can happen when the
118118
// "ignoreNull" option is enabled and fields to be checked are null
119-
if (empty($criteria)) {
119+
if (!$criteria) {
120120
return;
121121
}
122122

src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public function format(LogRecord $record): mixed
100100
{
101101
$record = $this->replacePlaceHolder($record);
102102

103-
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->context)) {
103+
if (!$this->options['ignore_empty_context_and_extra'] || $record->context) {
104104
$context = $record->context;
105105
$context = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($context);
106106
} else {
107107
$context = '';
108108
}
109109

110-
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->extra)) {
110+
if (!$this->options['ignore_empty_context_and_extra'] || $record->extra) {
111111
$extra = $record->extra;
112112
$extra = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($extra);
113113
} else {

src/Symfony/Bridge/PsrHttpMessage/Factory/PsrHttpFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function createResponse(Response $symfonyResponse): ResponseInterface
178178

179179
$headers = $symfonyResponse->headers->all();
180180
$cookies = $symfonyResponse->headers->getCookies();
181-
if (!empty($cookies)) {
181+
if ($cookies) {
182182
$headers['Set-Cookie'] = [];
183183

184184
foreach ($cookies as $cookie) {

src/Symfony/Bridge/PsrHttpMessage/Tests/Fixtures/Uri.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public function getScheme(): string
4747

4848
public function getAuthority(): string
4949
{
50-
if (empty($this->host)) {
50+
if (!$this->host) {
5151
return '';
5252
}
5353

5454
$authority = $this->host;
5555

56-
if (!empty($this->userInfo)) {
56+
if ($this->userInfo) {
5757
$authority = $this->userInfo.'@'.$authority;
5858
}
5959

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public function searchAction(Request $request): Response
275275
$session->set('_profiler_search_type', $profileType);
276276
}
277277

278-
if (!empty($token)) {
278+
if ($token) {
279279
return new RedirectResponse($this->generator->generate('_profiler', ['token' => $token]), 302, ['Content-Type' => 'text/html']);
280280
}
281281

src/Symfony/Component/BrowserKit/AbstractBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public function reload(): Crawler
531531
*/
532532
public function followRedirect(): Crawler
533533
{
534-
if (empty($this->redirect)) {
534+
if (!$this->redirect) {
535535
throw new LogicException('The request was not redirected.');
536536
}
537537

src/Symfony/Component/BrowserKit/Cookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(
7171
$this->value = $value ?? '';
7272
$this->rawValue = rawurlencode($this->value);
7373
}
74-
$this->path = empty($path) ? '/' : $path;
74+
$this->path = $path ?: '/';
7575

7676
if (null !== $expires) {
7777
$timestampAsDateTime = \DateTimeImmutable::createFromFormat('U', $expires);

src/Symfony/Component/BrowserKit/CookieJar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function expire(string $name, ?string $path = '/', ?string $domain = null
7171
{
7272
$path ??= '/';
7373

74-
if (empty($domain)) {
74+
if (!$domain) {
7575
// an empty domain means any domain
7676
// this should never happen but it allows for a better BC
7777
$domains = array_keys($this->cookieJar);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
112112

113113
unset($options['username'], $options['password']);
114114
foreach ($options as $option => $value) {
115-
if (!empty($value)) {
115+
if ($value) {
116116
$bucket->$option = $value;
117117
}
118118
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function ifNull(): static
100100
*/
101101
public function ifEmpty(): static
102102
{
103-
$this->ifPart = static fn ($v) => empty($v);
103+
$this->ifPart = static fn ($v) => !$v;
104104
$this->allowedTypes = self::TYPE_ANY;
105105

106106
return $this;

0 commit comments

Comments
 (0)