Test Failed
Push — router-extensions ( abc42a...4c1179 )
by Alex
02:50
created

ResourceRegistrar::getResourceWildcard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Huntie\JsonApi\Routing;
4
5
class ResourceRegistrar extends \Illuminate\Routing\ResourceRegistrar
6
{
7
    /**
8
     * The default actions for a resourceful controller.
9
     *
10
     * @var array
11
     */
12
    protected $resourceDefaults = ['index', 'store', 'show', 'update', 'destroy'];
13
14
    /**
15
     * The default actions for each named resource relationship.
16
     *
17
     * @var array
18
     */
19
    protected $relationshipDefaults = ['show'];
20
21
    /**
22
     * Create a new resource registrar instance.
23
     *
24
     * @param \Huntie\JsonApi\Routing\Router $router
25
     */
26
    public function __construct(Router $router)
27
    {
28
        $this->router = $router;
29
    }
30
31
    /**
32
     * Route a resource to a controller.
33
     *
34
     * @param string $name
35
     * @param string $controller
36
     * @param array  $options
37
     */
38
    public function register($name, $controller, array $options = [])
39
    {
40
        parent::register($name, $controller, $options);
41
42
        $this->registerRelationships($name, $controller, $options);
43
    }
44
45
    /**
46
     * Route any resource relationships to a controller.
47
     *
48
     * @param string $name
49
     * @param string $controller
50
     * @param array  $options
51
     */
52
    protected function registerRelationships(string $name, string $controller, array $options)
53
    {
54
        // Map non-associative members as keys, with default relationship actions
55
        $relationships = collect(array_get($options, 'relationships', []))
56
            ->mapWithKeys(function ($methods, $relationship) {
57
                return is_numeric($relationship)
58
                    ? [$methods => $this->relationshipDefaults]
59
                    : [$relationship => (array) $methods];
60
            });
61
62
        foreach (['show', 'update'] as $action) {
63
            $matched = (array) $relationships->filter(function ($methods) use ($action) {
64
                return in_array($action, $methods);
65
            });
66
67
            $this->{'addRelationship' . ucfirst($action)}($name, $base, $matched, $controller, $options);
0 ignored issues
show
Bug introduced by
The variable $base does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
68
        }
69
    }
70
71
    /**
72
     * Add a relationship show method to match named relationships on the resource.
73
     *
74
     * @param string $name
75
     * @param string $base
76
     * @param array  $relationships
77
     * @param string $controller
78
     * @param array  $options
79
     *
80
     * @return \Illuminate\Routing\Route
81
     */
82
    protected function addRelationshipShow(string $name, string $base, array $relationships, string $controller, array $options)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
83
    {
84
        $uri = $this->getRelationshipUri($name, $base);
85
        $action = $this->getResourceAction($name, $controller, 'showRelationship', $options);
86
87
        return $this->router->get($uri, $action)
88
            ->where('relationship', '(' . implode(')|(', $relationships) . ')');
89
    }
90
91
    /**
92
     * Add a relationship update method to match named relationships on the resource.
93
     *
94
     * @param string $name
95
     * @param string $base
96
     * @param array  $relationships
97
     * @param string $controller
98
     * @param array  $options
99
     *
100
     * @return \Illuminate\Routing\Route
101
     */
102
    protected function addRelationshipUpdate(string $name, string $base, array $relationships, string $controller, array $options)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
103
    {
104
        $uri = $this->getRelationshipUri($name, $base);
105
        $action = $this->getResourceAction($name, $relationships, $controller, 'updateRelationship', $options);
0 ignored issues
show
Documentation introduced by
$relationships is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'updateRelationship' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to ResourceRegistrar::getResourceAction() has too many arguments starting with $options.

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...
106
107
        return $this->router->match(['PUT', 'PATCH'], $uri, $action)
108
            ->where('relationship', '(' . implode(')|(', $relationships) . ')');
109
    }
110
111
    /**
112
     * Get the URI for resource relationships.
113
     *
114
     * @param string $name
115
     * @param string $base
116
     */
117
    public function getRelationshipUri(string $name, string $base): string
118
    {
119
        return sprintf('%s/{%s}/relationships/{relationship}', $this->getResourceUri($name), $base);
120
    }
121
122
    /**
123
     * Format a resource parameter for usage.
124
     *
125
     * @param string $value
126
     */
127
    public function getResourceWildcard($value): string
128
    {
129
        return camel_case(parent::getResourceWildcard($value));
130
    }
131
}
132