Completed
Pull Request — master (#12)
by
unknown
03:59
created

ArrayLoader::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 10
loc 10
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonReference\Loader;
4
5
use League\JsonReference\DecoderManager;
6
use League\JsonReference\DecoderInterface;
7
use League\JsonReference\LoaderInterface;
8
use League\JsonReference\SchemaLoadingException;
9
10
final class ArrayLoader implements LoaderInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $schemas;
16
17
    /**
18
     * @var DecoderManager
19
     */
20
    private $decoders;
21
22
    /**
23
     * @param array $schemas  A map of schemas where path => schema.The schema should be a string or the
24
     *                        object resulting from a json_decode call.
25
     *
26
     * @param JsonDecoderInterface|DecoderManager $decoders
27
     */
28 18 View Code Duplication
    public function __construct(array $schemas, $decoders = null)
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...
29
    {
30 18
        $this->schemas = $schemas;
31
        
32 18
        if ($decoders instanceof DecoderInterface) {
33
            $this->decoders = new DecoderManager([$decoders]);
34
        } else {
35 18
            $this->decoders = $decoders ?: new DecoderManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $decoders ?: new \League...erence\DecoderManager() can also be of type object<League\JsonRefere...r\JsonDecoderInterface>. However, the property $decoders is declared as type object<League\JsonReference\DecoderManager>. 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...
36
        }
37 18
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 12
    public function load($path, $extension = 'json')
43
    {
44 12
        if (!array_key_exists($path, $this->schemas)) {
45 4
            throw SchemaLoadingException::notFound($path);
46
        }
47
48 10
        $schema = $this->schemas[$path];
49
50 10
        if (is_string($schema)) {
51 4
            return $this->schemas[$path] = $this->decoders->getDecoder($extension)->decode($schema);
52 8
        } elseif (is_object($schema)) {
53 6
            return $schema;
54
        } else {
55 2
            throw SchemaLoadingException::create($path);
56
        }
57
    }
58
}
59