From 05311ac7b6b2f175c9dd02076734d818cf0cb3c3 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 17 Dec 2020 22:43:05 +0100 Subject: [PATCH] Add return types --- classes/Config.php | 8 ++++---- classes/Controller/BaseController.php | 8 ++++---- classes/Controller/DownloadController.php | 14 +++++++------- classes/Controller/FrontController.php | 18 +++++++++--------- classes/Controller/JsonController.php | 2 +- classes/Factory/ConfigFactory.php | 2 +- classes/Factory/LocaleManagerFactory.php | 4 ++-- classes/Factory/LoggerFactory.php | 2 +- classes/Factory/SessionFactory.php | 2 +- classes/Factory/ViewFactory.php | 4 ++-- classes/Locale.php | 10 +++++----- classes/LocaleManager.php | 8 ++++---- classes/Middleware/CspMiddleware.php | 2 +- classes/Middleware/LocaleMiddleware.php | 4 ++-- classes/Stream/PlaylistArchiveStream.php | 22 +++++++++++----------- classes/Stream/YoutubeChunkStream.php | 20 ++++++++++---------- classes/UglyRouter.php | 4 ++-- tests/BaseTest.php | 2 +- tests/ControllerTest.php | 2 +- tests/LocaleMiddlewareTest.php | 14 ++++++++++---- tests/VideoTest.php | 10 +++++----- 21 files changed, 84 insertions(+), 78 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index 666b741..da7a10a 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -187,7 +187,7 @@ class Config * * @return string */ - public static function addHttpToFormat(string $format) + public static function addHttpToFormat(string $format): string { $newFormat = []; foreach (explode('/', $format) as $subformat) { @@ -266,7 +266,7 @@ class Config * @return Config * @throws ConfigException */ - public static function fromFile(string $file) + public static function fromFile(string $file): Config { if (is_file($file)) { return new self(Yaml::parse(strval(file_get_contents($file)))); @@ -293,7 +293,7 @@ class Config * * @return Downloader */ - public function getDownloader() + public function getDownloader(): Downloader { return new Downloader( $this->youtubedl, @@ -308,7 +308,7 @@ class Config /** * @return string */ - public function getAppVersion() + public function getAppVersion(): string { $version = PrettyVersions::getRootPackageVersion(); diff --git a/classes/Controller/BaseController.php b/classes/Controller/BaseController.php index f57b0aa..f773bc1 100644 --- a/classes/Controller/BaseController.php +++ b/classes/Controller/BaseController.php @@ -111,7 +111,7 @@ abstract class BaseController * * @return string format */ - protected function getFormat(Request $request) + protected function getFormat(Request $request): string { $format = $request->getQueryParam('format'); if (!isset($format)) { @@ -126,9 +126,9 @@ abstract class BaseController * * @param Request $request PSR-7 request * - * @return string Password + * @return string|null Password */ - protected function getPassword(Request $request) + protected function getPassword(Request $request): ?string { $url = $request->getQueryParam('url'); @@ -151,7 +151,7 @@ abstract class BaseController * * @return Response HTTP response */ - protected function displayError(Request $request, Response $response, string $message) + protected function displayError(Request $request, Response $response, string $message): Response { $controller = new FrontController($this->container); diff --git a/classes/Controller/DownloadController.php b/classes/Controller/DownloadController.php index b80890f..78e8b77 100644 --- a/classes/Controller/DownloadController.php +++ b/classes/Controller/DownloadController.php @@ -38,7 +38,7 @@ class DownloadController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - public function download(Request $request, Response $response) + public function download(Request $request, Response $response): Response { $url = $request->getQueryParam('url'); @@ -99,7 +99,7 @@ class DownloadController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - private function getConvertedAudioResponse(Request $request, Response $response) + private function getConvertedAudioResponse(Request $request, Response $response): Response { $from = null; $to = null; @@ -135,7 +135,7 @@ class DownloadController extends BaseController * @throws PasswordException * @throws WrongPasswordException */ - private function getAudioResponse(Request $request, Response $response) + private function getAudioResponse(Request $request, Response $response): Response { if (!empty($request->getQueryParam('from')) || !empty($request->getQueryParam('to'))) { // Force convert when we need to seek. @@ -174,7 +174,7 @@ class DownloadController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - private function getStream(Request $request, Response $response) + private function getStream(Request $request, Response $response): Response { if (isset($this->video->entries)) { if ($this->config->convert && $request->getQueryParam('audio')) { @@ -240,7 +240,7 @@ class DownloadController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - private function getRemuxStream(Request $request, Response $response) + private function getRemuxStream(Request $request, Response $response): Response { if (!$this->config->remux) { throw new RemuxException('You need to enable remux mode to merge two formats.'); @@ -267,7 +267,7 @@ class DownloadController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - private function getDownloadResponse(Request $request, Response $response) + private function getDownloadResponse(Request $request, Response $response): Response { try { $videoUrls = $this->video->getUrl(); @@ -306,7 +306,7 @@ class DownloadController extends BaseController * @throws YoutubedlException * @throws PopenStreamException */ - private function getConvertedResponse(Request $request, Response $response) + private function getConvertedResponse(Request $request, Response $response): Response { $response = $response->withHeader( 'Content-Disposition', diff --git a/classes/Controller/FrontController.php b/classes/Controller/FrontController.php index 38040b8..e091705 100644 --- a/classes/Controller/FrontController.php +++ b/classes/Controller/FrontController.php @@ -52,7 +52,7 @@ class FrontController extends BaseController * * @return Response HTTP response */ - public function index(Request $request, Response $response) + public function index(Request $request, Response $response): Response { $this->view->render( $response, @@ -78,7 +78,7 @@ class FrontController extends BaseController * * @return Response */ - public function locale(Request $request, Response $response, array $data) + public function locale(Request $request, Response $response, array $data): Response { $this->localeManager->setLocale(new Locale($data['locale'])); @@ -94,7 +94,7 @@ class FrontController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - public function extractors(Request $request, Response $response) + public function extractors(Request $request, Response $response): Response { $this->view->render( $response, @@ -119,7 +119,7 @@ class FrontController extends BaseController * * @return Response HTTP response */ - public function password(Request $request, Response $response) + public function password(Request $request, Response $response): Response { $this->view->render( $response, @@ -199,7 +199,7 @@ class FrontController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - public function info(Request $request, Response $response) + public function info(Request $request, Response $response): Response { $url = $request->getQueryParam('url') ?: $request->getQueryParam('v'); @@ -228,7 +228,7 @@ class FrontController extends BaseController * * @return Response HTTP response */ - protected function displayError(Request $request, Response $response, string $message) + protected function displayError(Request $request, Response $response, string $message): Response { $this->view->render( $response, @@ -248,7 +248,7 @@ class FrontController extends BaseController * @param Response $response * @return Response */ - public function notFound(Request $request, Response $response) + public function notFound(Request $request, Response $response): Response { return $this->displayError($request, $response, $this->localeManager->t('Page not found')) ->withStatus(StatusCode::HTTP_NOT_FOUND); @@ -259,7 +259,7 @@ class FrontController extends BaseController * @param Response $response * @return Response */ - public function notAllowed(Request $request, Response $response) + public function notAllowed(Request $request, Response $response): Response { return $this->displayError($request, $response, $this->localeManager->t('Method not allowed')) ->withStatus(StatusCode::HTTP_METHOD_NOT_ALLOWED); @@ -274,7 +274,7 @@ class FrontController extends BaseController * * @return Response HTTP response */ - public function error(Request $request, Response $response, Throwable $error) + public function error(Request $request, Response $response, Throwable $error): Response { $this->logger->error($error); diff --git a/classes/Controller/JsonController.php b/classes/Controller/JsonController.php index 1247c6b..9f916bc 100644 --- a/classes/Controller/JsonController.php +++ b/classes/Controller/JsonController.php @@ -25,7 +25,7 @@ class JsonController extends BaseController * @return Response HTTP response * @throws AlltubeLibraryException */ - public function json(Request $request, Response $response) + public function json(Request $request, Response $response): Response { $url = $request->getQueryParam('url'); diff --git a/classes/Factory/ConfigFactory.php b/classes/Factory/ConfigFactory.php index 29cd0d7..c408b0e 100644 --- a/classes/Factory/ConfigFactory.php +++ b/classes/Factory/ConfigFactory.php @@ -20,7 +20,7 @@ class ConfigFactory * @return Config * @throws ConfigException */ - public static function create(Container $container) + public static function create(Container $container): Config { $configPath = __DIR__ . '/../../config/config.yml'; if (is_file($configPath)) { diff --git a/classes/Factory/LocaleManagerFactory.php b/classes/Factory/LocaleManagerFactory.php index 22fa261..30df471 100644 --- a/classes/Factory/LocaleManagerFactory.php +++ b/classes/Factory/LocaleManagerFactory.php @@ -15,10 +15,10 @@ class LocaleManagerFactory /** * @param Container $container - * @return LocaleManager|null + * @return LocaleManager * @throws DependencyException */ - public static function create(Container $container) + public static function create(Container $container): LocaleManager { if (!class_exists('Locale')) { throw new DependencyException('You need to install the intl extension for PHP.'); diff --git a/classes/Factory/LoggerFactory.php b/classes/Factory/LoggerFactory.php index 87cae0e..fe85133 100644 --- a/classes/Factory/LoggerFactory.php +++ b/classes/Factory/LoggerFactory.php @@ -18,7 +18,7 @@ class LoggerFactory * @param Container $container * @return Logger */ - public static function create(Container $container) + public static function create(Container $container): Logger { $config = $container->get('config'); if ($config->debug) { diff --git a/classes/Factory/SessionFactory.php b/classes/Factory/SessionFactory.php index b819b2f..05bb796 100644 --- a/classes/Factory/SessionFactory.php +++ b/classes/Factory/SessionFactory.php @@ -21,7 +21,7 @@ class SessionFactory * @param Container $container * @return Session */ - public static function create(Container $container) + public static function create(Container $container): Session { $session_factory = new \Aura\Session\SessionFactory(); $session = $session_factory->newInstance($_COOKIE); diff --git a/classes/Factory/ViewFactory.php b/classes/Factory/ViewFactory.php index d7048b7..1cc09e2 100644 --- a/classes/Factory/ViewFactory.php +++ b/classes/Factory/ViewFactory.php @@ -26,7 +26,7 @@ class ViewFactory * * @return string URL */ - private static function getCanonicalUrl(Request $request) + private static function getCanonicalUrl(Request $request): string { /** @var Uri $uri */ $uri = $request->getUri(); @@ -45,7 +45,7 @@ class ViewFactory * @return Smarty * @throws SmartyException */ - public static function create(ContainerInterface $container, Request $request = null) + public static function create(ContainerInterface $container, Request $request = null): Smarty { if (!isset($request)) { $request = $container->get('request'); diff --git a/classes/Locale.php b/classes/Locale.php index 9e81180..bacfc9d 100644 --- a/classes/Locale.php +++ b/classes/Locale.php @@ -48,7 +48,7 @@ class Locale * * @return string ISO 15897 code */ - public function __toString() + public function __toString(): string { return $this->getIso15897(); } @@ -58,7 +58,7 @@ class Locale * * @return string */ - public function getFullName() + public function getFullName(): string { return PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897()); } @@ -68,7 +68,7 @@ class Locale * * @return string */ - public function getIso15897() + public function getIso15897(): string { if (isset($this->region)) { return $this->language . '_' . $this->region; @@ -82,7 +82,7 @@ class Locale * * @return string */ - public function getBcp47() + public function getBcp47(): string { if (isset($this->region)) { return $this->language . '-' . $this->region; @@ -96,7 +96,7 @@ class Locale * * @return string */ - public function getIso3166() + public function getIso3166(): string { return strtolower($this->region); } diff --git a/classes/LocaleManager.php b/classes/LocaleManager.php index f3f7919..884579e 100644 --- a/classes/LocaleManager.php +++ b/classes/LocaleManager.php @@ -80,7 +80,7 @@ class LocaleManager * * @return Locale[] */ - public function getSupportedLocales() + public function getSupportedLocales(): array { $return = [ new Locale('en_US') @@ -103,7 +103,7 @@ class LocaleManager * * @return Locale|null */ - public function getLocale() + public function getLocale(): ?Locale { return $this->curLocale; } @@ -140,7 +140,7 @@ class LocaleManager * * @return string Translated string */ - public function smartyTranslate(array $params, string $text = null) + public function smartyTranslate(array $params, string $text = null): string { if (isset($params['params'])) { return $this->t($text, $params['params']); @@ -157,7 +157,7 @@ class LocaleManager * @param mixed[] $params * @return string Translated string */ - public function t(string $string = null, array $params = []) + public function t(string $string = null, array $params = []): string { if (isset($string)) { return $this->translator->trans($string, $params); diff --git a/classes/Middleware/CspMiddleware.php b/classes/Middleware/CspMiddleware.php index c3c72ff..3449df3 100644 --- a/classes/Middleware/CspMiddleware.php +++ b/classes/Middleware/CspMiddleware.php @@ -34,7 +34,7 @@ class CspMiddleware * @param Response $response * @return MessageInterface */ - public function applyHeader(Response $response) + public function applyHeader(Response $response): MessageInterface { $csp = new CSPBuilder(); $csp->addDirective('default-src', []) diff --git a/classes/Middleware/LocaleMiddleware.php b/classes/Middleware/LocaleMiddleware.php index b28f357..418ee7d 100644 --- a/classes/Middleware/LocaleMiddleware.php +++ b/classes/Middleware/LocaleMiddleware.php @@ -42,7 +42,7 @@ class LocaleMiddleware * * @return Locale|null Locale if chosen, nothing otherwise */ - public function testLocale(array $proposedLocale) + public function testLocale(array $proposedLocale): ?Locale { foreach ($this->localeManager->getSupportedLocales() as $locale) { $parsedLocale = AcceptLanguage::parse($locale); @@ -67,7 +67,7 @@ class LocaleMiddleware * * @return Response */ - public function __invoke(Request $request, Response $response, callable $next) + public function __invoke(Request $request, Response $response, callable $next): Response { $headers = $request->getHeader('Accept-Language'); $curLocale = $this->localeManager->getLocale(); diff --git a/classes/Stream/PlaylistArchiveStream.php b/classes/Stream/PlaylistArchiveStream.php index 1bf08ff..065a57b 100644 --- a/classes/Stream/PlaylistArchiveStream.php +++ b/classes/Stream/PlaylistArchiveStream.php @@ -113,7 +113,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return int|null */ - public function getSize() + public function getSize(): ?int { return null; } @@ -123,7 +123,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return bool */ - public function isSeekable() + public function isSeekable(): bool { return true; } @@ -143,7 +143,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return bool */ - public function isWritable() + public function isWritable(): bool { return true; } @@ -153,7 +153,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return bool */ - public function isReadable() + public function isReadable(): bool { return true; } @@ -173,7 +173,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @param string|null $key string $key Specific metadata to retrieve. * - * @return array|mixed|null + * @return mixed|null */ public function getMetadata($key = null) { @@ -208,7 +208,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return string */ - public function __toString() + public function __toString(): string { $this->rewind(); @@ -243,7 +243,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface * * @return bool */ - public function eof() + public function eof(): bool { return $this->isComplete && feof($this->buffer); } @@ -272,12 +272,12 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface /** * Read data from the stream. * - * @param mixed $count Number of bytes to read + * @param mixed $length Number of bytes to read * * @return string|false * @throws AlltubeLibraryException */ - public function read($count) + public function read($length) { // If the archive is complete, we only read the remaining buffer. if (!$this->isComplete) { @@ -297,7 +297,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface } } else { // Continue streaming the current video. - $this->stream_file_part($this->curVideoStream->read($count)); + $this->stream_file_part($this->curVideoStream->read($length)); } } else { // Start streaming the first video. @@ -305,7 +305,7 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface } } - return fread($this->buffer, $count); + return fread($this->buffer, $length); } /** diff --git a/classes/Stream/YoutubeChunkStream.php b/classes/Stream/YoutubeChunkStream.php index bf64c0a..bb95138 100644 --- a/classes/Stream/YoutubeChunkStream.php +++ b/classes/Stream/YoutubeChunkStream.php @@ -39,7 +39,7 @@ class YoutubeChunkStream implements StreamInterface * * @return string */ - public function read($length) + public function read($length): string { $size = intval($this->response->getHeader('Content-Length')[0]); if ($size - $this->tell() < $length) { @@ -53,7 +53,7 @@ class YoutubeChunkStream implements StreamInterface /** * Reads all data from the stream into a string, from the beginning to end. */ - public function __toString() + public function __toString(): string { return (string)$this->response->getBody(); } @@ -83,7 +83,7 @@ class YoutubeChunkStream implements StreamInterface * * @return int|null */ - public function getSize() + public function getSize(): ?int { return $this->response->getBody()->getSize(); } @@ -93,7 +93,7 @@ class YoutubeChunkStream implements StreamInterface * * @return int */ - public function tell() + public function tell(): int { return $this->response->getBody()->tell(); } @@ -103,7 +103,7 @@ class YoutubeChunkStream implements StreamInterface * * @return bool */ - public function eof() + public function eof(): bool { return $this->response->getBody()->eof(); } @@ -113,7 +113,7 @@ class YoutubeChunkStream implements StreamInterface * * @return bool */ - public function isSeekable() + public function isSeekable(): bool { return $this->response->getBody()->isSeekable(); } @@ -146,7 +146,7 @@ class YoutubeChunkStream implements StreamInterface * * @return bool */ - public function isWritable() + public function isWritable(): bool { return $this->response->getBody()->isWritable(); } @@ -168,7 +168,7 @@ class YoutubeChunkStream implements StreamInterface * * @return bool */ - public function isReadable() + public function isReadable(): bool { return $this->response->getBody()->isReadable(); } @@ -178,7 +178,7 @@ class YoutubeChunkStream implements StreamInterface * * @return string */ - public function getContents() + public function getContents(): string { return $this->response->getBody()->getContents(); } @@ -188,7 +188,7 @@ class YoutubeChunkStream implements StreamInterface * * @param string|null $key Specific metadata to retrieve. * - * @return array|mixed|null + * @return mixed|null */ public function getMetadata($key = null) { diff --git a/classes/UglyRouter.php b/classes/UglyRouter.php index 95a60cf..51f080f 100644 --- a/classes/UglyRouter.php +++ b/classes/UglyRouter.php @@ -26,7 +26,7 @@ class UglyRouter extends Router * * @link https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php */ - public function dispatch(ServerRequestInterface $request) + public function dispatch(ServerRequestInterface $request): array { $params = $request->getQueryParams(); $uri = new Uri('', ''); @@ -53,7 +53,7 @@ class UglyRouter extends Router * @throws InvalidArgumentException If required data not provided * @throws RuntimeException If named route does not exist */ - public function pathFor($name, array $data = [], array $queryParams = []) + public function pathFor($name, array $data = [], array $queryParams = []): string { $queryParams['page'] = $name; $url = Uri::createFromString($this->relativePathFor($name, $data, $queryParams))->withPath(''); diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 7aa5351..3d41d6a 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -18,7 +18,7 @@ abstract class BaseTest extends TestCase * * @return string Path to file */ - protected function getConfigFile() + protected function getConfigFile(): string { return __DIR__ . '/../config/config_test.yml'; } diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 6229ccf..40396bf 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -69,7 +69,7 @@ abstract class ControllerTest extends ContainerTest * * @return Response HTTP response */ - protected function getRequestResult(string $request, array $params) + protected function getRequestResult(string $request, array $params): Response { return $this->controller->$request( $this->container->get('request')->withQueryParams($params), diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index b9a45cd..3452fdc 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -85,26 +85,32 @@ class LocaleMiddlewareTest extends ContainerTest * Check that the request contains an Accept-Language header. * * @param Request $request PSR-7 request + * @param Response $response * - * @return void + * @return Response */ - public function assertHeader(Request $request) + public function assertHeader(Request $request, Response $response): Response { $header = $request->getHeader('Accept-Language'); $this->assertEquals('foo-BAR', $header[0]); + + return $response; } /** * Check that the request contains no Accept-Language header. * * @param Request $request PSR-7 request + * @param Response $response * - * @return void + * @return Response */ - public function assertNoHeader(Request $request) + public function assertNoHeader(Request $request, Response $response): Response { $header = $request->getHeader('Accept-Language'); $this->assertEmpty($header); + + return $response; } /** diff --git a/tests/VideoTest.php b/tests/VideoTest.php index 6a26801..2107375 100644 --- a/tests/VideoTest.php +++ b/tests/VideoTest.php @@ -157,7 +157,7 @@ class VideoTest extends ContainerTest * * @return array[] */ - public function urlProvider() + public function urlProvider(): array { return [ [ @@ -193,7 +193,7 @@ class VideoTest extends ContainerTest * * @return array[] */ - public function remuxUrlProvider() + public function remuxUrlProvider(): array { return [ [ @@ -210,7 +210,7 @@ class VideoTest extends ContainerTest * * @return array[] */ - public function m3uUrlProvider() + public function m3uUrlProvider(): array { return [ [ @@ -227,7 +227,7 @@ class VideoTest extends ContainerTest * * @return array[] */ - public function rtmpUrlProvider() + public function rtmpUrlProvider(): array { return [ [ @@ -244,7 +244,7 @@ class VideoTest extends ContainerTest * * @return array[] */ - public function errorUrlProvider() + public function errorUrlProvider(): array { return [ ['http://example.com/video'],