Skip to content

Commit

Permalink
Replace "empty()" calls with null comparisons.
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nikolaev committed Dec 3, 2019
1 parent 2961e62 commit 1a3b03f
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Command/TranslateTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(string $name, ?string $apiKey = null)
$this->setDefinition([
new InputArgument('target_languages', InputArgument::REQUIRED, 'Target language(s), comma separated'),
new InputArgument('directory', InputArgument::REQUIRED, 'Translation file directory'),
new InputArgument('yandex_translate_api_key', !empty($apiKey) ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'Yandex Translate API key'),
new InputArgument('yandex_translate_api_key', null !== $apiKey ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'Yandex Translate API key'),
new InputOption('source_language', 's', InputOption::VALUE_OPTIONAL, 'Source language', 'en'),
]);
}
Expand All @@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$apiKey = $input->getArgument('yandex_translate_api_key');
$io = new SymfonyStyle($input, $output);

if (!empty($apiKey)) {
if (null !== $apiKey) {
$this->apiKey = $apiKey;
}
foreach (array_map('trim', explode(',', $input->getArgument('target_languages'))) as $to) {
Expand Down Expand Up @@ -116,7 +116,7 @@ private function translate(array $translations): array
*/
private function translateText(?string $text): ?string
{
if (empty($text) || preg_match('/^\s+$/', $text)) {
if (null === $text || preg_match('/^\s+$/', $text)) {
return $text;
}
if (false !== strpos($text, '|')) {
Expand All @@ -137,7 +137,7 @@ private function translateText(?string $text): ?string
$parts = [];

foreach ($words as $i => $word) {
$parts[] = !empty($word) && !preg_match('/^\s+$/', $word) ? $this->translateText($word) : $word;
$parts[] = '' !== $word && !preg_match('/^\s+$/', $word) ? $this->translateText($word) : $word;

if (isset($placeholders[$i])) {
$parts[] = $placeholders[$i];
Expand Down
2 changes: 1 addition & 1 deletion DataFixtures/ORM/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ final protected function generateImageFile(
if (null !== $text) {
$text = trim($text);

if (empty($text)) {
if ('' === $text) {
$text = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion EventListener/CompressResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function compressResponse(ResponseEvent $event): void
if (is_array($contentType)) {
$contentType = reset($contentType);
}
if (Response::class === get_class($response) && (empty($contentType) || 0 === strpos($contentType, 'text/html'))) {
if (Response::class === get_class($response) && (null === $contentType || 0 === strpos($contentType, 'text/html'))) {
$response->setContent(trim(preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', PHP_EOL, preg_replace('/\h+/u', ' ', $response->getContent()))));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Form/DataTransformer/EntityToIDTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(EntityManager $em, $entityClass)
*/
public function transform($value)
{
if (empty($value)) {
if (null === $value) {
return null;
}

Expand All @@ -57,7 +57,7 @@ public function transform($value)
*/
public function reverseTransform($value)
{
if (empty($value)) {
if (null === $value) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion Locale/LocaleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function getCurrentLocale()
{
$request = $this->requestStack->getCurrentRequest();

return !empty($request) ? $request->getLocale() : $this->defaultLocale;
return null !== $request ? $request->getLocale() : $this->defaultLocale;
}
}
2 changes: 1 addition & 1 deletion Mapping/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private function getClass($objectOrClass)
*/
private function getObjectManager()
{
if (empty($this->om)) {
if (null === $this->om) {
$this->om = $this->objectManagerProvider->getService();
}

Expand Down
4 changes: 2 additions & 2 deletions Routing/CachedRouteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ public function cacheRoutes()

if ($delocalizedName !== $name) {
$name = $delocalizedName;
$routeLocale = $locale;
$routeLocale = (string)$locale;

break;
}
}
if (!isset($routes[$name])) {
if (!empty($routeLocale)) {
if ('' !== $routeLocale) {
$requirements[self::REQUIREMENT_LOCALE] = $localePattern;
}

Expand Down
15 changes: 8 additions & 7 deletions Sluggable/SluggableEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,20 @@ public function generateSlugs($entity, $dispatchUpdateEvent = false, $prefix = n
}

/**
* @param object $entity Entity
* @param string $slugProperty Slug property
* @param array $sourcePropertyPaths Source property paths
* @param string $prefix Slug prefix
* @param object $entity Entity
* @param string $slugProperty Slug property
* @param array $sourcePropertyPaths Source property paths
* @param string|null $prefix Slug prefix
*
* @return array
* @throws \Darvin\Utils\Sluggable\SluggableException
*/
private function getSlugParts($entity, $slugProperty, array $sourcePropertyPaths, $prefix = null)
{
$slugParts = [];
$prefix = (string)$prefix;

if (!empty($prefix)) {
if ('' !== $prefix) {
$slugParts[] = $prefix;
}
foreach ($sourcePropertyPaths as $propertyPath) {
Expand All @@ -177,9 +178,9 @@ private function getSlugParts($entity, $slugProperty, array $sourcePropertyPaths
}
}

$slugPart = $this->getPropertyValue($entity, $propertyPath);
$slugPart = (string)$this->getPropertyValue($entity, $propertyPath);

if (!empty($slugPart)) {
if ('' !== $slugPart) {
$slugParts[] = $slugPart;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tree/TreeSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function sortTree(array $entities)
foreach ($entities as $entity) {
$parent = $this->propertyAccessor->getValue($entity, $config['parent']);

if (empty($parent)) {
if (null === $parent) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions User/UserQueryBuilderFilterer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ public function filter(QueryBuilder $qb, UserInterface $user = null)
if (!$this->isFilterable($qb)) {
throw new \InvalidArgumentException('Query builder is not filterable.');
}
if (empty($user)) {
if (null === $user) {
$user = $this->getUser();

if (empty($user)) {
if (null === $user) {
return;
}
}

$userIds = $this->extendedMetadataFactory->getDoctrineMetadata($user)->getIdentifierValues($user);
$userId = reset($userIds);

if (empty($userId)) {
if (null === $userId) {
throw new \InvalidArgumentException('User ID is empty.');
}
foreach (array_combine($qb->getRootAliases(), $qb->getRootEntities()) as $alias => $entity) {
Expand Down Expand Up @@ -130,7 +130,7 @@ private function getUser()
{
$token = $this->authenticationTokenStorage->getToken();

if (empty($token)) {
if (null === $token) {
return null;
}

Expand Down

0 comments on commit 1a3b03f

Please sign in to comment.