Issues (16)

src/Json/Loader/ChainLoader.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\Json\Loader;
15
16
use Nijens\OpenapiBundle\Json\Exception\LoaderLoadException;
17
use stdClass;
18
19
/**
20
 * This loader calls several loaders in a chain until one loader is able to load the file.
21
 *
22
 * @author Niels Nijens <[email protected]>
23
 */
24
final class ChainLoader implements LoaderInterface
25
{
26
    /**
27
     * @var LoaderInterface[]
28
     */
29
    private $loaders;
30
31
    /**
32
     * Constructs a new {@see ChainLoader} instance.
33
     *
34
     * @param LoaderInterface[] $loaders
35
     */
36
    public function __construct(iterable $loaders = [])
37
    {
38
        $this->loaders = $loaders;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loaders can also be of type iterable. However, the property $loaders is declared as type Nijens\OpenapiBundle\Json\Loader\LoaderInterface[]. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supports(string $file): bool
45
    {
46
        foreach ($this->loaders as $loader) {
47
            if ($loader->supports($file)) {
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function load(string $file): stdClass
59
    {
60
        foreach ($this->loaders as $loader) {
61
            if ($loader->supports($file)) {
62
                return $loader->load($file);
63
            }
64
        }
65
66
        throw new LoaderLoadException(sprintf('No loader available to load "%s".', $file));
67
    }
68
}
69