Completed
Push — master ( 2a3256...770069 )
by Andrii
04:50
created

PackageController   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 53
lcom 1
cbo 3
dl 0
loc 156
ccs 0
cts 117
cp 0
rs 7.4757
c 0
b 0
f 0

27 Methods

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

How to fix   Complexity   

Complex Class

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