Skip to content

Commit 6426b34

Browse files
committed
refactor: phpstan code cleanup
1 parent 799e1d9 commit 6426b34

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/Transcoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author nystudio107
3939
* @package Transcode
4040
* @since 1.0.0
41+
* @method Settings getSettings()
4142
*/
4243
class Transcoder extends Plugin
4344
{

src/controllers/DefaultController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use craft\helpers\Path as PathHelper;
1717
use craft\web\Controller;
1818
use nystudio107\transcoder\Transcoder;
19+
use yii\base\ExitException;
1920
use yii\web\BadRequestHttpException;
21+
use function count;
22+
use function is_array;
2023

2124
/**
2225
* @author nystudio107
@@ -28,11 +31,6 @@ class DefaultController extends Controller
2831
// Protected Properties
2932
// =========================================================================
3033

31-
/**
32-
* @var bool|array Allows anonymous access to this controller's actions.
33-
* The actions must be in 'kebab-case'
34-
* @access protected
35-
*/
3634
protected $allowAnonymous = [
3735
'download-file',
3836
'progress',
@@ -59,7 +57,7 @@ public function beforeAction($action)
5957
*
6058
* @param $url
6159
*
62-
* @throws \yii\base\ExitException
60+
* @throws ExitException
6361
*/
6462
public function actionDownloadFile($url)
6563
{
@@ -105,7 +103,7 @@ public function actionProgress($filename)
105103
if ($content) {
106104
// get duration of source
107105
preg_match('/Duration: (.*?), start:/', $content, $matches);
108-
if (\count($matches) > 0) {
106+
if (count($matches) > 0) {
109107
$rawDuration = $matches[1];
110108

111109
// rawDuration is in 00:00:00.00 format. This converts it to seconds.
@@ -126,7 +124,7 @@ public function actionProgress($filename)
126124
$rawTime = array_pop($matches);
127125

128126
// this is needed if there is more than one match
129-
if (\is_array($rawTime)) {
127+
if (is_array($rawTime)) {
130128
$rawTime = array_pop($rawTime);
131129
}
132130

src/services/Transcode.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
111111
$filePath = $this->getAssetPath($filePath);
112112

113113
if (!empty($filePath)) {
114-
$destVideoPath = $settings['transcoderPaths']['video'] . $subfolder ?? $settings['transcoderPaths']['default'];
114+
$destVideoPath = $settings['transcoderPaths']['video'] ?? $settings['transcoderPaths']['default'];
115+
$destVideoPath .= $subfolder;
115116
$destVideoPath = Craft::parseEnv($destVideoPath);
116117
$videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions);
117118

@@ -205,7 +206,8 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
205206

206207
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
207208
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
208-
$url = $settings['transcoderUrls']['video'] . $subfolder ?? $settings['transcoderUrls']['default'];
209+
$url = $settings['transcoderUrls']['video'] ?? $settings['transcoderUrls']['default'];
210+
$url .= $subfolder;
209211
$result = Craft::parseEnv($url) . $destVideoFile;
210212
// skip encoding
211213
} elseif (!$generate) {
@@ -226,7 +228,7 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
226228
/**
227229
* Returns a URL to a video thumbnail
228230
*
229-
* @param string $filePath path to the original video or an Asset
231+
* @param Asset|string $filePath path to the original video or an Asset
230232
* @param array $thumbnailOptions of options for the thumbnail
231233
* @param bool $generate whether the thumbnail should be
232234
* generated if it doesn't exists
@@ -248,7 +250,8 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
248250
$filePath = $this->getAssetPath($filePath);
249251

250252
if (!empty($filePath)) {
251-
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] . $subfolder ?? $settings['transcoderPaths']['default'];
253+
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] ?? $settings['transcoderPaths']['default'];
254+
$destThumbnailPath .= $subfolder;
252255
$destThumbnailPath = Craft::parseEnv($destThumbnailPath);
253256

254257
$thumbnailOptions = $this->coalesceOptions('defaultThumbnailOptions', $thumbnailOptions);
@@ -308,7 +311,8 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
308311
if ($asPath) {
309312
$result = $destThumbnailPath;
310313
} else {
311-
$url = $settings['transcoderUrls']['thumbnail'] . $subfolder ?? $settings['transcoderUrls']['default'];
314+
$url = $settings['transcoderUrls']['thumbnail'] ?? $settings['transcoderUrls']['default'];
315+
$url .= $subfolder;
312316
$result = Craft::parseEnv($url) . $destThumbnailFile;
313317
}
314318
}
@@ -339,7 +343,8 @@ public function getAudioUrl($filePath, $audioOptions): string
339343
$filePath = $this->getAssetPath($filePath);
340344

341345
if (!empty($filePath)) {
342-
$destAudioPath = $settings['transcoderPaths']['audio'] . $subfolder ?? $settings['transcoderPaths']['default'];
346+
$destAudioPath = $settings['transcoderPaths']['audio'] ?? $settings['transcoderPaths']['default'];
347+
$destAudioPath .= $subfolder;
343348
$destAudioPath = Craft::parseEnv($destAudioPath);
344349

345350
$audioOptions = $this->coalesceOptions('defaultAudioOptions', $audioOptions);
@@ -434,15 +439,17 @@ public function getAudioUrl($filePath, $audioOptions): string
434439

435440
// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
436441
if (file_exists($destAudioPath) && (@filemtime($destAudioPath) >= @filemtime($filePath))) {
437-
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
442+
$url = $settings['transcoderUrls']['audio'] ?? $settings['transcoderUrls']['default'];
443+
$url .= $subfolder;
438444
$result = Craft::parseEnv($url) . $destAudioFile;
439445
} else {
440446
// Kick off the transcoding
441447
$pid = $this->executeShellCommand($ffmpegCmd);
442448

443449
if ($synchronous) {
444450
Craft::info($ffmpegCmd, __METHOD__);
445-
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
451+
$url = $settings['transcoderUrls']['audio'] ?? $settings['transcoderUrls']['default'];
452+
$url .= $subfolder;
446453
$result = Craft::parseEnv($url) . $destAudioFile;
447454
} else {
448455
Craft::info($ffmpegCmd . "\nffmpeg PID: " . $pid, __METHOD__);
@@ -619,13 +626,13 @@ public function handleGetAssetThumbPath(AssetThumbEvent $event)
619626
/**
620627
* Returns a URL to a encoded GIF file (mp4)
621628
*
622-
* @param string $filePath path to the original video or an Asset
629+
* @param Asset|string $filePath path to the original video or an Asset
623630
* @param array $gifOptions of options for the GIF file
624631
*
625632
* @return string|false|null URL or path of the GIF file
626633
*/
627634

628-
public function getGifUrl($filePath, $gifOptions): string
635+
public function getGifUrl($filePath, $gifOptions)
629636
{
630637
$result = '';
631638
$settings = Transcoder::$plugin->getSettings();
@@ -640,7 +647,8 @@ public function getGifUrl($filePath, $gifOptions): string
640647

641648
if (!empty($filePath)) {
642649
// Dest path
643-
$destVideoPath = $settings['transcoderPaths']['gif'] . $subfolder ?? $settings['transcoderPaths']['default'];
650+
$destVideoPath = $settings['transcoderPaths']['gif'] ?? $settings['transcoderPaths']['default'];
651+
$destVideoPath .= $subfolder;
644652
$destVideoPath = Craft::parseEnv($destVideoPath);
645653

646654
// Options
@@ -696,7 +704,8 @@ public function getGifUrl($filePath, $gifOptions): string
696704

697705
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
698706
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
699-
$url = $settings['transcoderUrls']['gif'] . $subfolder ?? $settings['transcoderUrls']['default'];
707+
$url = $settings['transcoderUrls']['gif'] ?? $settings['transcoderUrls']['default'];
708+
$url .= $subfolder;
700709
$result = Craft::parseEnv($url) . $destVideoFile;
701710
} else {
702711
// Kick off the transcoding
@@ -736,7 +745,7 @@ protected function getFilename($filePath, $options): string
736745

737746
// Add our options to the file name
738747
foreach ($options as $key => $value) {
739-
if (!empty($value)) {
748+
if (isset($value)) {
740749
$suffix = '';
741750
if (!empty(self::SUFFIX_MAP[$key])) {
742751
$suffix = self::SUFFIX_MAP[$key];

0 commit comments

Comments
 (0)