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); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function sort(array $specs): array |
52
|
|
|
{ |
53
|
|
|
uksort($specs, function ($first, $second) { |
54
|
|
|
if (is_subclass_of($first, $second)) { |
|
|
|
|
55
|
|
|
return -1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (is_subclass_of($second, $first)) { |
|
|
|
|
59
|
|
|
return 1; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 0; |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
return $specs; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
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.