Package   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 39
c 2
b 1
f 0
dl 0
loc 156
ccs 0
cts 118
cp 0
rs 6.4799
wmc 54

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabel() 0 3 3
A getRepositoryUrl() 0 3 1
A getLanguage() 0 3 2
A getTitle() 0 3 3
A getPackageManager() 0 3 2
A getKeywords() 0 3 1
A getAuthors() 0 3 2
A getHomepage() 0 3 3
A getHeadline() 0 3 1
A getForum() 0 3 2
A getYear() 0 3 2
A getType() 0 3 2
A isDomain() 0 3 1
A getIssues() 0 3 2
A getLicense() 0 3 3
A getDescription() 0 3 1
A getWiki() 0 3 2
A getFullName() 0 3 2
A getSource() 0 3 2
A defaultNamespace() 0 3 1
A hasRequireAny() 0 3 2
A getVersion() 0 3 1
A getSrc() 0 5 2
A hasRequireDev() 0 5 2
A getYears() 0 10 4
A hasRequire() 0 5 2
A getNamespace() 0 5 3

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.

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
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use hidev\helpers\Helper;
14
15
/**
16
 * Package part of the config.
17
 * @property string name
18
 * @property string year
19
 * @property string source
20
 */
21
class Package extends \hidev\base\Component
22
{
23
    use \hiqdev\yii2\collection\ObjectTrait;
24
25
    public function getType()
26
    {
27
        return $this->getItem('type') ?: 'project';
28
    }
29
30
    public function getLanguage()
31
    {
32
        return $this->getItem('language') ?: 'php';
33
    }
34
35
    public function getYears()
36
    {
37
        $years = $this->getItem('years');
38
        if (!empty($years)) {
39
            return $years;
40
        }
41
        $cur = (int) date('Y');
42
        $old = (int) $this->year;
43
44
        return ($old && $old < $cur ? $this->year . '-' : '') . $cur;
45
    }
46
47
    public function getYear()
48
    {
49
        return $this->getItem('year') ?: $this->take('vcs')->getYear();
50
    }
51
52
    public function getLicense()
53
    {
54
        return $this->getItem('license') ?: $this->take('vendor')->license ?: 'No license';
55
    }
56
57
    public function getIssues()
58
    {
59
        return $this->getItem('issues') ?: ($this->source . '/issues');
60
    }
61
62
    public function getWiki()
63
    {
64
        return $this->getItem('wiki') ?: ($this->source . '/wiki');
65
    }
66
67
    public function getKeywords()
68
    {
69
        return Helper::csplit($this->getItem('keywords'));
70
    }
71
72
    public function getFullName()
73
    {
74
        return $this->getItem('fullName') ?: ($this->take('vendor')->name . '/' . $this->name);
75
    }
76
77
    public function getSource()
78
    {
79
        return $this->getItem('source') ?: ('https://github.com/' . $this->take('github')->full_name);
80
    }
81
82
    public function getVersion()
83
    {
84
        return $this->take('version')->version;
85
    }
86
87
    public function getNamespace()
88
    {
89
        return $this->getItem('namespace')
90
            ?: $this->getPackageManager()->getConfiguration()->namespace
91
            ?: self::defaultNamespace($this->take('vendor')->name, $this->name);
92
    }
93
94
    public static function defaultNamespace($vendor, $package)
95
    {
96
        return preg_replace('/[^a-zA-Z0-9\\\\]+/', '', $vendor . strtr("-$package", '-', '\\'));
97
    }
98
99
    public function getSrc()
100
    {
101
        $src = $this->rawItem('src');
102
103
        return isset($src) ? $src : 'src';
104
    }
105
106
    public function getHomepage()
107
    {
108
        return $this->getItem('homepage') ?: ($this->isDomain() ? 'http://' . $this->name . '/' : $this->getSource());
109
    }
110
111
    public function getForum()
112
    {
113
        return $this->getItem('forum') ?: $this->take('vendor')->forum;
114
    }
115
116
    public function getLabel()
117
    {
118
        return $this->getItem('label') ?: $this->getHeadline() ?: Helper::id2camel($this->name);
119
    }
120
121
    public function isDomain()
122
    {
123
        return preg_match('/^[a-z0-9-]+(\.[a-z0-9]+)+$/', $this->name);
124
    }
125
126
    public function getTitle()
127
    {
128
        return $this->getItem('title') ?: ($this->isDomain() ? $this->name : Helper::titleize($this->name));
129
    }
130
131
    public function getHeadline()
132
    {
133
        return $this->getItem('headline');
134
    }
135
136
    public function getDescription()
137
    {
138
        return $this->getItem('description');
139
    }
140
141
    public function getRepositoryUrl($file)
142
    {
143
        return 'https://github.com/' . $this->getFullName() . '/blob/master/' . $file;
144
    }
145
146
    public function getAuthors()
147
    {
148
        return $this->getItem('authors') ?: $this->take('vendor')->authors;
149
    }
150
151
    /**
152
     * Composer for the moment.
153
     * To be changed to get actual Package Manager.
154
     */
155
    public function getPackageManager()
156
    {
157
        return $this->getApp()->has('composer') ? $this->take('composer') : null;
158
    }
159
160
    public function hasRequireAny($package)
161
    {
162
        return $this->hasRequire($package) || $this->hasRequireDev($package);
163
    }
164
165
    public function hasRequire($package)
166
    {
167
        $pman = $this->getPackageManager();
168
169
        return $pman && array_key_exists($package, $pman->getConfiguration()->getRequire());
170
    }
171
172
    public function hasRequireDev($package)
173
    {
174
        $pman = $this->getPackageManager();
175
176
        return $pman && array_key_exists($package, $pman->getConfiguration()->getRequireDev());
177
    }
178
}
179