Completed
Push — master ( d1c0c8...4b87d9 )
by Lukas Kahwe
06:48
created

FOSRestExtension::loadAllowedMethodsListener()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 13
loc 13
ccs 7
cts 10
cp 0.7
rs 9.4286
c 2
b 1
f 0
cc 3
eloc 7
nc 3
nop 3
crap 3.243
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Symfony\Component\HttpFoundation\Response;
22
23
class FOSRestExtension extends Extension implements PrependExtensionInterface
24
{
25
    /**
26
     * Default sensio_framework_extra { view: { annotations: false } }.
27
     *
28
     * {@inheritdoc}
29
     */
30 6
    public function prepend(ContainerBuilder $container)
31
    {
32 6
        $configs = $container->getExtensionConfig($this->getAlias());
33 6
        $parameterBag = $container->getParameterBag();
34 6
        $configs = $parameterBag->resolveValue($configs);
35 6
        $config = $this->processConfiguration(new Configuration(), $configs);
36
37 6
        if ($config['view']['view_response_listener']['enabled']) {
38 2
            $container->prependExtensionConfig('sensio_framework_extra', ['view' => ['annotations' => false]]);
39 2
        }
40 6
    }
41
42
    /**
43
     * Loads the services based on your application configuration.
44
     *
45
     * @param array            $configs
46
     * @param ContainerBuilder $container
47
     *
48
     * @throws \InvalidArgumentException
49
     * @throws \LogicException
50
     */
51 47
    public function load(array $configs, ContainerBuilder $container)
52
    {
53 47
        $config = $this->processConfiguration(new Configuration(), $configs);
54
55 42
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
56 42
        $loader->load('context_adapters.xml');
57 42
        $loader->load('view.xml');
58 42
        $loader->load('routing.xml');
59 42
        $loader->load('request.xml');
60
61 42
        $container->getDefinition('fos_rest.routing.loader.controller')->replaceArgument(4, $config['routing_loader']['default_format']);
62 42
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);
63 42
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);
64
65 42
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(2, $config['routing_loader']['include_format']);
66 42
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(2, $config['routing_loader']['include_format']);
67 42
        $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(3, $config['routing_loader']['include_format']);
68
69
        // The validator service alias is only set if validation is enabled for the request body converter
70 42
        $validator = $config['service']['validator'];
71 42
        unset($config['service']['validator']);
72
73 42
        foreach ($config['service'] as $key => $service) {
74 42
            if (null !== $service) {
75 42
                $container->setAlias('fos_rest.'.$key, $service);
76 42
            }
77 42
        }
78
79 42
        $this->loadForm($config, $loader, $container);
80 42
        $this->loadException($config, $loader, $container);
81 40
        $this->loadBodyConverter($config, $validator, $loader, $container);
82 40
        $this->loadView($config, $loader, $container);
83
84 40
        $this->loadBodyListener($config, $loader, $container);
85 40
        $this->loadFormatListener($config, $loader, $container);
86 40
        $this->loadVersioning($config, $loader, $container);
87 40
        $this->loadParamFetcherListener($config, $loader, $container);
88 40
        $this->loadAllowedMethodsListener($config, $loader, $container);
89 40
        $this->loadAccessDeniedListener($config, $loader, $container);
90 40
        $this->loadZoneMatcherListener($config, $loader, $container);
91
92
        // Needs RequestBodyParamConverter and View Handler loaded.
93 40
        $this->loadSerializer($config, $container);
94 40
    }
95
96 42
    private function loadForm(array $config, XmlFileLoader $loader, ContainerBuilder $container)
97
    {
98 42
        if (!empty($config['disable_csrf_role'])) {
99 1
            $loader->load('forms.xml');
100 1
            $container->getDefinition('fos_rest.form.extension.csrf_disable')->replaceArgument(1, $config['disable_csrf_role']);
101 1
        }
102 42
    }
103
104 40
    private function loadAccessDeniedListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
105
    {
106 40
        if ($config['access_denied_listener']['enabled'] && !empty($config['access_denied_listener']['formats'])) {
107
            $loader->load('access_denied_listener.xml');
108
109
            $service = $container->getDefinition('fos_rest.access_denied_listener');
110
111
            if (!empty($config['access_denied_listener']['service'])) {
112
                $service->clearTag('kernel.event_listener');
113
            }
114
115
            $service->replaceArgument(0, $config['access_denied_listener']['formats']);
116
            $service->replaceArgument(1, $config['unauthorized_challenge']);
117
        }
118 40
    }
119
120 40 View Code Duplication
    private function loadAllowedMethodsListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122 40
        if ($config['allowed_methods_listener']['enabled']) {
123 1
            if (!empty($config['allowed_methods_listener']['service'])) {
124
                $service = $container->getDefinition('fos_rest.allowed_methods_listener');
125
                $service->clearTag('kernel.event_listener');
126
            }
127
128 1
            $loader->load('allowed_methods_listener.xml');
129
130 1
            $container->getDefinition('fos_rest.allowed_methods_loader')->replaceArgument(1, $config['cache_dir']);
131 1
        }
132 40
    }
133
134 40
    private function loadBodyListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
135
    {
136 40
        if ($config['body_listener']['enabled']) {
137 39
            $loader->load('body_listener.xml');
138
139 39
            $service = $container->getDefinition('fos_rest.body_listener');
140
141 39
            if (!empty($config['body_listener']['service'])) {
142
                $service->clearTag('kernel.event_listener');
143
            }
144
145 39
            $service->replaceArgument(1, $config['body_listener']['throw_exception_on_unsupported_content_type']);
146 39
            $service->addMethodCall('setDefaultFormat', array($config['body_listener']['default_format']));
147
148 39
            $container->getDefinition('fos_rest.decoder_provider')->replaceArgument(1, $config['body_listener']['decoders']);
149
150 39
            $arrayNormalizer = $config['body_listener']['array_normalizer'];
151
152 39
            if (null !== $arrayNormalizer['service']) {
153 3
                $bodyListener = $container->getDefinition('fos_rest.body_listener');
154 3
                $bodyListener->addArgument(new Reference($arrayNormalizer['service']));
155 3
                $bodyListener->addArgument($arrayNormalizer['forms']);
156 3
            }
157 39
        }
158 40
    }
159
160 40
    private function loadFormatListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
161
    {
162 40
        if ($config['format_listener']['enabled'] && !empty($config['format_listener']['rules'])) {
163 4
            $loader->load('format_listener.xml');
164
165 4
            if (!empty($config['format_listener']['service'])) {
166
                $service = $container->getDefinition('fos_rest.format_listener');
167
                $service->clearTag('kernel.event_listener');
168
            }
169
170 4
            foreach ($config['format_listener']['rules'] as &$rule) {
171 4
                if (!isset($rule['exception_fallback_format'])) {
172 4
                    $rule['exception_fallback_format'] = $rule['fallback_format'];
173 4
                }
174 4
            }
175
176 4
            $container->setParameter(
177 4
                'fos_rest.format_listener.rules',
178 4
                $config['format_listener']['rules']
179 4
            );
180
181 4 View Code Duplication
            if ($config['view']['mime_types']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182 2
                $container->getDefinition('fos_rest.format_negotiator')->replaceArgument(1, $config['view']['mime_types']['formats']);
183 2
            }
184 4
        }
185 40
    }
186
187 40
    private function loadVersioning(array $config, XmlFileLoader $loader, ContainerBuilder $container)
188
    {
189 40
        if (!empty($config['versioning']['enabled'])) {
190 2
            $loader->load('versioning.xml');
191
192 2
            $versionListener = $container->getDefinition('fos_rest.versioning.listener');
193 2
            $versionListener->replaceArgument(2, $config['versioning']['default_version']);
194
195 2
            $resolvers = [];
196 2 View Code Duplication
            if ($config['versioning']['resolvers']['query']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197 2
                $resolvers['query'] = $container->getDefinition('fos_rest.versioning.query_parameter_resolver');
198 2
                $resolvers['query']->replaceArgument(0, $config['versioning']['resolvers']['query']['parameter_name']);
199 2
            }
200 2 View Code Duplication
            if ($config['versioning']['resolvers']['custom_header']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201 2
                $resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver');
202 2
                $resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']);
203 2
            }
204 2 View Code Duplication
            if ($config['versioning']['resolvers']['media_type']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205 2
                $resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver');
206 2
                $resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']);
207 2
            }
208
209 2
            $chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver');
210 2
            foreach ($config['versioning']['guessing_order'] as $resolver) {
211 2
                if (isset($resolvers[$resolver])) {
212 2
                    $chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]);
213 2
                }
214 2
            }
215 2
        }
216 40
    }
217
218 40 View Code Duplication
    private function loadParamFetcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
    {
220 40
        if ($config['param_fetcher_listener']['enabled']) {
221 4
            $loader->load('param_fetcher_listener.xml');
222
223 4
            if (!empty($config['param_fetcher_listener']['service'])) {
224
                $service = $container->getDefinition('fos_rest.param_fetcher_listener');
225
                $service->clearTag('kernel.event_listener');
226
            }
227
228 4
            if ($config['param_fetcher_listener']['force']) {
229 1
                $container->getDefinition('fos_rest.param_fetcher_listener')->replaceArgument(1, true);
230 1
            }
231 4
        }
232 40
    }
233
234 40
    private function loadBodyConverter(array $config, $validator, XmlFileLoader $loader, ContainerBuilder $container)
235
    {
236 40
        if (empty($config['body_converter'])) {
237
            return;
238
        }
239
240 40
        if (!empty($config['body_converter']['enabled'])) {
241 2
            $loader->load('request_body_param_converter.xml');
242
243 2
            if (!empty($config['body_converter']['validation_errors_argument'])) {
244 2
                $container->getDefinition('fos_rest.converter.request_body')->replaceArgument(4, $config['body_converter']['validation_errors_argument']);
245 2
            }
246 2
        }
247
248 40
        if (!empty($config['body_converter']['validate'])) {
249 2
            $container->setAlias('fos_rest.validator', $validator);
250 2
        }
251 40
    }
252
253 40
    private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container)
254
    {
255 40
        if (!empty($config['view']['exception_wrapper_handler'])) {
256
            $container->setAlias('fos_rest.view.exception_wrapper_handler', $config['view']['exception_wrapper_handler']);
257
        }
258
259 40
        if (!empty($config['view']['jsonp_handler'])) {
260 1
            $handler = new DefinitionDecorator($config['service']['view_handler']);
261 1
            $handler->setPublic(true);
262
263 1
            $jsonpHandler = new Reference('fos_rest.view_handler.jsonp');
264 1
            $handler->addMethodCall('registerHandler', ['jsonp', [$jsonpHandler, 'createResponse']]);
265 1
            $container->setDefinition('fos_rest.view_handler', $handler);
266
267 1
            $container->getDefinition('fos_rest.view_handler.jsonp')->replaceArgument(0, $config['view']['jsonp_handler']['callback_param']);
268
269 1
            if (empty($config['view']['mime_types']['jsonp'])) {
270 1
                $config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type'];
271 1
            }
272 1
        }
273
274 40
        if ($config['view']['mime_types']['enabled']) {
275 2
            $loader->load('mime_type_listener.xml');
276
277 2
            if (!empty($config['mime_type_listener']['service'])) {
278
                $service = $container->getDefinition('fos_rest.mime_type_listener');
279
                $service->clearTag('kernel.event_listener');
280
            }
281
282 2
            $container->getDefinition('fos_rest.mime_type_listener')->replaceArgument(0, $config['view']['mime_types']);
283 2
        }
284
285 40
        if ($config['view']['view_response_listener']['enabled']) {
286 4
            $loader->load('view_response_listener.xml');
287 4
            $service = $container->getDefinition('fos_rest.view_response_listener');
288
289 4
            if (!empty($config['view_response_listener']['service'])) {
290
                $service->clearTag('kernel.event_listener');
291
            }
292
293 4
            $service->replaceArgument(1, $config['view']['view_response_listener']['force']);
294 4
        }
295
296 40
        $formats = [];
297 40 View Code Duplication
        foreach ($config['view']['formats'] as $format => $enabled) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298 40
            if ($enabled) {
299 40
                $formats[$format] = false;
300 40
            }
301 40
        }
302 40 View Code Duplication
        foreach ($config['view']['templating_formats'] as $format => $enabled) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303 40
            if ($enabled) {
304 40
                $formats[$format] = true;
305 40
            }
306 40
        }
307
308 40
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(3, $formats);
309 40
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(3, $formats);
310 40
        $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(4, $formats);
311
312 40
        foreach ($config['view']['force_redirects'] as $format => $code) {
313 40
            if (true === $code) {
314 40
                $config['view']['force_redirects'][$format] = Response::HTTP_FOUND;
315 40
            }
316 40
        }
317
318 40 View Code Duplication
        if (!is_numeric($config['view']['failed_validation'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
            $config['view']['failed_validation'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['failed_validation']);
320
        }
321
322 40
        $defaultViewHandler = $container->getDefinition('fos_rest.view_handler.default');
323 40
        $defaultViewHandler->replaceArgument(5, $formats);
324 40
        $defaultViewHandler->replaceArgument(6, $config['view']['failed_validation']);
325
326 40 View Code Duplication
        if (!is_numeric($config['view']['empty_content'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
            $config['view']['empty_content'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['empty_content']);
328
        }
329
330 40
        $defaultViewHandler->replaceArgument(7, $config['view']['empty_content']);
331 40
        $defaultViewHandler->replaceArgument(8, $config['view']['serialize_null']);
332 40
        $defaultViewHandler->replaceArgument(9, $config['view']['force_redirects']);
333 40
        $defaultViewHandler->replaceArgument(10, $config['view']['default_engine']);
334 40
    }
335
336 42
    private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container)
337
    {
338 42
        if ($config['exception']['enabled']) {
339 6
            $loader->load('exception_listener.xml');
340
341 6
            if (!empty($config['exception']['service'])) {
342
                $service = $container->getDefinition('fos_rest.exception_listener');
343
                $service->clearTag('kernel.event_listener');
344
            }
345
346 6
            if ($config['exception']['exception_controller']) {
347
                $container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, $config['exception']['exception_controller']);
348 6
            } elseif (isset($container->getParameter('kernel.bundles')['TwigBundle'])) {
349 1
                $container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, 'fos_rest.exception.twig_controller:showAction');
350 1
            }
351
352 6 View Code Duplication
            if ($config['view']['mime_types']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
353 1
                $container->getDefinition('fos_rest.exception_format_negotiator')->replaceArgument(1, $config['view']['mime_types']['formats']);
354 1
            }
355
356 6
            $exceptionController = $container->getDefinition('fos_rest.exception.controller');
357 6
            $exceptionController->replaceArgument(3, $config['exception']['codes']);
358 6
            $exceptionController->replaceArgument(4, $config['exception']['messages']);
359 6
        }
360
361 42
        foreach ($config['exception']['codes'] as $exception => $code) {
362 2
            if (!is_numeric($code)) {
363
                $config['exception']['codes'][$exception] = constant("\Symfony\Component\HttpFoundation\Response::$code");
364
            }
365
366 2
            $this->testExceptionExists($exception);
367 41
        }
368
369 41
        foreach ($config['exception']['messages'] as $exception => $message) {
370 1
            $this->testExceptionExists($exception);
371 40
        }
372 40
    }
373
374 40
    private function loadSerializer(array $config, ContainerBuilder $container)
375
    {
376 40
        $bodyConverter = $container->hasDefinition('fos_rest.converter.request_body') ? $container->getDefinition('fos_rest.converter.request_body') : null;
377 40
        $viewHandler = $container->getDefinition('fos_rest.view_handler.default');
378
379 40 View Code Duplication
        if (!empty($config['serializer']['version'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
380 1
            if ($bodyConverter) {
381
                $bodyConverter->replaceArgument(2, $config['serializer']['version']);
382
            }
383 1
            $viewHandler->addMethodCall('setExclusionStrategyVersion', array($config['serializer']['version']));
384 1
        }
385
386 40 View Code Duplication
        if (!empty($config['serializer']['groups'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
387 1
            if ($bodyConverter) {
388
                $bodyConverter->replaceArgument(1, $config['serializer']['groups']);
389
            }
390 1
            $viewHandler->addMethodCall('setExclusionStrategyGroups', array($config['serializer']['groups']));
391 1
        }
392
393 40
        $viewHandler->addMethodCall('setSerializeNullStrategy', array($config['serializer']['serialize_null']));
394 40
    }
395
396 40
    private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
397
    {
398 40
        if (!empty($config['zone'])) {
399 1
            $loader->load('zone_matcher_listener.xml');
400 1
            $zoneMatcherListener = $container->getDefinition('fos_rest.zone_matcher_listener');
401
402 1
            foreach ($config['zone'] as $zone) {
403 1
                $matcher = $this->createZoneRequestMatcher($container,
404 1
                    $zone['path'],
405 1
                    $zone['host'],
406 1
                    $zone['methods'],
407 1
                    $zone['ips']
408 1
                );
409
410 1
                $zoneMatcherListener->addMethodCall('addRequestMatcher', array($matcher));
411 1
            }
412 1
        }
413 40
    }
414
415 1
    private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null)
416
    {
417 1
        if ($methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
418
            $methods = array_map('strtoupper', (array) $methods);
419
        }
420
421 1
        $serialized = serialize(array($path, $host, $methods, $ip));
422 1
        $id = 'fos_rest.zone_request_matcher.'.md5($serialized).sha1($serialized);
423
424
        // only add arguments that are necessary
425 1
        $arguments = array($path, $host, $methods, $ip);
426 1
        while (count($arguments) > 0 && !end($arguments)) {
427 1
            array_pop($arguments);
428 1
        }
429
430
        $container
431 1
            ->setDefinition($id, new DefinitionDecorator('fos_rest.zone_request_matcher'))
432 1
            ->setArguments($arguments)
433
        ;
434
435 1
        return new Reference($id);
436
    }
437
438
    /**
439
     * Checks if an exception is loadable.
440
     *
441
     * @param string $exception Class to test
442
     *
443
     * @throws \InvalidArgumentException if the class was not found.
444
     */
445 3
    private function testExceptionExists($exception)
446
    {
447 3
        if (!is_subclass_of($exception, \Exception::class) && !is_a($exception, \Exception::class, true)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Exception::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
448 2
            throw new \InvalidArgumentException("FOSRestBundle exception mapper: Could not load class '$exception' or the class does not extend from '\Exception'. Most probably this is a configuration problem.");
449
        }
450 1
    }
451
}
452