Completed
Pull Request — master (#472)
by Théo
03:07
created

GetItemAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 11 1
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
namespace ApiPlatform\Core\Http\Action;
13
14
use ApiPlatform\Core\Api\ItemDataProviderInterface;
15
use ApiPlatform\Core\Exception\RuntimeException;
16
use ApiPlatform\Core\Http\ItemDataProvider;
17
use ApiPlatform\Core\Http\RequestAttributesExtractorInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
/**
22
 * Default API action retrieving a resource (used for GET and DELETE methods).
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class GetItemAction
27
{
28
    /**
29
     * @var ItemDataProvider
30
     */
31
    private $itemDataProvider;
32
33
    /**
34
     * @var RequestAttributesExtractorInterface
35
     */
36
    private $attributesExtractor;
37
38
    public function __construct(ItemDataProviderInterface $itemDataProvider, RequestAttributesExtractorInterface $attributesExtractor)
39
    {
40
        $this->itemDataProvider = new ItemDataProvider($itemDataProvider);
41
        $this->attributesExtractor = $attributesExtractor;
42
    }
43
44
    /**
45
     * Retrieves an item.
46
     *
47
     * @param Request    $request
48
     * @param string|int $id
49
     *
50
     * @throws NotFoundHttpException
51
     * @throws RuntimeException
52
     *
53
     * @return mixed
54
     */
55
    public function __invoke(Request $request, $id)
56
    {
57
        $attributesBag = $this->attributesExtractor->extract($request);
58
59
        return $this->itemDataProvider->getItem(
60
            $this->itemDataProvider,
0 ignored issues
show
Documentation introduced by
$this->itemDataProvider is of type object<ApiPlatform\Core\Http\ItemDataProvider>, 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...
61
            $attributesBag->getResourceClass(),
62
            $attributesBag->getItemOperationName(),
63
            $id
0 ignored issues
show
Documentation introduced by
$id is of type string|integer, but the function expects a boolean.

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...
64
        );
65
    }
66
}
67