Completed
Pull Request — 2.x (#2227)
by
unknown
03:46
created

JMSHandlerRegistryV2   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 45
loc 45
ccs 12
cts 19
cp 0.6316
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A registerSubscribingHandler() 4 4 1
A registerHandler() 4 4 1
A getHandler() 16 16 5

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 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\Serializer;
13
14
use JMS\Serializer\Handler\SubscribingHandlerInterface;
15
use JMS\Serializer\Handler\HandlerRegistryInterface;
16
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17
18
/**
19
 * Search in the class parents to find an adapted handler.
20
 *
21
 * @author Ener-Getick <[email protected]>
22
 *
23
 * @internal do not depend on this class directly
24
 */
25 View Code Duplication
final class JMSHandlerRegistryV2 implements HandlerRegistryInterface
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...
26
{
27
    private $registry;
28
29 10
    public function __construct(HandlerRegistryInterface $registry)
30
    {
31 10
        $this->registry = $registry;
32 10
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function registerSubscribingHandler(SubscribingHandlerInterface $handler): void
38
    {
39
        $this->registry->registerSubscribingHandler($handler);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function registerHandler(int $direction, string $typeName, string $format, $handler): void
46
    {
47
        $this->registry->registerHandler($direction, $typeName, $format, $handler);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 9
    public function getHandler(int $direction, string $typeName, string $format)
54
    {
55 9
        $first = true;
56
        do {
57 9
            $handler = $this->registry->getHandler($direction, $typeName, $format);
58 9
            if (null !== $handler) {
59 9
                if (!$first && FlattenException::class !== $typeName) {
60 3
                    @trigger_error(sprintf('The behavior of %s is deprecated since FOSRestBundle 2.8. In version 3.0, it will only force JMS Serializer to use the %s handler when possible. You should not rely on it for the parent type "%s".', __CLASS__, FlattenException::class, $typeName), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
                }
62
63 9
                return $handler;
64
            }
65
66 3
            $first = false;
67 3
        } while ($typeName = get_parent_class($typeName));
68
    }
69
}
70