Add return types

This commit is contained in:
Pierre Rudloff 2020-12-17 22:43:05 +01:00
parent f2785bca03
commit 05311ac7b6
21 changed files with 84 additions and 78 deletions

View file

@ -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();

View file

@ -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);

View file

@ -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',

View file

@ -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);

View file

@ -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');

View file

@ -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)) {

View file

@ -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.');

View file

@ -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) {

View file

@ -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);

View file

@ -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');

View file

@ -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);
}

View file

@ -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);

View file

@ -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', [])

View file

@ -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();

View file

@ -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);
}
/**

View file

@ -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)
{

View file

@ -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('');

View file

@ -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';
}

View file

@ -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),

View file

@ -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;
}
/**

View file

@ -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'],