Completed
Push — master ( 8c0a67...1fafee )
by Grégoire
9s
created

FormatterMediaExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Twig\Extension;
13
14
use Sonata\FormatterBundle\Extension\BaseProxyExtension;
15
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser;
16
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser;
17
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser;
18
19
class FormatterMediaExtension extends BaseProxyExtension
0 ignored issues
show
Bug introduced by
There is one abstract method getName in this class; you could implement it, or declare this class as abstract.
Loading history...
20
{
21
    /**
22
     * @var \Twig_Extension
23
     */
24
    protected $twigExtension;
25
26
    /**
27
     * @param \Twig_Extension $twigExtension
28
     */
29
    public function __construct(\Twig_Extension $twigExtension)
30
    {
31
        $this->twigExtension = $twigExtension;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getAllowedTags()
38
    {
39
        return array(
40
            'media',
41
            'path',
42
            'thumbnail',
43
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAllowedMethods()
50
    {
51
        return array(
52
            'Sonata\MediaBundle\Model\MediaInterface' => array(
53
                'getproviderreference',
54
            ),
55
        );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getTokenParsers()
62
    {
63
        return array(
64
            new MediaTokenParser(get_class()),
65
            new ThumbnailTokenParser(get_class()),
66
            new PathTokenParser(get_class()),
67
        );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getTwigExtension()
74
    {
75
        return $this->twigExtension;
76
    }
77
78
    /**
79
     * @param int    $media
80
     * @param string $format
81
     * @param array  $options
82
     *
83
     * @return string
84
     */
85
    public function media($media, $format, $options = array())
86
    {
87
        return $this->getTwigExtension()->media($media, $format, $options);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twig_ExtensionInterface as the method media() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension, Sonata\MediaBundle\Twig\Extension\MediaExtension.

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...
88
    }
89
90
    /**
91
     * @param int    $media
92
     * @param string $format
93
     * @param array  $options
94
     *
95
     * @return string
96
     */
97
    public function thumbnail($media, $format, $options = array())
98
    {
99
        return $this->getTwigExtension()->thumbnail($media, $format, $options);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twig_ExtensionInterface as the method thumbnail() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension, Sonata\MediaBundle\Twig\Extension\MediaExtension.

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...
100
    }
101
102
    /**
103
     * @param int    $media
104
     * @param string $format
105
     *
106
     * @return string
107
     */
108
    public function path($media, $format)
109
    {
110
        return $this->getTwigExtension()->path($media, $format);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twig_ExtensionInterface as the method path() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension, Sonata\MediaBundle\Twig\Extension\MediaExtension.

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...
111
    }
112
}
113