Package   B
last analyzed

Complexity

Total Complexity 49

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 8.96%

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 2
dl 0
loc 281
ccs 6
cts 67
cp 0.0896
rs 8.48
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getName() 0 4 1
A getTitle() 0 6 2
A hasTitle() 0 4 2
A getDescription() 0 4 2
A getVersion() 0 4 2
A getType() 0 4 2
A getKeywords() 0 12 3
A getHomepage() 0 4 2
A getReleaseDate() 0 13 3
A getLicense() 0 12 3
A getAuthors() 0 10 2
A getSupport() 0 4 2
A getRequire() 0 14 4
A getRequireDev() 0 14 4
A getAutoload() 0 10 2
A getExtra() 0 10 2
A getConfiguration() 0 10 2
A getScripts() 0 10 2
A getUniqueName() 0 4 1
A getPath() 0 6 1
A compare() 0 5 2
A __toString() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Package often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Package, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Package.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Entities
10
 * @since          1.0.0
11
 *
12
 * @date           27.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Entities;
18
19
use Nette;
20
use Nette\Utils;
21
22
use IPub\Packages\Exceptions;
23
24
/**
25
 * Package abstract entity
26
 *
27
 * @package        iPublikuj:Packages!
28
 * @subpackage     Entities
29
 *
30
 * @author         Adam Kadlec <[email protected]>
31
 */
32 1
abstract class Package implements IPackage
33
{
34
	/**
35
	 * Implement nette smart magic
36
	 */
37 1
	use Nette\SmartObject;
38
39
	/**
40
	 * @var mixed
41
	 */
42
	protected $composerData;
43
44
	/**
45
	 * @param mixed[] $composerData
46
	 */
47
	public function __construct(array $composerData)
48
	{
49 1
		$this->composerData = $composerData;
50
51 1
		if (!isset($composerData['name'])) {
52
			throw new Exceptions\UnexpectedValueException('Unknown package, has no name defined (' . json_encode($composerData) . ').');
53
		}
54 1
	}
55
56
	/**
57
	 * {@inheritdoc}
58
	 */
59
	public function getName() : string
60
	{
61 1
		return $this->composerData['name'];
62
	}
63
64
	/**
65
	 * {@inheritdoc}
66
	 */
67
	public function getTitle() : string
68
	{
69
		$extra = $this->getExtra();
70
71
		return $extra->offsetExists('title') ? $extra->offsetGet('title') : $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77
	public function hasTitle() : bool
78
	{
79
		return $this->getTitle() ? TRUE : FALSE;
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85
	public function getDescription() : ?string
86
	{
87
		return isset($this->composerData['description']) ? $this->composerData['description'] : NULL;
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93
	public function getVersion() : string
94
	{
95
		return isset($this->composerData['version']) ? $this->composerData['version'] : '0.0.0';
96
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function getType() : string
102
	{
103
		return isset($this->composerData['type']) ? $this->composerData['type'] : 'undefined';
104
	}
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109
	public function getKeywords() : Utils\ArrayHash
110
	{
111
		$keywords = [];
112
113
		if (isset($this->composerData['keywords'])) {
114
			$keywords = $this->composerData['keywords'];
115
116
			$keywords = is_array($keywords) ? $keywords : array_map('trim', explode(',', $keywords));
117
		}
118
119
		return Utils\ArrayHash::from($keywords);
120
	}
121
122
	/**
123
	 * {@inheritdoc}
124
	 */
125
	public function getHomepage() : ?string
126
	{
127
		return isset($this->composerData['homepage']) ? $this->composerData['homepage'] : NULL;
128
	}
129
130
	/**
131
	 * {@inheritdoc}
132
	 */
133
	public function getReleaseDate() : ?\DateTimeInterface
134
	{
135
		if (isset($this->composerData['time'])) {
136
			try {
137
				return new Utils\DateTime($this->composerData['time'], new \DateTimeZone('UTC'));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Nette\Utils\... \DateTimeZone('UTC')); (Nette\Utils\DateTime) is incompatible with the return type declared by the interface IPub\Packages\Entities\IPackage::getReleaseDate of type DateTimeInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
138
139
			} catch (\Exception $ex) {
140
				// If date could not be converted to object, than is in wrong format and is not added to package
141
			}
142
		}
143
144
		return NULL;
145
	}
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150
	public function getLicense() : Utils\ArrayHash
151
	{
152
		$licenses = [];
153
154
		if (isset($this->composerData['license'])) {
155
			$licenses = $this->composerData['license'];
156
157
			$licenses = is_array($licenses) ? $licenses : array_map('trim', explode(',', $licenses));
158
		}
159
160
		return Utils\ArrayHash::from($licenses);
161
	}
162
163
	/**
164
	 * {@inheritdoc}
165
	 */
166
	public function getAuthors() : Utils\ArrayHash
167
	{
168
		$authors = [];
169
170
		if (isset($this->composerData['authors'])) {
171
			$authors = $this->composerData['authors'];
172
		}
173
174
		return Utils\ArrayHash::from($authors);
175
	}
176
177
178
	/**
179
	 * {@inheritdoc}
180
	 */
181
	public function getSupport() : ?array
182
	{
183
		return isset($this->composerData['support']) ? $this->composerData['support'] : NULL;
184
	}
185
186
	/**
187
	 * {@inheritdoc}
188
	 */
189
	public function getRequire() : array
190
	{
191
		$ret = [];
192
193
		if (isset($this->composerData['require'])) {
194
			foreach ($this->composerData['require'] as $name => $require) {
195
				if (strpos($name, '/') !== FALSE) {
196
					$ret[] = $name;
197
				}
198
			}
199
		}
200
201
		return $ret;
202
	}
203
204
	/**
205
	 * {@inheritdoc}
206
	 */
207
	public function getRequireDev() : array
208
	{
209
		$ret = [];
210
211
		if (isset($this->composerData['require-dev'])) {
212
			foreach ($this->composerData['require-dev'] as $name => $require) {
213
				if (strpos($name, '/') !== FALSE) {
214
					$ret[] = $name;
215
				}
216
			}
217
		}
218
219
		return $ret;
220
	}
221
222
	/**
223
	 * {@inheritdoc}
224
	 */
225
	public function getAutoload() : Utils\ArrayHash
226
	{
227
		$autoload = [];
228
229
		if (isset($this->composerData['autoload'])) {
230
			$autoload = $this->composerData['autoload'];
231
		}
232
233
		return Utils\ArrayHash::from($autoload);
234
	}
235
236
	/**
237
	 * {@inheritdoc}
238
	 */
239
	public function getExtra() : Utils\ArrayHash
240
	{
241
		$data = [];
242
243
		if (isset($this->composerData['extra'])) {
244
			$data = $this->composerData['extra'];
245
		}
246
247
		return Utils\ArrayHash::from($data);
248
	}
249
250
	/**
251
	 * {@inheritdoc}
252
	 */
253
	public function getConfiguration() : Utils\ArrayHash
254
	{
255
		$data = [];
256
257
		if (isset($this->composerData['extra']['ipub']['configuration'])) {
258
			$data = $this->composerData['extra']['ipub']['configuration'];
259
		}
260
261
		return Utils\ArrayHash::from($data);
262
	}
263
264
	/**
265
	 * {@inheritdoc}
266
	 */
267
	public function getScripts() : Utils\ArrayHash
268
	{
269
		$scripts = ['IPub\Packages\Scripts\ConfigurationScript'];
270
271
		if (isset($this->composerData['extra']['ipub']['scripts'])) {
272
			$scripts = array_merge($scripts, $this->composerData['extra']['ipub']['scripts']);
273
		}
274
275
		return Utils\ArrayHash::from($scripts);
276
	}
277
278
	/**
279
	 * {@inheritdoc}
280
	 */
281
	public function getUniqueName() : string
282
	{
283
		return sprintf('%s-%s', $this->getName(), $this->composerData['version']);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
284
	}
285
286
	/**
287
	 * {@inheritdoc}
288
	 */
289
	public function getPath() : string
290
	{
291
		$reflectionClass = new \ReflectionClass($this);
292
293
		return dirname($reflectionClass->getFileName());
294
	}
295
296
	/**
297
	 * {@inheritdoc}
298
	 */
299
	public function compare(IPackage $package, string $operator = '==') : bool
300
	{
301
		return strtolower($this->getName()) === strtolower($package->getName()) &&
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
302
			version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator);
303
	}
304
305
	/**
306
	 * {@inheritdoc}
307
	 */
308
	public function __toString() : string
309
	{
310
		return $this->getUniqueName();
311
	}
312
}
313