ObjectSpecsCollection::sort()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 1
nop 1
crap 12
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Specs;
17
18
19
use OutOfBoundsException;
20
use OverflowException;
21
22
23
class ObjectSpecsCollection implements ObjectSpecsCollectionInterface
24
{
25
    private $specs = [];
26
27
    public function get(string $class): ObjectSpecInterface
28
    {
29
        // TODO: lookup for class parent specs
30
31
        if (!isset($this->specs[$class])) {
32
            throw new OutOfBoundsException('Spec not found');
33
        }
34
35
        return $this->specs[$class];
36
    }
37
38
    public function put(string $class, ObjectSpecInterface $spec)
39
    {
40
        if (isset($this->specs[$class])) {
41
            throw new OverflowException('Spec with the same name already exists');
42
        }
43
        $this->specs[$class] = $spec;
44
45
        $this->sort($this->specs);
46
47
        // TODO:
48
        //$this->specs = $this->sort($this->specs);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
    }
50
51
    private function sort(array $specs): array
52
    {
53
        uksort($specs, function ($first, $second) {
54
            if (is_subclass_of($first, $second)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $second can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
55
                return -1;
56
            }
57
58
            if (is_subclass_of($second, $first)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $first can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
59
                return 1;
60
            }
61
62
            return 0;
63
        });
64
65
        return $specs;
66
    }
67
}
68
69