From 44bf858c35dd9c9e8d2449301ebd9678a93d7c62 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 3 Oct 2019 21:24:12 +0200 Subject: [PATCH] style(phpcs): Switch to PSR-12 https://www.php-fig.org/psr/psr-12/ --- Gruntfile.js | 2 +- classes/Config.php | 9 ++++--- classes/Locale.php | 5 ++-- classes/LocaleManager.php | 12 +++++---- classes/LocaleMiddleware.php | 6 +++-- classes/SessionManager.php | 1 + classes/UglyRouter.php | 3 ++- classes/Video.php | 23 +++++++++-------- classes/ViewFactory.php | 3 ++- composer.json | 2 +- composer.lock | 12 ++++----- controllers/BaseController.php | 1 + controllers/DownloadController.php | 26 +++++++++++--------- controllers/FrontController.php | 16 ++++++------ controllers/JsonController.php | 1 + index.php | 10 ++++---- tests/BaseTest.php | 3 ++- tests/ConfigTest.php | 1 + tests/ControllerTest.php | 1 + tests/ConvertedPlaylistArchiveStreamTest.php | 1 + tests/DownloadControllerTest.php | 1 + tests/FrontControllerTest.php | 1 + tests/JsonControllerTest.php | 1 + tests/LocaleManagerTest.php | 1 + tests/LocaleMiddlewareTest.php | 1 + tests/LocaleTest.php | 1 + tests/PlaylistArchiveStreamTest.php | 1 + tests/StreamTest.php | 1 + tests/UglyRouterTest.php | 1 + tests/VideoStubsTest.php | 1 + tests/VideoTest.php | 5 ++-- tests/ViewFactoryTest.php | 1 + tests/YoutubeChunkStreamTest.php | 1 + tests/YoutubeStreamTest.php | 1 + tests/bootstrap.php | 12 ++++----- 35 files changed, 101 insertions(+), 67 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index ace1270..91a7b8d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -23,7 +23,7 @@ module.exports = function (grunt) { }, phpcs: { options: { - standard: 'PSR2', + standard: 'PSR12', bin: 'vendor/bin/phpcs' }, php: { diff --git a/classes/Config.php b/classes/Config.php index 8105200..ea98a85 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -1,4 +1,5 @@ stream) { // Force HTTP if stream is not enabled. - $this->replaceGenericFormat($format, $format.'[protocol=https]/'.$format.'[protocol=http]'); + $this->replaceGenericFormat($format, $format . '[protocol=https]/' . $format . '[protocol=http]'); } } } @@ -197,9 +198,9 @@ class Config so they will go to the logs. */ if (!is_file($this->youtubedl)) { - throw new Exception("Can't find youtube-dl at ".$this->youtubedl); + throw new Exception("Can't find youtube-dl at " . $this->youtubedl); } elseif (!Video::checkCommand([$this->python, '--version'])) { - throw new Exception("Can't find Python at ".$this->python); + throw new Exception("Can't find Python at " . $this->python); } } @@ -262,7 +263,7 @@ class Config self::$instance = new self($options); self::$instance->validateOptions(); } else { - throw new Exception("Can't find config file at ".$file); + throw new Exception("Can't find config file at " . $file); } } diff --git a/classes/Locale.php b/classes/Locale.php index 9ccf020..6418df7 100644 --- a/classes/Locale.php +++ b/classes/Locale.php @@ -1,4 +1,5 @@ language.'_'.$this->region; + return $this->language . '_' . $this->region; } /** @@ -77,7 +78,7 @@ class Locale */ public function getBcp47() { - return $this->language.'-'.$this->region; + return $this->language . '-' . $this->region; } /** diff --git a/classes/LocaleManager.php b/classes/LocaleManager.php index c2bbc14..81ed684 100644 --- a/classes/LocaleManager.php +++ b/classes/LocaleManager.php @@ -1,4 +1,5 @@ setLocale(new Locale($cookieLocale)); } - bindtextdomain('Alltube', __DIR__.'/../i18n/'); + bindtextdomain('Alltube', __DIR__ . '/../i18n/'); textdomain('Alltube'); } @@ -61,8 +62,9 @@ class LocaleManager $process->run(); $installedLocales = explode(PHP_EOL, trim($process->getOutput())); foreach ($this->supportedLocales as $supportedLocale) { - if (in_array($supportedLocale, $installedLocales) - || in_array($supportedLocale.'.utf8', $installedLocales) + if ( + in_array($supportedLocale, $installedLocales) + || in_array($supportedLocale . '.utf8', $installedLocales) ) { $return[] = new Locale($supportedLocale); } @@ -88,8 +90,8 @@ class LocaleManager */ public function setLocale(Locale $locale) { - putenv('LANG='.$locale); - setlocale(LC_ALL, [$locale.'.utf8', $locale]); + putenv('LANG=' . $locale); + setlocale(LC_ALL, [$locale . '.utf8', $locale]); $this->curLocale = $locale; $this->sessionSegment->set('locale', $locale); } diff --git a/classes/LocaleMiddleware.php b/classes/LocaleMiddleware.php index 9aeb81e..f1ba610 100644 --- a/classes/LocaleMiddleware.php +++ b/classes/LocaleMiddleware.php @@ -1,4 +1,5 @@ localeManager->getSupportedLocales() as $locale) { $parsedLocale = AcceptLanguage::parse($locale); - if (isset($proposedLocale['language']) + if ( + isset($proposedLocale['language']) && $parsedLocale[1]['language'] == $proposedLocale['language'] && $parsedLocale[1]['region'] == $proposedLocale['region'] ) { - return new Locale($proposedLocale['language'].'_'.$proposedLocale['region']); + return new Locale($proposedLocale['language'] . '_' . $proposedLocale['region']); } } } diff --git a/classes/SessionManager.php b/classes/SessionManager.php index 0ecd586..9d8dace 100644 --- a/classes/SessionManager.php +++ b/classes/SessionManager.php @@ -1,4 +1,5 @@ relativePathFor($name, $data, $queryParams)); if ($this->basePath) { - $url = $this->basePath.$url; + $url = $this->basePath . $url; } return $url; diff --git a/classes/Video.php b/classes/Video.php index 72072c7..f368385 100644 --- a/classes/Video.php +++ b/classes/Video.php @@ -1,4 +1,5 @@ setEnv(['PATH'=>$config->phantomjsDir]); + $process->setEnv(['PATH' => $config->phantomjsDir]); $process->inheritEnvironmentVariables(); $process->run(); if (!$process->isSuccessful()) { @@ -162,7 +163,7 @@ class Video */ private function getProp($prop = 'dump-json') { - $arguments = ['--'.$prop]; + $arguments = ['--' . $prop]; if (isset($this->webpageUrl)) { $arguments[] = $this->webpageUrl; @@ -265,7 +266,7 @@ class Video pathinfo( $this->getFilename(), PATHINFO_FILENAME - ).'.'.$extension, + ) . '.' . $extension, ENT_COMPAT, 'ISO-8859-1' ); @@ -281,14 +282,16 @@ class Video $arguments = []; if ($this->protocol == 'rtmp') { - foreach ([ + foreach ( + [ 'url' => '-rtmp_tcurl', 'webpage_url' => '-rtmp_pageurl', 'player_url' => '-rtmp_swfverify', 'flash_version' => '-rtmp_flashver', 'play_path' => '-rtmp_playpath', 'app' => '-rtmp_app', - ] as $property => $option) { + ] as $property => $option + ) { if (isset($this->{$property})) { $arguments[] = $option; $arguments[] = $this->{$property}; @@ -342,7 +345,7 @@ class Video $to = null ) { if (!$this->checkCommand([$this->config->avconv, '-version'])) { - throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.'); + throw new Exception(_('Can\'t find avconv or ffmpeg at ') . $this->config->avconv . '.'); } $durationRegex = '/(\d+:)?(\d+:)?(\d+)/'; @@ -355,14 +358,14 @@ class Video if (!empty($from)) { if (!preg_match($durationRegex, $from)) { - throw new Exception(_('Invalid start time: ').$from.'.'); + throw new Exception(_('Invalid start time: ') . $from . '.'); } $afterArguments[] = '-ss'; $afterArguments[] = $from; } if (!empty($to)) { if (!preg_match($durationRegex, $to)) { - throw new Exception(_('Invalid end time: ').$to.'.'); + throw new Exception(_('Invalid end time: ') . $to . '.'); } $afterArguments[] = '-to'; $afterArguments[] = $to; @@ -379,7 +382,7 @@ class Video [ '-i', $urls[0], '-f', $filetype, - '-b:a', $audioBitrate.'k', + '-b:a', $audioBitrate . 'k', ], $afterArguments, [ @@ -441,7 +444,7 @@ class Video public function getM3uStream() { if (!$this->checkCommand([$this->config->avconv, '-version'])) { - throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.'); + throw new Exception(_('Can\'t find avconv or ffmpeg at ') . $this->config->avconv . '.'); } $urls = $this->getUrl(); diff --git a/classes/ViewFactory.php b/classes/ViewFactory.php index 95503fc..a78efd7 100644 --- a/classes/ViewFactory.php +++ b/classes/ViewFactory.php @@ -1,4 +1,5 @@ getHeader('X-Forwarded-Proto'))) { $request = $request->withUri($request->getUri()->withScheme('https')->withPort(443)); } diff --git a/composer.json b/composer.json index ee4a245..5d2c0fc 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "require-dev": { "symfony/var-dumper": "~3.4.1", - "squizlabs/php_codesniffer": "~3.4.0", + "squizlabs/php_codesniffer": "^3.5", "phpunit/phpunit": "~6.5.2", "ffmpeg/ffmpeg": "^4.1", "rg3/youtube-dl": "^2019.09", diff --git a/composer.lock b/composer.lock index b285741..2b960a4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "33493e464172695368aaecf1f8e4e563", + "content-hash": "07b922cb69b4f4dbd5e537656d559c8d", "packages": [ { "name": "aura/session", @@ -3831,16 +3831,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.4.2", + "version": "3.5.0", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" + "reference": "0afebf16a2e7f1e434920fa976253576151effe9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", - "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/0afebf16a2e7f1e434920fa976253576151effe9", + "reference": "0afebf16a2e7f1e434920fa976253576151effe9", "shasum": "" }, "require": { @@ -3878,7 +3878,7 @@ "phpcs", "standards" ], - "time": "2019-04-10T23:49:02+00:00" + "time": "2019-09-26T23:12:26+00:00" }, { "name": "symfony/console", diff --git a/controllers/BaseController.php b/controllers/BaseController.php index f96842e..e555a6f 100644 --- a/controllers/BaseController.php +++ b/controllers/BaseController.php @@ -1,4 +1,5 @@ getDownloadResponse($request, $response); } catch (PasswordException $e) { return $response->withRedirect( - $this->container->get('router')->pathFor('info').'?'.http_build_query($request->getQueryParams()) + $this->container->get('router')->pathFor('info') . + '?' . http_build_query($request->getQueryParams()) ); } catch (Exception $e) { $response->getBody()->write($e->getMessage()); @@ -76,8 +78,8 @@ class DownloadController extends BaseController $response = $response->withHeader( 'Content-Disposition', - 'attachment; filename="'. - $this->video->getFileNameWithExtension('mp3').'"' + 'attachment; filename="' . + $this->video->getFileNameWithExtension('mp3') . '"' ); $response = $response->withHeader('Content-Type', 'audio/mpeg'); @@ -153,15 +155,15 @@ class DownloadController extends BaseController $response = $response->withHeader('Content-Type', 'application/zip'); $response = $response->withHeader( 'Content-Disposition', - 'attachment; filename="'.$this->video->title.'.zip"' + 'attachment; filename="' . $this->video->title . '.zip"' ); return $response->withBody($stream); } elseif ($this->video->protocol == 'rtmp') { - $response = $response->withHeader('Content-Type', 'video/'.$this->video->ext); + $response = $response->withHeader('Content-Type', 'video/' . $this->video->ext); $body = new Stream($this->video->getRtmpStream()); } elseif ($this->video->protocol == 'm3u8' || $this->video->protocol == 'm3u8_native') { - $response = $response->withHeader('Content-Type', 'video/'.$this->video->ext); + $response = $response->withHeader('Content-Type', 'video/' . $this->video->ext); $body = new Stream($this->video->getM3uStream()); } else { $headers = (array) $this->video->http_headers; @@ -192,8 +194,8 @@ class DownloadController extends BaseController } $response = $response->withHeader( 'Content-Disposition', - 'attachment; filename="'. - $this->video->getFilename().'"' + 'attachment; filename="' . + $this->video->getFilename() . '"' ); return $response; @@ -220,7 +222,7 @@ class DownloadController extends BaseController return $response->withHeader( 'Content-Disposition', - 'attachment; filename="'.$this->video->getFileNameWithExtension('mkv') + 'attachment; filename="' . $this->video->getFileNameWithExtension('mkv') ); } @@ -269,10 +271,10 @@ class DownloadController extends BaseController { $response = $response->withHeader( 'Content-Disposition', - 'attachment; filename="'. - $this->video->getFileNameWithExtension($request->getQueryParam('customFormat')).'"' + 'attachment; filename="' . + $this->video->getFileNameWithExtension($request->getQueryParam('customFormat')) . '"' ); - $response = $response->withHeader('Content-Type', 'video/'.$request->getQueryParam('customFormat')); + $response = $response->withHeader('Content-Type', 'video/' . $request->getQueryParam('customFormat')); if ($request->isGet() || $request->isPost()) { $process = $this->video->getConvertedStream( diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 6376b85..733bbd5 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -1,4 +1,5 @@ $this->config, 'class' => 'index', 'description' => _('Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'), - 'domain' => $uri->getScheme().'://'.$uri->getAuthority(), + 'domain' => $uri->getScheme() . '://' . $uri->getAuthority(), 'canonical' => $this->getCanonicalUrl($request), 'supportedLocales' => $this->localeManager->getSupportedLocales(), 'locale' => $this->localeManager->getLocale(), @@ -110,7 +111,7 @@ class FrontController extends BaseController 'extractors' => Video::getExtractors(), 'class' => 'extractors', 'title' => _('Supported websites'), - 'description' => _('List of all supported websites from which Alltube Download '. + 'description' => _('List of all supported websites from which Alltube Download ' . 'can extract video or audio files'), 'canonical' => $this->getCanonicalUrl($request), 'locale' => $this->localeManager->getLocale(), @@ -168,10 +169,11 @@ class FrontController extends BaseController $template = 'info.tpl'; } $title = _('Video download'); - $description = _('Download video from ').$this->video->extractor_key; + $description = _('Download video from ') . $this->video->extractor_key; if (isset($this->video->title)) { $title = $this->video->title; - $description = _('Download').' "'.$this->video->title.'" '._('from').' '.$this->video->extractor_key; + $description = _('Download') . ' "' . $this->video->title . '" ' . + _('from') . ' ' . $this->video->extractor_key; } $this->view->render( $response, @@ -209,8 +211,8 @@ class FrontController extends BaseController if ($this->config->convert && $request->getQueryParam('audio')) { // We skip the info page and get directly to the download. return $response->withRedirect( - $this->container->get('router')->pathFor('download'). - '?'.http_build_query($request->getQueryParams()) + $this->container->get('router')->pathFor('download') . + '?' . http_build_query($request->getQueryParams()) ); } else { return $this->getInfoResponse($request, $response); @@ -266,7 +268,7 @@ class FrontController extends BaseController $query = $uri->getQuery(); if (!empty($query)) { - $return .= '?'.$query; + $return .= '?' . $query; } return $return; diff --git a/controllers/JsonController.php b/controllers/JsonController.php index cc36e46..bfc9d75 100644 --- a/controllers/JsonController.php +++ b/controllers/JsonController.php @@ -1,4 +1,5 @@ get( try { $app->run(); } catch (SmartyException $e) { - die('Smarty could not compile the template file: '.$e->getMessage()); + die('Smarty could not compile the template file: ' . $e->getMessage()); } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 5499101..136a553 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -1,4 +1,5 @@ assertEquals($video->getFilename(), $filename.'.'.$extension); + $this->assertEquals($video->getFilename(), $filename . '.' . $extension); } /** diff --git a/tests/ViewFactoryTest.php b/tests/ViewFactoryTest.php index c73a3b4..163eeb1 100644 --- a/tests/ViewFactoryTest.php +++ b/tests/ViewFactoryTest.php @@ -1,4 +1,5 @@