Completed
Push — master ( cd0400...f5eb40 )
by Pascal
02:09
created

Media::addFilter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use Closure;
6
use FFMpeg\Coordinate\TimeCode;
7
use FFMpeg\Media\MediaTypeInterface;
8
9
/**
10
 * @method mixed save(FormatInterface $format, $outputPathfile)
11
 */
12
class Media
13
{
14
    protected $file;
15
16
    protected $media;
17
18
    public function __construct(File $file, MediaTypeInterface $media)
19
    {
20
        $this->file  = $file;
21
        $this->media = $media;
22
    }
23
24
    public function isFrame(): bool
25
    {
26
        return $this instanceof Frame;
27
    }
28
29
    public function getFile(): File
30
    {
31
        return $this->file;
32
    }
33
34
    public function export(): MediaExporter
35
    {
36
        return new MediaExporter($this);
37
    }
38
39
    public function getFrameFromString(string $timecode): Frame
40
    {
41
        return $this->getFrameFromTimecode(
42
            TimeCode::fromString($timecode)
43
        );
44
    }
45
46
    public function getFrameFromSeconds(float $quantity): Frame
47
    {
48
        return $this->getFrameFromTimecode(
49
            TimeCode::fromSeconds($quantity)
50
        );
51
    }
52
53
    public function getFrameFromTimecode(TimeCode $timecode): Frame
54
    {
55
        $frame = $this->media->frame($timecode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FFMpeg\Media\MediaTypeInterface as the method frame() does only exist in the following implementations of said interface: FFMpeg\Media\Video.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56
57
        return new Frame($this->getFile(), $frame);
58
    }
59
60
    public function addFilter(): Media
61
    {
62
        $arguments = func_get_args();
63
64
        if (isset($arguments[0]) && $arguments[0] instanceof Closure) {
65
            call_user_func_array($arguments[0], [$this->media->filters()]);
66
        } else {
67
            call_user_func_array([$this->media, 'addFilter'], $arguments);
68
        }
69
70
        return $this;
71
    }
72
73
    protected function selfOrArgument($argument)
74
    {
75
        return ($argument === $this->media) ? $this : $argument;
76
    }
77
78
    public function __call($method, $parameters)
79
    {
80
        return $this->selfOrArgument(
81
            call_user_func_array([$this->media, $method], $parameters)
82
        );
83
    }
84
}
85