Completed
Push — 1.0 ( 8e636c...f10ae8 )
by Rob
02:02
created

OptionsResolver::setupResolverLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Utility\OptionsResolver;
13
14
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
15
use Symfony\Component\OptionsResolver\OptionsResolver as BaseOptionsResolver;
16
17
/**
18
 * Provides a compatible interface for Symfony's newest OptionsResolver implementation, while internally supporting
19
 * legacy variants of the interface. This allows for a clean migration plan in our 2.x branch where any classes
20
 * implementing this must simple "use" the real "Symfony\Component\OptionsResolver\OptionsResolver" class without any
21
 * changes to usage within the code (once support for Symfony <2.7 is dropped).
22
 *
23
 * @deprecated Deprecated in v1.7 and scheduled for removal in v2.0.x
24
 */
25
class OptionsResolver
26
{
27
    /**
28
     * @var array
29
     */
30
    private $defined = array();
31
32
    /**
33
     * @var array
34
     */
35
    private $required = array();
36
37
    /**
38
     * @var array
39
     */
40
    private $defaults = array();
41
42
    /**
43
     * @var array
44
     */
45
    private $allowedValues = array();
46
47
    /**
48
     * @var array
49
     */
50
    private $allowedTypes = array();
51
52
    /**
53
     * @var array
54
     */
55
    private $normalizers = array();
56
57
    /**
58
     * @param array $defined
59
     *
60
     * @return $this
61
     */
62
    public function setDefined(array $defined)
63
    {
64
        $this->defined = $defined;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $option
71
     * @param mixed  $value
72
     *
73
     * @return $this
74
     */
75
    public function setDefault($option, $value)
76
    {
77
        $this->defaults[$option] = $value;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param array $options
84
     *
85
     * @return $this
86
     */
87
    public function setRequired(array $options)
88
    {
89
        $this->required = $options;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string  $option
96
     * @param mixed[] $values
97
     *
98
     * @return $this
99
     */
100
    public function setAllowedValues($option, array $values)
101
    {
102
        $this->allowedValues[$option] = $values;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string  $option
109
     * @param mixed[] $types
110
     *
111
     * @return $this
112
     */
113
    public function setAllowedTypes($option, array $types)
114
    {
115
        $this->allowedTypes[$option] = $types;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string   $option
122
     * @param \Closure $normalizer
123
     *
124
     * @return $this
125
     */
126
    public function setNormalizer($option, \Closure $normalizer)
127
    {
128
        $this->normalizers[$option] = $normalizer;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param array $options
135
     *
136
     * @return array
137
     */
138
    public function resolve(array $options)
139
    {
140
        $resolver = new BaseOptionsResolver();
141
        $resolver->setDefaults($this->defaults);
142
        $resolver->setRequired($this->required);
143
144
        if (SymfonyFramework::isKernelGreaterThanOrEqualTo(2, 7)) {
145
            $this->setupResolver($resolver);
146
        } else {
147
            $this->setupResolverLegacy($resolver);
148
        }
149
150
        return $resolver->resolve($options);
151
    }
152
153
    /**
154
     * @param BaseOptionsResolver $resolver
155
     */
156
    private function setupResolver(BaseOptionsResolver $resolver)
157
    {
158
        $resolver->setDefined($this->defined);
159
160
        foreach ($this->allowedValues as $option => $values) {
161
            $resolver->setAllowedValues($option, $values);
162
        }
163
164
        foreach ($this->allowedTypes as $option => $types) {
165
            $resolver->setAllowedTypes($option, $types);
166
        }
167
168
        foreach ($this->normalizers as $option => $normalizer) {
169
            $resolver->setNormalizer($option, $normalizer);
170
        }
171
    }
172
173
    /**
174
     * @param BaseOptionsResolver $resolver
175
     */
176
    private function setupResolverLegacy(BaseOptionsResolver $resolver)
177
    {
178
        $resolver->setOptional($this->defined);
0 ignored issues
show
Bug introduced by
The method setOptional() does not seem to exist on object<Symfony\Component...solver\OptionsResolver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
        $resolver->setAllowedValues($this->allowedValues);
0 ignored issues
show
Bug introduced by
The call to setAllowedValues() misses a required argument $allowedValues.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$this->allowedValues is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
        $resolver->setAllowedTypes($this->allowedTypes);
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$this->allowedTypes is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
        $resolver->setNormalizers($this->normalizers);
0 ignored issues
show
Bug introduced by
The method setNormalizers() does not exist on Symfony\Component\OptionsResolver\OptionsResolver. Did you maybe mean setNormalizer()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
182
    }
183
}
184