Skip to content

Commit 1dace41

Browse files
committed
minor #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 ------- ccc813c65f Remove unnecessary empty usages
2 parents 4ae6203 + e5b4db4 commit 1dace41

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

Application.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public function findNamespace(string $namespace): string
620620
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*';
621621
$namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);
622622

623-
if (empty($namespaces)) {
623+
if (!$namespaces) {
624624
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
625625

626626
if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {
@@ -674,12 +674,12 @@ public function find(string $name): Command
674674
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $name))).'[^:]*';
675675
$commands = preg_grep('{^'.$expr.'}', $allCommands);
676676

677-
if (empty($commands)) {
677+
if (!$commands) {
678678
$commands = preg_grep('{^'.$expr.'}i', $allCommands);
679679
}
680680

681681
// if no commands matched or we just matched namespaces
682-
if (empty($commands) || \count(preg_grep('{^'.$expr.'$}i', $commands)) < 1) {
682+
if (!$commands || \count(preg_grep('{^'.$expr.'$}i', $commands)) < 1) {
683683
if (false !== $pos = strrpos($name, ':')) {
684684
// check if a namespace exists and contains commands
685685
$this->findNamespace(substr($name, 0, $pos));

Descriptor/XmlDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function getInputOptionDocument(InputOption $option): \DOMDocument
208208
$defaults = \is_array($option->getDefault()) ? $option->getDefault() : (\is_bool($option->getDefault()) ? [var_export($option->getDefault(), true)] : ($option->getDefault() ? [$option->getDefault()] : []));
209209
$objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
210210

211-
if (!empty($defaults)) {
211+
if ($defaults) {
212212
foreach ($defaults as $default) {
213213
$defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
214214
$defaultXML->appendChild($dom->createTextNode($default));

Helper/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private function fillNextRows(array $rows, int $line): array
727727
} else {
728728
$row = $this->copyRow($rows, $unmergedRowKey - 1);
729729
foreach ($unmergedRow as $column => $cell) {
730-
if (!empty($cell)) {
730+
if ($cell) {
731731
$row[$column] = $cell;
732732
}
733733
}

Input/InputOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(
7575
$name = substr($name, 2);
7676
}
7777

78-
if (empty($name)) {
78+
if (!$name) {
7979
throw new InvalidArgumentException('An option name cannot be empty.');
8080
}
8181

Output/ConsoleSectionOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setMaxHeight(int $maxHeight): void
6363
*/
6464
public function clear(?int $lines = null): void
6565
{
66-
if (empty($this->content) || !$this->isDecorated()) {
66+
if (!$this->content || !$this->isDecorated()) {
6767
return;
6868
}
6969

0 commit comments

Comments
 (0)