Completed
Pull Request — 2.0 (#1192)
by
unknown
04:16
created

RouterOperationPathResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 14.89 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 7
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B resolveOperationPath() 7 26 6

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\Bridge\Symfony\Routing;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * Resolves the operations path using a Symfony route.
22
 *
23
 * @author Guilhem N. <[email protected]>
24
 */
25
final class RouterOperationPathResolver implements OperationPathResolverInterface
26
{
27
    private $router;
28
    private $deferred;
29
30
    public function __construct(RouterInterface $router, OperationPathResolverInterface $deferred = null)
31
    {
32
        if ($deferred) {
33
            @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.0.10 and will be removed in 3.0.', OperationPathResolverInterface::class, __METHOD__), 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...
34
        }
35
36
        $this->router = $router;
37
        $this->deferred = $deferred;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @throws InvalidArgumentException
44
     */
45
    public function resolveOperationPath(string $resourceShortName, array $operation, bool $collection/*, string $operationName = null*/): string
46
    {
47 View Code Duplication
        if (func_num_args() >= 4) {
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...
48
            $operationName = func_get_arg(3);
49
        } else {
50
            @trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.0.10.', __METHOD__), E_USER_DEPRECATED);
1 ignored issue
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...
51
52
            $operationName = null;
53
        }
54
55
        if (isset($operation['route_name'])) {
56
            $routeName = $operation['route_name'];
57
        } elseif (null !== $operationName) {
58
            $routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $collection);
59
        } elseif ($this->deferred) {
60
            return $this->deferred->resolveOperationPath($resourceShortName, $operation, $collection, $operationName);
0 ignored issues
show
Unused Code introduced by
The call to OperationPathResolverInt...:resolveOperationPath() has too many arguments starting with $operationName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
        } else {
62
            throw new InvalidArgumentException(sprintf('Cannot resolve operation path for resource short name "%s".', $resourceShortName));
63
        }
64
65
        if (!$route = $this->router->getRouteCollection()->get($routeName)) {
66
            throw new InvalidArgumentException(sprintf('The route "%s" of the resource "%s" was not found.', $routeName, $resourceShortName));
67
        }
68
69
        return $route->getPath();
70
    }
71
}
72