Completed
Push — master ( 5482a9...c97bf1 )
by Grégoire
02:48
created

FormatterMediaExtension   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 0
loc 131
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAllowedTags() 0 8 1
A getAllowedMethods() 0 8 1
A getTokenParsers() 0 8 1
A getTwigExtension() 0 4 1
A media() 0 4 1
A thumbnail() 0 4 1
A path() 0 4 1
A getNodeVisitors() 0 4 1
A getFilters() 0 4 1
A getTests() 0 4 1
A getFunctions() 0 4 1
A getOperators() 0 4 1
A getAllowedFilters() 0 4 1
A getAllowedFunctions() 0 4 1
A getAllowedProperties() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Twig\Extension;
15
16
use Sonata\MediaBundle\Model\MediaInterface;
17
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser;
18
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser;
19
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser;
20
use Twig\Extension\AbstractExtension;
21
use Twig\Extension\ExtensionInterface;
22
23
class FormatterMediaExtension extends AbstractExtension implements ExtensionInterface
24
{
25
    /**
26
     * @var ExtensionInterface
27
     */
28
    protected $twigExtension;
29
30
    public function __construct(ExtensionInterface $twigExtension)
31
    {
32
        $this->twigExtension = $twigExtension;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getAllowedTags(): array
39
    {
40
        return [
41
            'media',
42
            'path',
43
            'thumbnail',
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAllowedMethods(): array
51
    {
52
        return [
53
            MediaInterface::class => [
54
                'getproviderreference',
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getTokenParsers(): array
63
    {
64
        return [
65
            new MediaTokenParser(__CLASS__),
66
            new ThumbnailTokenParser(__CLASS__),
67
            new PathTokenParser(__CLASS__),
68
        ];
69
    }
70
71
    /**
72
     * @return ExtensionInterface
73
     */
74
    public function getTwigExtension(): ExtensionInterface
75
    {
76
        return $this->twigExtension;
77
    }
78
79
    /**
80
     * @param int    $media
81
     * @param string $format
82
     * @param array  $options
83
     *
84
     * @return string
85
     */
86
    public function media($media, $format, $options = [])
87
    {
88
        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\Extension\ExtensionInterface as the method media() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension.

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...
89
    }
90
91
    /**
92
     * @param int    $media
93
     * @param string $format
94
     * @param array  $options
95
     *
96
     * @return string
97
     */
98
    public function thumbnail($media, $format, $options = [])
99
    {
100
        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\Extension\ExtensionInterface as the method thumbnail() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension.

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...
101
    }
102
103
    /**
104
     * @param int    $media
105
     * @param string $format
106
     *
107
     * @return string
108
     */
109
    public function path($media, $format)
110
    {
111
        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\Extension\ExtensionInterface as the method path() does only exist in the following implementations of said interface: Sonata\MediaBundle\Twig\...FormatterMediaExtension.

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...
112
    }
113
114
    public function getNodeVisitors()
115
    {
116
        return $this->getTwigExtension()->getNodeVisitors();
117
    }
118
119
    public function getFilters()
120
    {
121
        return $this->getTwigExtension()->getFilters();
122
    }
123
124
    public function getTests()
125
    {
126
        return $this->getTwigExtension()->getTests();
127
    }
128
129
    public function getFunctions()
130
    {
131
        return $this->getTwigExtension()->getFunctions();
132
    }
133
134
    public function getOperators()
135
    {
136
        return $this->getTwigExtension()->getOperators();
137
    }
138
139
    public function getAllowedFilters()
140
    {
141
        return [];
142
    }
143
144
    public function getAllowedFunctions()
145
    {
146
        return [];
147
    }
148
149
    public function getAllowedProperties()
150
    {
151
        return [];
152
    }
153
}
154