Replaced usage of Functional\contians

This commit is contained in:
Alejandro Celaya 2023-11-30 09:13:29 +01:00
parent f50263d2d9
commit 549c6605f0
15 changed files with 68 additions and 46 deletions

View File

@ -46,12 +46,12 @@
"php-middleware/request-id": "^4.1",
"pugx/shortid-php": "^1.1",
"ramsey/uuid": "^4.7",
"shlinkio/shlink-common": "^5.7",
"shlinkio/shlink-common": "dev-main#1f1b3b8 as 5.8",
"shlinkio/shlink-config": "^2.5",
"shlinkio/shlink-event-dispatcher": "^3.1",
"shlinkio/shlink-importer": "^5.2",
"shlinkio/shlink-installer": "^8.6",
"shlinkio/shlink-ip-geolocation": "^3.3",
"shlinkio/shlink-importer": "dev-main#4616c54 as 5.3",
"shlinkio/shlink-installer": "dev-develop#cb0eaea as 8.7",
"shlinkio/shlink-ip-geolocation": "dev-main#ea88ae8 as 3.4",
"shlinkio/shlink-json": "^1.1",
"spiral/roadrunner": "^2023.2",
"spiral/roadrunner-cli": "^2.5",

View File

@ -5,11 +5,11 @@ declare(strict_types=1);
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\Config\EnvVars;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
return (static function (): array {
$driver = EnvVars::DB_DRIVER->loadFromEnv();
$isMysqlCompatible = contains(['maria', 'mysql'], $driver);
$isMysqlCompatible = contains($driver, ['maria', 'mysql']);
$resolveDriver = static fn () => match ($driver) {
'postgres' => 'pdo_pgsql',

View File

@ -28,9 +28,9 @@ use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use function file_exists;
use function Functional\contains;
use function Laminas\Stratigility\middleware;
use function Shlinkio\Shlink\Config\env;
use function Shlinkio\Shlink\Core\contains;
use function sprintf;
use function sys_get_temp_dir;
@ -41,7 +41,7 @@ $isApiTest = env('TEST_ENV') === 'api';
$isCliTest = env('TEST_ENV') === 'cli';
$isE2eTest = $isApiTest || $isCliTest;
$coverageType = env('GENERATE_COVERAGE');
$generateCoverage = contains(['yes', 'pretty'], $coverageType);
$generateCoverage = contains($coverageType, ['yes', 'pretty']);
$coverage = null;
if ($isE2eTest && $generateCoverage) {

View File

@ -17,8 +17,7 @@ use Symfony\Component\Process\PhpExecutableFinder;
use Throwable;
use function array_map;
use function Functional\contains;
use function Functional\some;
use function Shlinkio\Shlink\Core\contains;
class CreateDatabaseCommand extends AbstractDatabaseCommand
{
@ -72,9 +71,15 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
$allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
$shlinkTables = array_map(static fn (ClassMetadata $metadata) => $metadata->getTableName(), $allMetadata);
// If at least one of the shlink tables exist, we will consider the database exists somehow.
// Any other inconsistency will be taken care of by the migrations.
return some($shlinkTables, static fn (string $shlinkTable) => contains($existingTables, $shlinkTable));
foreach ($shlinkTables as $shlinkTable) {
// If at least one of the shlink tables exist, we will consider the database exists somehow.
// Any other inconsistency will be taken care of by the migrations.
if (contains($shlinkTable, $existingTables)) {
return true;
}
}
return false;
}
private function ensureDatabaseExistsAndGetTables(): array

View File

@ -20,10 +20,9 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function array_map;
use function array_unique;
use function explode;
use function Functional\curry;
use function Functional\flatten;
use function Functional\unique;
use function Shlinkio\SHlink\Core\flatten;
use function sprintf;
class CreateShortUrlCommand extends Command
@ -144,8 +143,8 @@ class CreateShortUrlCommand extends Command
return ExitCode::EXIT_FAILURE;
}
$explodeWithComma = curry(explode(...))(',');
$tags = unique(flatten(array_map($explodeWithComma, $input->getOption('tags'))));
$explodeWithComma = static fn (string $tag) => explode(',', $tag);
$tags = array_unique(flatten(array_map($explodeWithComma, $input->getOption('tags'))));
$customSlug = $input->getOption('custom-slug');
$maxVisits = $input->getOption('max-visits');
$shortCodeLength = $input->getOption('short-code-length') ?? $this->options->defaultShortCodesLength;

View File

@ -19,9 +19,9 @@ use Symfony\Component\Console\Output\OutputInterface;
use function array_filter;
use function array_keys;
use function array_map;
use function in_array;
use function Shlinkio\Shlink\Common\buildDateRange;
use function Shlinkio\Shlink\Core\camelCaseToHumanFriendly;
use function Shlinkio\Shlink\Core\contains;
use const ARRAY_FILTER_USE_KEY;
@ -66,10 +66,9 @@ abstract class AbstractVisitsListCommand extends Command
// Filter out unknown keys
return array_filter(
$rowData,
static fn (string $key) => in_array(
static fn (string $key) => contains(
$key,
['referer', 'date', 'userAgent', 'country', 'city', ...$extraKeys],
strict: true,
),
ARRAY_FILTER_USE_KEY,
);

View File

@ -6,7 +6,6 @@ namespace Shlinkio\Shlink\Core;
use BackedEnum;
use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosInterface;
use DateTimeInterface;
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
@ -18,8 +17,10 @@ use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
use function array_map;
use function array_reduce;
use function date_default_timezone_get;
use function Functional\reduce_left;
use function in_array;
use function is_array;
use function print_r;
use function Shlinkio\Shlink\Common\buildDateRange;
@ -57,7 +58,7 @@ function parseDateRangeFromQuery(array $query, string $startDateName, string $en
/**
* @return ($date is null ? null : Chronos)
*/
function normalizeOptionalDate(string|DateTimeInterface|ChronosInterface|null $date): ?Chronos
function normalizeOptionalDate(string|DateTimeInterface|Chronos|null $date): ?Chronos
{
$parsedDate = match (true) {
$date === null || $date instanceof Chronos => $date,
@ -68,7 +69,7 @@ function normalizeOptionalDate(string|DateTimeInterface|ChronosInterface|null $d
return $parsedDate?->setTimezone(date_default_timezone_get());
}
function normalizeDate(string|DateTimeInterface|ChronosInterface $date): Chronos
function normalizeDate(string|DateTimeInterface|Chronos $date): Chronos
{
return normalizeOptionalDate($date);
}
@ -180,3 +181,21 @@ function enumValues(string $enum): array
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
);
}
function contains(mixed $value, array $array): bool
{
return in_array($value, $array, strict: true);
}
/**
* @param array[] $multiArray
* @return array
*/
function flatten(array $multiArray): array
{
return array_reduce(
$multiArray,
static fn (array $carry, array $value) => [...$carry, ...$value],
initial: [],
);
}

View File

@ -18,7 +18,7 @@ use Endroid\QrCode\Writer\WriterInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
use function strtolower;
use function trim;
@ -74,7 +74,7 @@ final class QrCodeParams
private static function resolveWriter(array $query, QrCodeOptions $defaults): WriterInterface
{
$qFormat = self::normalizeParam($query['format'] ?? '');
$format = contains(self::SUPPORTED_FORMATS, $qFormat) ? $qFormat : self::normalizeParam($defaults->format);
$format = contains($qFormat, self::SUPPORTED_FORMATS) ? $qFormat : self::normalizeParam($defaults->format);
return match ($format) {
'svg' => new SvgWriter(),

View File

@ -2,7 +2,7 @@
namespace Shlinkio\Shlink\Core\ShortUrl\Model;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
enum OrderableField: string
{
@ -16,8 +16,8 @@ enum OrderableField: string
public static function isBasicField(string $value): bool
{
return contains(
[self::LONG_URL->value, self::SHORT_CODE->value, self::DATE_CREATED->value, self::TITLE->value],
$value,
[self::LONG_URL->value, self::SHORT_CODE->value, self::DATE_CREATED->value, self::TITLE->value],
);
}

View File

@ -10,9 +10,9 @@ use Shlinkio\Shlink\Core\Model\DeviceType;
use function array_keys;
use function array_values;
use function Functional\contains;
use function Functional\every;
use function is_array;
use function Shlinkio\Shlink\Core\contains;
use function Shlinkio\Shlink\Core\enumValues;
class DeviceLongUrlsValidator extends AbstractValidator
@ -41,7 +41,7 @@ class DeviceLongUrlsValidator extends AbstractValidator
$validValues = enumValues(DeviceType::class);
$keys = array_keys($value);
if (! every($keys, static fn ($key) => contains($validValues, $key))) {
if (! every($keys, static fn ($key) => contains($key, $validValues))) {
$this->error(self::INVALID_DEVICE);
return false;
}

View File

@ -2,7 +2,7 @@
namespace Shlinkio\Shlink\Core\Util;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
enum RedirectStatus: int
{
@ -13,11 +13,11 @@ enum RedirectStatus: int
public function allowsCache(): bool
{
return contains([self::STATUS_301, self::STATUS_308], $this);
return contains($this, [self::STATUS_301, self::STATUS_308]);
}
public function isLegacyStatus(): bool
{
return contains([self::STATUS_301, self::STATUS_302], $this);
return contains($this, [self::STATUS_301, self::STATUS_302]);
}
}

View File

@ -28,7 +28,7 @@ use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use function count;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
class NotifyVisitToWebHooksTest extends TestCase
{
@ -102,7 +102,7 @@ class NotifyVisitToWebHooksTest extends TestCase
return true;
}),
)->willReturnCallback(function ($_, $webhook) use ($invalidWebhooks) {
$shouldReject = contains($invalidWebhooks, $webhook);
$shouldReject = contains($webhook, $invalidWebhooks);
return $shouldReject ? new RejectedPromise(new Exception('')) : new FulfilledPromise('');
});
$this->logger->expects($this->exactly(count($invalidWebhooks)))->method('warning')->with(

View File

@ -32,8 +32,8 @@ use stdClass;
use Symfony\Component\Console\Style\StyleInterface;
use function count;
use function Functional\contains;
use function Functional\some;
use function Shlinkio\Shlink\Core\contains;
use function sprintf;
use function str_contains;
@ -128,8 +128,8 @@ class ImportedLinksProcessorTest extends TestCase
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->repo->expects($this->exactly(count($urls)))->method('findOneByImportedUrl')->willReturnCallback(
fn (ImportedShlinkUrl $url): ?ShortUrl => contains(
['https://foo', 'https://baz2', 'https://baz3'],
$url->longUrl,
['https://foo', 'https://baz2', 'https://baz3'],
) ? ShortUrl::fromImport($url, true) : null,
);
$this->shortCodeHelper->expects($this->exactly(2))->method('ensureShortCodeUniqueness')->willReturn(true);

View File

@ -17,16 +17,16 @@ use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException;
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface
{
public const API_KEY_HEADER = 'X-Api-Key';
public function __construct(
private ApiKeyServiceInterface $apiKeyService,
private array $routesWithoutApiKey,
private array $routesWithQueryApiKey,
private readonly ApiKeyServiceInterface $apiKeyService,
private readonly array $routesWithoutApiKey,
private readonly array $routesWithQueryApiKey,
) {
}
@ -38,7 +38,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
$routeResult === null
|| $routeResult->isFailure()
|| $request->getMethod() === self::METHOD_OPTIONS
|| contains($this->routesWithoutApiKey, $routeResult->getMatchedRouteName())
|| contains($routeResult->getMatchedRouteName(), $this->routesWithoutApiKey)
) {
return $handler->handle($request);
}
@ -61,7 +61,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
{
$routeName = $routeResult->getMatchedRouteName();
$query = $request->getQueryParams();
$isRouteWithApiKeyInQuery = contains($this->routesWithQueryApiKey, $routeName);
$isRouteWithApiKeyInQuery = contains($routeName, $this->routesWithQueryApiKey);
$apiKey = $isRouteWithApiKeyInQuery ? ($query['apiKey'] ?? '') : $request->getHeaderLine(self::API_KEY_HEADER);
if (empty($apiKey)) {

View File

@ -12,7 +12,7 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Exception\MalformedBodyException;
use function Functional\contains;
use function Shlinkio\Shlink\Core\contains;
use function Shlinkio\Shlink\Json\json_decode;
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
@ -25,11 +25,11 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
if (
! empty($currentParams)
|| contains([
|| contains($method, [
self::METHOD_GET,
self::METHOD_HEAD,
self::METHOD_OPTIONS,
], $method)
])
) {
return $handler->handle($request);
}