GitHub   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 192
ccs 0
cts 122
cp 0
rs 9.92
wmc 31

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getVendorType() 0 7 2
A setVendorType() 0 3 1
A findToken() 0 3 2
A cloneRepo() 0 3 1
A getToken() 0 7 2
A exists() 0 3 1
A request() 0 5 1
A existsRepo() 0 3 2
A releaseRepo() 0 13 2
A waitPush() 0 5 1
A createRepo() 0 16 3
A setFull_name() 0 3 1
A setName() 0 3 1
A setVendor() 0 3 1
A setDescription() 0 3 1
A getFullName() 0 3 1
A getFull_name() 0 3 1
A getName() 0 7 2
A getVendor() 0 7 2
A getDescription() 0 7 2
A setFullName() 0 3 1
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
use yii\helpers\Json;
15
16
/**
17
 * GitHub component.
18
 */
19
class GitHub extends \hidev\base\Component
20
{
21
    protected $_name;
22
    protected $_vendor;
23
    protected $_description;
24
    protected $_vendorType;
25
26
    /**
27
     * @var string GitHub OAuth access token
28
     */
29
    protected $_token;
30
31
    public function setFull_name($value)
32
    {
33
        list($this->_vendor, $this->_name) = explode('/', $value, 2);
34
    }
35
36
    public function getFull_name()
37
    {
38
        return $this->getVendor() . '/' . $this->getName();
39
    }
40
41
    public function setFullName($value)
42
    {
43
        return $this->setFull_name($value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setFull_name($value) targeting hidev\components\GitHub::setFull_name() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
    }
45
46
    public function getFullName()
47
    {
48
        return $this->getFull_name();
49
    }
50
51
    public function setName($value)
52
    {
53
        $this->_name = $value;
54
    }
55
56
    public function getName()
57
    {
58
        if (!$this->_name) {
59
            $this->_name = $this->take('package')->name;
60
        }
61
62
        return $this->_name;
63
    }
64
65
    public function setVendorType($value)
66
    {
67
        $this->_vendorType = $value;
68
    }
69
70
    public function getVendorType()
71
    {
72
        if (!$this->_vendorType) {
73
            $this->_vendorType = 'org';
74
        }
75
76
        return $this->_vendorType;
77
    }
78
79
    public function setVendor($value)
80
    {
81
        $this->_vendor = $value;
82
    }
83
84
    public function getVendor()
85
    {
86
        if (!$this->_vendor) {
87
            $this->_vendor = $this->take('vendor')->name;
88
        }
89
90
        return $this->_vendor;
91
    }
92
93
    public function setDescription($value)
94
    {
95
        $this->_description = $value;
96
    }
97
98
    public function getDescription()
99
    {
100
        if ($this->_description === null) {
101
            $this->_description = $this->take('package')->getTitle();
102
        }
103
104
        return $this->_description;
105
    }
106
107
    /**
108
     * Create the repo on GitHub.
109
     * @return int exit code
110
     */
111
    public function createRepo(string $repo = null)
0 ignored issues
show
Unused Code introduced by
The parameter $repo is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    public function createRepo(/** @scrutinizer ignore-unused */ string $repo = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $end = $this->getVendorType() === 'org'
114
            ?'/orgs/' . $this->getVendor() . '/repos'
115
            : '/user/repos'
116
        ;
117
        $res = $this->request('POST', $end, [
118
            'name'        => $this->getName(),
119
            'description' => $this->getDescription(),
120
        ]);
121
        if (Helper::isResponseOk($res)) {
122
            echo "\ngit remote add origin [email protected]:{$this->getFullName()}.git\n";
123
            echo "git push -u origin master\n";
124
        }
125
126
        return $res;
127
    }
128
129
    /**
130
     * Clone repo from github.
131
     * TODO this action must be run without `start`.
132
     * @param string $repo full name vendor/package
133
     * @return int exit code
134
     */
135
    public function cloneRepo($repo)
136
    {
137
        return $this->passthru('git', ['clone', '[email protected]:' . $repo]);
138
    }
139
140
    /**
141
     * Checks if repo exists.
142
     * @param string $repo full name vendor/package defaults to this repo name
143
     * @return int exit code
144
     */
145
    public function existsRepo($repo = null)
146
    {
147
        return static::exists($repo ?: $this->getFull_name());
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::exists($r... $this->getFull_name()) returns the type boolean which is incompatible with the documented return type integer.
Loading history...
148
    }
149
150
    /**
151
     * Check if repo exists.
152
     * @param string $repo
153
     * @return bool
154
     */
155
    public static function exists($repo)
156
    {
157
        return !static::exec('git', ['ls-remote', '[email protected]:' . $repo], true);
0 ignored issues
show
Bug Best Practice introduced by
The method hidev\base\Component::exec() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        return !static::/** @scrutinizer ignore-call */ exec('git', ['ls-remote', '[email protected]:' . $repo], true);
Loading history...
Bug introduced by
array('ls-remote', '[email protected]:' . $repo) of type array<integer,string> is incompatible with the type string expected by parameter $args of hidev\base\Component::exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
        return !static::exec('git', /** @scrutinizer ignore-type */ ['ls-remote', '[email protected]:' . $repo], true);
Loading history...
158
    }
159
160
    /**
161
     * Creates github release.
162
     * @param string $release version number
163
     */
164
    public function releaseRepo($release = null)
165
    {
166
        $release = $this->take('version')->getRelease($release);
167
        $notes = $this->take('chkipper')->getReleaseNotes();
168
        $wait = $this->waitPush();
169
        if ($wait) {
170
            return $wait;
171
        }
172
173
        return $this->request('POST', '/repos/' . $this->getFull_name() . '/releases', [
174
            'tag_name'  => $release,
175
            'name'      => $release,
176
            'body'      => $notes,
177
        ]);
178
    }
179
180
    /**
181
     * Waits until push is actually finished.
182
     * TODO Check github if it really has latest local commit.
183
     * @return int 0 - success, 1 - failed
184
     */
185
    public function waitPush()
186
    {
187
        sleep(7);
188
189
        return 0;
190
    }
191
192
    public function request($method, $path, $data)
193
    {
194
        $url = 'https://api.github.com' . $path;
195
196
        return $this->passthru('curl', ['-X', $method, '-H', 'Authorization: token ' . $this->getToken(), '--data', Json::encode($data), $url]);
197
    }
198
199
    public function findToken()
200
    {
201
        return $_SERVER['GITHUB_TOKEN'] ?: Helper::readpassword('GitHub token:');
202
    }
203
204
    public function getToken()
205
    {
206
        if ($this->_token === null) {
207
            $this->_token = $this->findToken();
208
        }
209
210
        return $this->_token;
211
    }
212
}
213