Completed
Pull Request — master (#904)
by Antoine
03:00
created

ChainSubcollectionDataProvider::getSubcollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 5
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\DataProvider;
13
14
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
15
16
/**
17
 * Tries each configured data provider and returns the result of the first able to handle the resource class.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21
final class ChainSubcollectionDataProvider implements SubcollectionDataProviderInterface
22
{
23
    private $dataProviders;
24
25
    /**
26
     * @param SubcollectionDataProviderInterface[] $dataProviders
27
     */
28
    public function __construct(array $dataProviders)
29
    {
30
        $this->dataProviders = $dataProviders;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 View Code Duplication
    public function getSubcollection(string $resourceClass, $id, string $subcollectionProperty, string $operationName = null, array $context = [])
0 ignored issues
show
Duplication introduced by
This method 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...
37
    {
38
        foreach ($this->dataProviders as $dataProviders) {
39
            try {
40
                return $dataProviders->getSubcollection($resourceClass, $id, $subcollectionProperty, $operationName, $context);
0 ignored issues
show
Documentation introduced by
$subcollectionProperty 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 SubcollectionDataProvide...ace::getSubcollection() 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...
41
            } catch (ResourceClassNotSupportedException $e) {
42
                continue;
43
            }
44
        }
45
    }
46
}
47