Conditions | 7 |
Paths | 15 |
Total Lines | 56 |
Lines | 3 |
Ratio | 5.36 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function sideload($url) |
||
32 | { |
||
33 | if (!self::validateUrl($url)) { |
||
34 | $this->addError('load', Yii::t('app', 'Empty or incorrect URL')); |
||
35 | |||
36 | return false; |
||
37 | } |
||
38 | |||
39 | $youtubeParams = [ |
||
40 | 'restrict-filenames' => true, |
||
41 | 'format' => 'best[ext=mp4]/best[ext=flv]', |
||
42 | ]; |
||
43 | if (Yii::$app->params['proxy']) { |
||
44 | $youtubeParams['proxy'] = Yii::$app->params['proxy']; |
||
45 | } |
||
46 | $dl = new YoutubeDl($youtubeParams); |
||
47 | $dl->setDownloadPath(sys_get_temp_dir()); |
||
48 | |||
49 | try { |
||
50 | $video = $dl->download($url); |
||
51 | if (!$video) { |
||
52 | $this->addError('load', Yii::t('app', 'Downloading failed')); |
||
53 | |||
54 | return false; |
||
55 | } |
||
56 | } catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) { |
||
57 | $this->addError('load', Yii::t('app', 'Downloading error')); |
||
58 | Yii::error($e->getMessage()); |
||
59 | |||
60 | return false; |
||
61 | } catch (\YoutubeDl\Exception\NotFoundException $e) { |
||
62 | $this->addError('load', Yii::t('app', 'Media not found!')); |
||
63 | Yii::error($e->getMessage()); |
||
64 | |||
65 | return false; |
||
66 | } |
||
67 | |||
68 | $filename = $video->getFilename(); |
||
69 | $tmpFilepath = sys_get_temp_dir().'/'.$filename; |
||
70 | |||
71 | $fileInstance = new UploadedFile(); |
||
72 | $fileInstance->name = $filename; |
||
73 | $fileInstance->tempName = $tmpFilepath; |
||
74 | $fileInstance->type = FileHelper::getMimeType($fileInstance->tempName); |
||
75 | $fileInstance->size = $video->getFile()->getSize(); |
||
76 | $this->upload = $fileInstance; |
||
77 | |||
78 | View Code Duplication | if (static::validateFile($fileInstance->tempName)) { |
|
|
|||
79 | return ['filename' => $filename, 'tmppath' => $tmpFilepath, 'duration' => static::getDuration($tmpFilepath)]; |
||
80 | } |
||
81 | |||
82 | $this->addError('load', Yii::t('app', 'Invalid file')); |
||
83 | unlink($fileInstance->tempName); |
||
84 | |||
85 | return false; |
||
86 | } |
||
87 | } |
||
88 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.