Completed
Pull Request — 1.0 (#920)
by Rob
10:30
created

OptionsResolver::setNormalizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
 * @deprecated Deprecated in v1.7.x and scheduled for removal in v2.0.x
19
 */
20
class OptionsResolver
21
{
22
    /**
23
     * @var array
24
     */
25
    private $required = array();
26
27
    /**
28
     * @var array
29
     */
30
    private $defaults = array();
31
32
    /**
33
     * @var array
34
     */
35
    private $allowedValues = array();
36
37
    /**
38
     * @var array
39
     */
40
    private $allowedTypes = array();
41
42
    /**
43
     * @var array
44
     */
45
    private $normalizers = array();
46
47
    /**
48
     * @param string $option
49
     * @param mixed  $value
50
     */
51
    public function setDefault($option, $value)
52
    {
53
        $this->defaults[$option] = $value;
54
    }
55
56
    /**
57
     * @param array $options
58
     */
59
    public function setRequired(array $options)
60
    {
61
        $this->required = $options;
62
    }
63
64
    /**
65
     * @param string  $option
66
     * @param mixed[] $values
67
     *
68
     * @return $this
69
     */
70
    public function setAllowedValues($option, array $values)
71
    {
72
        $this->allowedValues[$option] = $values;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string  $option
79
     * @param mixed[] $types
80
     *
81
     * @return $this
82
     */
83
    public function setAllowedTypes($option, array $types)
84
    {
85
        $this->allowedTypes[$option] = $types;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string   $option
92
     * @param \Closure $normalizer
93
     *
94
     * @return $this
95
     */
96
    public function setNormalizer($option, \Closure $normalizer)
97
    {
98
        $this->normalizers[$option] = $normalizer;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param array $options
105
     *
106
     * @return array
107
     */
108
    public function resolve(array $options)
109
    {
110
        $resolver = new BaseOptionsResolver();
111
        $resolver->setDefaults($this->defaults);
112
        $resolver->setRequired($this->required);
113
114
        if (SymfonyFramework::isKernelGreaterThanOrEqualTo(2, 7)) {
115
            $this->setupResolver($resolver);
116
        } else {
117
            $this->setupResolverLegacy($resolver);
118
        }
119
120
        return $resolver->resolve($options);
121
    }
122
123
    /**
124
     * @param BaseOptionsResolver $resolver
125
     */
126
    private function setupResolver(BaseOptionsResolver $resolver)
127
    {
128
        foreach ($this->allowedValues as $option => $values) {
129
            $resolver->setAllowedValues($option, $values);
130
        }
131
132
        foreach ($this->allowedTypes as $option => $types) {
133
            $resolver->setAllowedTypes($option, $types);
134
        }
135
136
        foreach ($this->normalizers as $option => $normalizer) {
137
            $resolver->setNormalizer($option, $normalizer);
138
        }
139
    }
140
141
    /**
142
     * @param BaseOptionsResolver $resolver
143
     */
144
    private function setupResolverLegacy(BaseOptionsResolver $resolver)
145
    {
146
        $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...
147
        $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...
148
        $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...
149
    }
150
}