Completed
Push — master ( 4a6135...54df2f )
by Andrii
03:34
created

PackageController   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 25%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 51
c 6
b 3
f 0
lcom 1
cbo 3
dl 0
loc 150
ccs 3
cts 12
cp 0.25
rs 8.3206

26 Methods

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