LibraryInformation::getInfo()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 6
nc 8
nop 2
crap 4
1
<?php
2
/**
3
 * LibraryInformation.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained on one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
18
namespace Pvra\InformationProvider;
19
20
/**
21
 * Class LibraryInformation
22
 *
23
 * @package Pvra\InformationProvider
24
 */
25
class LibraryInformation implements LibraryInformationInterface
26
{
27
    /**
28
     * @var array
29
     */
30
    private $additions = [];
31
    /**
32
     * @var array
33
     */
34
    private $deprecations = [];
35
    /**
36
     * @var array
37
     */
38
    private $removals = [];
39
40
    /**
41
     * Create a new instance and load default information
42
     *
43
     * This methods loads the changes.php file distributed with the library or phar.     *
44
     *
45
     * @return static
46
     */
47 40
    public static function createWithDefaults()
48
    {
49 40
        $source = __DIR__ . '/../../data/library/php/changes.php';
50
51 40
        return static::createFromFile($source);
52
    }
53
54
    /**
55
     * Create a new instance based on a given file path
56
     *
57
     * The filepath given to this method has to represent a php file returning
58
     * an array with a valid structure.
59
     *
60
     * @param string $source Valid path to data source
61
     * @return static
62
     */
63 44
    public static function createFromFile($source)
64
    {
65 44 View Code Duplication
        if (!is_file($source) || !is_readable($source)) {
66 2
            throw new \InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable',
67 1
                $source));
68
        }
69
70 42
        return new static(include $source);
71
    }
72
73
74
    /**
75
     * Create a new instance using the supplied data
76
     *
77
     * @param array $data Array representation of data to represent
78
     */
79 82
    public function __construct(array $data = [])
80
    {
81 82
        if (isset($data['additions'])) {
82 78
            $this->additions = $data['additions'];
83 39
        }
84 82
        if (isset($data['deprecations'])) {
85 68
            $this->deprecations = $data['deprecations'];
86 34
        }
87 82
        if (isset($data['removals'])) {
88 68
            $this->removals = $data['removals'];
89 34
        }
90
91 82
        $baseArray = ['class' => [], 'function' => [], 'constant' => []];
92 82
        $this->additions += $baseArray;
93 82
        $this->deprecations += $baseArray;
94 82
        $this->removals += $baseArray;
95 82
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 6
    public function toArray()
101
    {
102
        return [
103 6
            'additions' => $this->additions,
104 6
            'deprecations' => $this->deprecations,
105 6
            'removals' => $this->removals,
106 3
        ];
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 2
    public function mergeWith(LibraryInformationInterface $info)
113
    {
114 2
        $newInfo = $info->toArray();
115 2
        $this->additions = array_replace_recursive($this->additions, $newInfo['additions']);
116 2
        $this->deprecations = array_replace_recursive($this->deprecations, $newInfo['deprecations']);
117 2
        $this->removals = array_replace_recursive($this->removals, $newInfo['removals']);
118 2
        return $this;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 40
    public function getFunctionInfo($name)
125
    {
126 40
        return $this->getInfo($name, 'function');
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132 36
    public function getClassInfo($name)
133
    {
134 36
        return $this->getInfo($name, 'class');
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140 26
    public function getConstantInfo($name)
141
    {
142 26
        return $this->getInfo($name, 'constant');
143
    }
144
145
    /**
146
     * @param string $name Name of the item
147
     * @param string $type Type of the item (function, class or constant
148
     * @return array An array in the format:
149
     * <code>
150
     * ['addition' => Version|null, 'deprecation' => Version|null, 'removal' => Version|null]
151
     * </code>
152
     */
153 58
    private function getInfo($name, $type)
154
    {
155 58
        $name = ltrim($name, '\\');
156
        return [
157 58
            'addition' => isset($this->additions[ $type ][ $name ]) ? $this->additions[ $type ][ $name ] : null,
158 58
            'deprecation' => isset($this->deprecations[ $type ][ $name ]) ? $this->deprecations[ $type ][ $name ] : null,
159 58
            'removal' => isset($this->removals[ $type ][ $name ]) ? $this->removals[ $type ][ $name ] : null,
160 29
        ];
161
    }
162
}
163