Passed
Push — master ( bb93e2...a22d39 )
by Pascal
02:49
created

HLSPlaylistGenerator::getStreamInfoLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Exporters;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
9
use ProtoneMedia\LaravelFFMpeg\Http\DynamicHLSPlaylist;
10
use ProtoneMedia\LaravelFFMpeg\MediaOpener;
11
12
class HLSPlaylistGenerator implements PlaylistGenerator
13
{
14
    const PLAYLIST_START = '#EXTM3U';
15
    const PLAYLIST_END   = '#EXT-X-ENDLIST';
16
17
    private function getFrameRate(MediaOpener $media)
18
    {
19
        $mediaStream = $media->getVideoStream();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mediaStream is correct as $media->getVideoStream() targeting ProtoneMedia\LaravelFFMpeg\MediaOpener::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The method getVideoStream() does not exist on ProtoneMedia\LaravelFFMpeg\MediaOpener. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        /** @scrutinizer ignore-call */ 
20
        $mediaStream = $media->getVideoStream();
Loading history...
20
21
        $frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1"));
22
23
        if (!$frameRate || Str::endsWith($frameRate, '/0')) {
24
            return null;
25
        }
26
27
        return $frameRate ? number_format($frameRate, 3, '.', '') : null;
0 ignored issues
show
Bug introduced by
$frameRate of type string is incompatible with the type double expected by parameter $number of number_format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
28
    }
29
30
    private function getStreamInfoLine(Media $playlistMedia, string $key): string
31
    {
32
        $masterPlaylist = $playlistMedia->getDisk()->get(
0 ignored issues
show
Bug introduced by
The method get() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $masterPlaylist = $playlistMedia->getDisk()->/** @scrutinizer ignore-call */ get(
Loading history...
33
            $playlistMedia->getDirectory() . HLSExporter::generateMasterPlaylistFilename($key, $playlistMedia)
0 ignored issues
show
Unused Code introduced by
The call to ProtoneMedia\LaravelFFMp...asterPlaylistFilename() has too many arguments starting with $playlistMedia. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            $playlistMedia->getDirectory() . HLSExporter::/** @scrutinizer ignore-call */ generateMasterPlaylistFilename($key, $playlistMedia)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
34
        );
35
36
        $lines = DynamicHLSPlaylist::parseLines($masterPlaylist)->filter();
37
38
        return $lines->get($lines->search($playlistMedia->getFilename()) - 1);
39
    }
40
41
    public function get(array $playlistMedia, PHPFFMpeg $driver): string
42
    {
43
        return Collection::make($playlistMedia)->map(function (Media $playlistMedia, $key) use ($driver) {
44
            $media = (new MediaOpener($playlistMedia->getDisk(), $driver))
45
                ->openWithInputOptions($playlistMedia->getPath(), ['-allowed_extensions', 'ALL']);
46
47
            $streamInfoLine = $this->getStreamInfoLine($playlistMedia, $key);
48
49
            if ($frameRate = $this->getFrameRate($media)) {
50
                $streamInfoLine .= ",FRAME-RATE={$frameRate}";
51
            }
52
53
            return [$streamInfoLine, $playlistMedia->getFilename()];
54
        })->collapse()
55
            ->prepend(static::PLAYLIST_START)
56
            ->push(static::PLAYLIST_END)
57
            ->implode(PHP_EOL);
58
    }
59
}
60