diff --git a/composer.json b/composer.json index d31169c9..fbf77aa0 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "endroid/qr-code": "^1.7", "firebase/php-jwt": "^4.0", "guzzlehttp/guzzle": "^6.2", + "lstrojny/functional-php": "^1.8", "mikehaertl/phpwkhtmltopdf": "^2.2", "monolog/monolog": "^1.21", "roave/security-advisories": "dev-master", diff --git a/module/Common/functions/functions.php b/module/Common/functions/functions.php index 7ba4d515..f84957dc 100644 --- a/module/Common/functions/functions.php +++ b/module/Common/functions/functions.php @@ -6,8 +6,8 @@ namespace Shlinkio\Shlink\Common; use const ARRAY_FILTER_USE_KEY; use const JSON_ERROR_NONE; use function array_filter; +use function Functional\contains; use function getenv; -use function in_array; use function json_decode as spl_json_decode; use function json_last_error; use function json_last_error_msg; @@ -49,11 +49,6 @@ function env($key, $default = null) return trim($value); } -function contains($needle, array $haystack): bool -{ - return in_array($needle, $haystack, true); -} - /** * Returns only the keys in keysToPick from provided array * @@ -63,7 +58,7 @@ function contains($needle, array $haystack): bool function pick(array $array, array $keysToPick): array { return array_filter($array, function (string $key) use ($keysToPick) { - return contains($key, $keysToPick); + return contains($keysToPick, $key); }, ARRAY_FILTER_USE_KEY); } diff --git a/module/Common/src/Factory/CacheFactory.php b/module/Common/src/Factory/CacheFactory.php index d1ce5c1e..92f63c61 100644 --- a/module/Common/src/Factory/CacheFactory.php +++ b/module/Common/src/Factory/CacheFactory.php @@ -11,7 +11,7 @@ use Shlinkio\Shlink\Core\Options\AppOptions; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Factory\FactoryInterface; -use function Shlinkio\Shlink\Common\contains; +use function Functional\contains; use function Shlinkio\Shlink\Common\env; class CacheFactory implements FactoryInterface @@ -53,7 +53,7 @@ class CacheFactory implements FactoryInterface { // Try to get the adapter from config $config = $container->get('config'); - if (isset($config['cache']['adapter']) && contains($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)) { + if (isset($config['cache']['adapter']) && contains(self::VALID_CACHE_ADAPTERS, $config['cache']['adapter'])) { return $this->resolveCacheAdapter($config['cache']); } diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index 03e95ba7..b204d6f8 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -9,9 +9,9 @@ use Doctrine\ORM\QueryBuilder; use Shlinkio\Shlink\Core\Entity\ShortUrl; use function array_column; use function array_key_exists; +use function Functional\contains; use function is_array; use function key; -use function Shlinkio\Shlink\Common\contains; class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface { @@ -63,7 +63,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI $fieldName = is_array($orderBy) ? key($orderBy) : $orderBy; $order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC'; - if (contains($fieldName, ['visits', 'visitsCount', 'visitCount'])) { + if (contains(['visits', 'visitsCount', 'visitCount'], $fieldName)) { $qb->addSelect('COUNT(DISTINCT v) AS totalVisits') ->leftJoin('s.visits', 'v') ->groupBy('s') diff --git a/module/Core/src/Response/NotFoundHandler.php b/module/Core/src/Response/NotFoundHandler.php index 1e40457b..2350ce52 100644 --- a/module/Core/src/Response/NotFoundHandler.php +++ b/module/Core/src/Response/NotFoundHandler.php @@ -11,7 +11,7 @@ use Zend\Diactoros\Response; use Zend\Expressive\Template\TemplateRendererInterface; use function array_shift; use function explode; -use function Shlinkio\Shlink\Common\contains; +use function Functional\contains; class NotFoundHandler implements RequestHandlerInterface { @@ -47,7 +47,7 @@ class NotFoundHandler implements RequestHandlerInterface $status = StatusCodeInterface::STATUS_NOT_FOUND; // If the first accepted type is json, return a json response - if (contains($accept, ['application/json', 'text/json', 'application/x-json'])) { + if (contains(['application/json', 'text/json', 'application/x-json'], $accept)) { return new Response\JsonResponse([ 'error' => 'NOT_FOUND', 'message' => 'Not found', diff --git a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php index 3039f2af..3b1e27be 100644 --- a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php @@ -10,7 +10,7 @@ use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; use function array_diff; use function array_keys; -use function Shlinkio\Shlink\Common\contains; +use function Functional\contains; class DatabaseConfigCustomizer implements ConfigCustomizerInterface { @@ -68,7 +68,7 @@ class DatabaseConfigCustomizer implements ConfigCustomizerInterface } // If the driver is one of the params to ask for, ask for it first - if (contains(self::DRIVER, $keysToAskFor)) { + if (contains($keysToAskFor, self::DRIVER)) { $io->title('DATABASE'); $titlePrinted = true; $db[self::DRIVER] = $this->ask($io, self::DRIVER); diff --git a/module/Rest/src/Middleware/AuthenticationMiddleware.php b/module/Rest/src/Middleware/AuthenticationMiddleware.php index 9a9cdb10..164fb636 100644 --- a/module/Rest/src/Middleware/AuthenticationMiddleware.php +++ b/module/Rest/src/Middleware/AuthenticationMiddleware.php @@ -20,8 +20,8 @@ use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; use Zend\Expressive\Router\RouteResult; use Zend\I18n\Translator\TranslatorInterface; +use function Functional\contains; use function implode; -use function Shlinkio\Shlink\Common\contains; use function sprintf; class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface @@ -72,7 +72,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa if ($routeResult === null || $routeResult->isFailure() || $request->getMethod() === self::METHOD_OPTIONS - || contains($routeResult->getMatchedRouteName(), $this->routesWhitelist) + || contains($this->routesWhitelist, $routeResult->getMatchedRouteName()) ) { return $handler->handle($request); } diff --git a/module/Rest/src/Middleware/BodyParserMiddleware.php b/module/Rest/src/Middleware/BodyParserMiddleware.php index 5d9b883f..ce5f6ee7 100644 --- a/module/Rest/src/Middleware/BodyParserMiddleware.php +++ b/module/Rest/src/Middleware/BodyParserMiddleware.php @@ -10,8 +10,8 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use function array_shift; use function explode; +use function Functional\contains; use function parse_str; -use function Shlinkio\Shlink\Common\contains; use function Shlinkio\Shlink\Common\json_decode; use function trim; @@ -32,17 +32,17 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac $currentParams = $request->getParsedBody(); // In requests that do not allow body or if the body has already been parsed, continue to next middleware - if (! empty($currentParams) || contains($method, [ + if (! empty($currentParams) || contains([ self::METHOD_GET, self::METHOD_HEAD, self::METHOD_OPTIONS, - ])) { + ], $method)) { return $handler->handle($request); } // If the accepted content is JSON, try to parse the body from JSON $contentType = $this->getRequestContentType($request); - if (contains($contentType, ['application/json', 'text/json', 'application/x-json'])) { + if (contains(['application/json', 'text/json', 'application/x-json'], $contentType)) { return $handler->handle($this->parseFromJson($request)); }