Completed
Push — develop ( dd6a23...99c8cd )
by Baptiste
06:26
created

RouteFactory::makeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Routing;
5
6
use Innmind\Rest\Server\Action;
7
use Innmind\Http\Message\MethodInterface;
8
use Innmind\Immutable\StringPrimitive as Str;
9
use Symfony\Component\Routing\Route;
10
11
final class RouteFactory
12
{
13
    /**
14
     * Make a route name
15
     *
16
     * @param string $path Path to resource definition
17
     * @param Action $action
18
     *
19
     * @return string
20
     */
21 3
    public function makeName(string $path, Action $action): string
22
    {
23 3
        return (string) (new Str($path))
24 3
            ->prepend('innmind_rest_server.')
25 3
            ->append('.')
26 3
            ->append((string) $action);
27
    }
28
29
    /**
30
     * Make a route
31
     *
32
     * @param string $path Path to resource definition
33
     * @param Action $action
34
     *
35
     * @return Route
36
     */
37 11
    public function makeRoute(string $path, Action $action): Route
38
    {
39 11
        $name = $path;
40 11
        $path = (new Str($path))->replace('.', '/')->append('/');
41 11
        $condition = '';
42
43 11
        switch ((string) $action) {
44 11
            case Action::GET:
45 3
                $path = $path->append('{identity}');
46 10
            case Action::LIST:
47 4
                $method = MethodInterface::GET;
48 4
                break;
49 9
            case Action::CREATE:
50 3
                $method = MethodInterface::POST;
51 3
                break;
52 8
            case Action::UPDATE:
53 3
                $method = MethodInterface::PUT;
54 3
                $path = $path->append('{identity}');
55 3
                break;
56 7
            case Action::REMOVE:
57 3
                $method = MethodInterface::DELETE;
58 3
                $path = $path->append('{identity}');
59 3
                break;
60 6 View Code Duplication
            case Action::LINK:
1 ignored issue
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...
61 3
                $method = MethodInterface::LINK;
62 3
                $path = $path->append('{identity}');
63 3
                $condition = 'request.headers.has(\'Link\')';
64 3
                break;
65 5 View Code Duplication
            case Action::UNLINK:
1 ignored issue
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...
66 3
                $method = MethodInterface::UNLINK;
67 3
                $path = $path->append('{identity}');
68 3
                $condition = 'request.headers.has(\'Link\')';
69 3
                break;
70 4
            case Action::OPTIONS:
71 4
                $method = MethodInterface::OPTIONS;
72 4
                break;
73
        }
74
75 11
        return new Route(
76 11
            (string) $path,
77
            [
78 11
                '_innmind_resource' => $name,
79 11
                '_innmind_action' => (string) $action,
80
            ],
81 11
            [],
82 11
            [],
83 11
            '',
84 11
            [],
85
            $method,
1 ignored issue
show
Bug introduced by
The variable $method does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
            $condition
87
        );
88
    }
89
}
90