Completed
Push — master ( 183b0a...e3c17c )
by Kévin
03:20
created

PropertyFilter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 41
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A apply() 12 12 4
A getDescription() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Serializer\Filter;
15
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Property filter.
20
 *
21
 * @author Baptiste Meyer <[email protected]>
22
 */
23 View Code Duplication
final class PropertyFilter implements FilterInterface
0 ignored issues
show
Duplication introduced by
This class 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...
24
{
25
    private $overrideDefaultProperties;
26
    private $parameterName;
27
28
    public function __construct(string $parameterName = 'properties', bool $overrideDefaultProperties = false)
29
    {
30
        $this->overrideDefaultProperties = $overrideDefaultProperties;
31
        $this->parameterName = $parameterName;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function apply(Request $request, bool $normalization, array $attributes, array &$context)
38
    {
39
        if (!is_array($properties = $request->query->get($this->parameterName))) {
40
            return;
41
        }
42
43
        if (!$this->overrideDefaultProperties && isset($context['attributes'])) {
44
            $properties = array_merge_recursive((array) $context['attributes'], $properties);
45
        }
46
47
        $context['attributes'] = $properties;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getDescription(string $resourceClass): array
54
    {
55
        return [
56
            $this->parameterName.'[]' => [
57
                'property' => null,
58
                'type' => 'string',
59
                'required' => false,
60
            ],
61
        ];
62
    }
63
}
64