Completed
Push — master ( dc44ee...94a152 )
by Guilh
05:14 queued 40s
created

FOSRestExtension::loadException()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 34
Code Lines 18

Duplication

Lines 3
Ratio 8.82 %

Code Coverage

Tests 19
CRAP Score 9.2485

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 34
ccs 19
cts 26
cp 0.7308
rs 5.3846
cc 8
eloc 18
nc 54
nop 3
crap 9.2485
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
    public 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(0, $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
            } else {
200
                $container->removeDefinition('fos_rest.versioning.query_parameter_resolver');
201
            }
202
203 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...
204 2
                $resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver');
205 2
                $resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']);
206 2
            } else {
207
                $container->removeDefinition('fos_rest.versioning.header_resolver');
208
            }
209
210 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...
211 2
                $resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver');
212 2
                $resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']);
213 2
            } else {
214
                $container->removeDefinition('fos_rest.versioning.media_type_resolver');
215
            }
216
217 2
            $chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver');
218 2
            foreach ($config['versioning']['guessing_order'] as $resolver) {
219 2
                if (isset($resolvers[$resolver])) {
220 2
                    $chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]);
221 2
                }
222 2
            }
223 2
        }
224 40
    }
225
226 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...
227
    {
228 40
        if ($config['param_fetcher_listener']['enabled']) {
229 4
            $loader->load('param_fetcher_listener.xml');
230
231 4
            if (!empty($config['param_fetcher_listener']['service'])) {
232
                $service = $container->getDefinition('fos_rest.param_fetcher_listener');
233
                $service->clearTag('kernel.event_listener');
234
            }
235
236 4
            if ($config['param_fetcher_listener']['force']) {
237 1
                $container->getDefinition('fos_rest.param_fetcher_listener')->replaceArgument(1, true);
238 1
            }
239 4
        }
240 40
    }
241
242 40
    private function loadBodyConverter(array $config, $validator, XmlFileLoader $loader, ContainerBuilder $container)
243
    {
244 40
        if (empty($config['body_converter'])) {
245
            return;
246
        }
247
248 40
        if (!empty($config['body_converter']['enabled'])) {
249 2
            $loader->load('request_body_param_converter.xml');
250
251 2
            if (!empty($config['body_converter']['validation_errors_argument'])) {
252 2
                $container->getDefinition('fos_rest.converter.request_body')->replaceArgument(4, $config['body_converter']['validation_errors_argument']);
253 2
            }
254 2
        }
255
256 40
        if (!empty($config['body_converter']['validate'])) {
257 2
            $container->setAlias('fos_rest.validator', $validator);
258 2
        }
259 40
    }
260
261 40
    private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container)
262
    {
263 40
        if (!empty($config['view']['exception_wrapper_handler'])) {
264
            $container->setAlias('fos_rest.view.exception_wrapper_handler', $config['view']['exception_wrapper_handler']);
265
        }
266
267 40
        if (!empty($config['view']['jsonp_handler'])) {
268 1
            $handler = new DefinitionDecorator($config['service']['view_handler']);
269 1
            $handler->setPublic(true);
270
271 1
            $jsonpHandler = new Reference('fos_rest.view_handler.jsonp');
272 1
            $handler->addMethodCall('registerHandler', ['jsonp', [$jsonpHandler, 'createResponse']]);
273 1
            $container->setDefinition('fos_rest.view_handler', $handler);
274
275 1
            $container->getDefinition('fos_rest.view_handler.jsonp')->replaceArgument(0, $config['view']['jsonp_handler']['callback_param']);
276
277 1
            if (empty($config['view']['mime_types']['jsonp'])) {
278 1
                $config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type'];
279 1
            }
280 1
        }
281
282 40
        if ($config['view']['mime_types']['enabled']) {
283 2
            $loader->load('mime_type_listener.xml');
284
285 2
            if (!empty($config['mime_type_listener']['service'])) {
286
                $service = $container->getDefinition('fos_rest.mime_type_listener');
287
                $service->clearTag('kernel.event_listener');
288
            }
289
290 2
            $container->getDefinition('fos_rest.mime_type_listener')->replaceArgument(0, $config['view']['mime_types']);
291 2
        }
292
293 40
        if ($config['view']['view_response_listener']['enabled']) {
294 4
            $loader->load('view_response_listener.xml');
295
296 4
            if (!empty($config['view_response_listener']['service'])) {
297
                $service = $container->getDefinition('fos_rest.view_response_listener');
298
                $service->clearTag('kernel.event_listener');
299
            }
300
301 4
            $container->setParameter('fos_rest.view_response_listener.force_view', $config['view']['view_response_listener']['force']);
302 4
        }
303
304 40
        $formats = [];
305 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...
306 40
            if ($enabled) {
307 40
                $formats[$format] = false;
308 40
            }
309 40
        }
310 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...
311 40
            if ($enabled) {
312 40
                $formats[$format] = true;
313 40
            }
314 40
        }
315
316 40
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(3, $formats);
317 40
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(3, $formats);
318 40
        $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(4, $formats);
319 40
        $container->getDefinition('fos_rest.view_handler.default')->replaceArgument(0, $formats);
320
321 40
        foreach ($config['view']['force_redirects'] as $format => $code) {
322 40
            if (true === $code) {
323 40
                $config['view']['force_redirects'][$format] = Response::HTTP_FOUND;
324 40
            }
325 40
        }
326
327 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...
328
            $config['view']['failed_validation'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['failed_validation']);
329
        }
330
331 40
        $defaultViewHandler = $container->getDefinition('fos_rest.view_handler.default');
332 40
        $defaultViewHandler->replaceArgument(1, $config['view']['failed_validation']);
333
334 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...
335
            $config['view']['empty_content'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['empty_content']);
336
        }
337
338 40
        $defaultViewHandler->replaceArgument(2, $config['view']['empty_content']);
339 40
        $defaultViewHandler->replaceArgument(3, $config['view']['serialize_null']);
340 40
        $defaultViewHandler->replaceArgument(4, $config['view']['force_redirects']);
341 40
        $defaultViewHandler->replaceArgument(5, $config['view']['default_engine']);
342 40
    }
343
344 42
    private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container)
345
    {
346 42
        if ($config['exception']['enabled']) {
347 6
            $loader->load('exception_listener.xml');
348
349 6
            if (!empty($config['exception']['service'])) {
350
                $service = $container->getDefinition('fos_rest.exception_listener');
351
                $service->clearTag('kernel.event_listener');
352
            }
353
354 6
            if ($config['exception']['exception_controller']) {
355
                $container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, $config['exception']['exception_controller']);
356
            }
357
358 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...
359 1
                $container->getDefinition('fos_rest.exception_format_negotiator')->replaceArgument(1, $config['view']['mime_types']['formats']);
360 1
            }
361 6
        }
362
363 42
        foreach ($config['exception']['codes'] as $exception => $code) {
364 2
            if (!is_numeric($code)) {
365
                $config['exception']['codes'][$exception] = constant("\Symfony\Component\HttpFoundation\Response::$code");
366
            }
367
368 2
            $this->testExceptionExists($exception);
369 41
        }
370
371 41
        foreach ($config['exception']['messages'] as $exception => $message) {
372 1
            $this->testExceptionExists($exception);
373 40
        }
374
375 40
        $container->setParameter('fos_rest.exception.codes', $config['exception']['codes']);
376 40
        $container->setParameter('fos_rest.exception.messages', $config['exception']['messages']);
377 40
    }
378
379 40
    private function loadSerializer(array $config, ContainerBuilder $container)
380
    {
381 40
        $bodyConverter = $container->hasDefinition('fos_rest.converter.request_body') ? $container->getDefinition('fos_rest.converter.request_body') : null;
382 40
        $viewHandler = $container->getDefinition('fos_rest.view_handler.default');
383
384 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...
385 1
            if ($bodyConverter) {
386
                $bodyConverter->replaceArgument(2, $config['serializer']['version']);
387
            }
388 1
            $viewHandler->addMethodCall('setExclusionStrategyVersion', array($config['serializer']['version']));
389 1
        }
390
391 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...
392 1
            if ($bodyConverter) {
393
                $bodyConverter->replaceArgument(1, $config['serializer']['groups']);
394
            }
395 1
            $viewHandler->addMethodCall('setExclusionStrategyGroups', array($config['serializer']['groups']));
396 1
        }
397
398 40
        $viewHandler->addMethodCall('setSerializeNullStrategy', array($config['serializer']['serialize_null']));
399 40
    }
400
401 40
    private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
402
    {
403 40
        if (!empty($config['zone'])) {
404 1
            $loader->load('zone_matcher_listener.xml');
405 1
            $zoneMatcherListener = $container->getDefinition('fos_rest.zone_matcher_listener');
406
407 1
            foreach ($config['zone'] as $zone) {
408 1
                $matcher = $this->createZoneRequestMatcher($container,
409 1
                    $zone['path'],
410 1
                    $zone['host'],
411 1
                    $zone['methods'],
412 1
                    $zone['ips']
413 1
                );
414
415 1
                $zoneMatcherListener->addMethodCall('addRequestMatcher', array($matcher));
416 1
            }
417 1
        }
418 40
    }
419
420 1
    private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null)
421
    {
422 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...
423
            $methods = array_map('strtoupper', (array) $methods);
424
        }
425
426 1
        $serialized = serialize(array($path, $host, $methods, $ip));
427 1
        $id = 'fos_rest.zone_request_matcher.'.md5($serialized).sha1($serialized);
428
429
        // only add arguments that are necessary
430 1
        $arguments = array($path, $host, $methods, $ip);
431 1
        while (count($arguments) > 0 && !end($arguments)) {
432 1
            array_pop($arguments);
433 1
        }
434
435
        $container
436 1
            ->setDefinition($id, new DefinitionDecorator('fos_rest.zone_request_matcher'))
437 1
            ->setArguments($arguments)
438
        ;
439
440 1
        return new Reference($id);
441
    }
442
443
    /**
444
     * Checks if an exception is loadable.
445
     *
446
     * @param string $exception Class to test
447
     *
448
     * @throws \InvalidArgumentException if the class was not found.
449
     */
450 3
    private function testExceptionExists($exception)
451
    {
452 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...
453 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.");
454
        }
455 1
    }
456
}
457