Completed
Pull Request — master (#12)
by
unknown
04:08 queued 02:45
created

FileLoader::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 8
loc 8
ccs 4
cts 6
cp 0.6667
crap 3.3332
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 FileLoader implements LoaderInterface
11
{
12
    /**
13
     * @var DecoderManager
14
     */
15
    private $decoders;
16
17
    /**
18
     * @param JsonDecoderInterface|DecoderManager $decoders
19
     */
20 68 View Code Duplication
    public function __construct($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...
21
    {
22 68
        if ($decoders instanceof DecoderInterface) {
23
            $this->decoders = new DecoderManager([$decoders]);
24
        } else {
25 68
            $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...
26
        }
27 68
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 38
    public function load($path, $defaultExtension = 'json')
33
    {
34 38
        $path      = 'file://'.$path;
35 38
        $extension = isset(pathinfo($path)['extension']) ? pathinfo($path)['extension'] : $defaultExtension;
36
37 38
        if (!file_exists($path)) {
38 4
            throw SchemaLoadingException::notFound($path);
39
        }
40
41 34
        return $this->decoders->getDecoder($extension)->decode(file_get_contents($path));
42
    }
43
}
44