GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Package::__isset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * See class comment
4
 *
5
 * PHP Version 5
6
 *
7
 * @category Netresearch
8
 * @package  Netresearch\Kite\Service\Composer
9
 * @author   Christian Opitz <[email protected]>
10
 * @license  http://www.netresearch.de Netresearch Copyright
11
 * @link     http://www.netresearch.de
12
 */
13
14
namespace Netresearch\Kite\Service\Composer;
15
use Netresearch\Kite\Exception;
16
use Netresearch\Kite\Service\Composer;
17
18
/**
19
 * Class Package
20
 *
21
 * // Composer properties, not necessarily present:
22
 *
23
 * @property string      $name      Package name
24
 * @property string      $version   Package version
25
 * @property object      $source    Information about the source
26
 *                                  (may have f.i. "reference")
27
 *
28
 * // Additional properties
29
 *
30
 * @property string      $path      Package path
31
 * @property bool        $isRoot    Whether package is root (project)
32
 * @property array       $requires  The packages from $require as array
33
 * @property bool        $git       Whether installed package is a get repository
34
 * @property array       $branches  Git branches (including remote branches)
35
 * @property array       $upstreams Upstream branches (values) for local branches (keys)
36
 * @property string|null $branch    The currently checked out branch, if any
37
 * @property string|null $tag       The currently checked out tag, if any
38
 * @property string|null $remote    The remote url (no multiple urls supported)
39
 *
40
 *
41
 * @category Netresearch
42
 * @package  Netresearch\Kite\Service\Composer
43
 * @author   Christian Opitz <[email protected]>
44
 * @license  http://www.netresearch.de Netresearch Copyright
45
 * @link     http://www.netresearch.de
46
 */
47
class Package
48
{
49
    /**
50
     * @var Composer
51
     */
52
    protected $composer;
53
54
    /**
55
     * @var bool
56
     */
57
    protected static $forEachRefHeadSupported = true;
58
59
    /**
60
     * Package constructor.
61
     *
62
     * @param Composer      $composer     The parent object
63
     * @param string|object $composerJson The composer json
64
     * @param bool          $isRoot       Whether package is root
65
     */
66
    public function __construct(Composer $composer, $composerJson, $isRoot = false)
67
    {
68
        $this->composer = $composer;
69
70
        if (is_string($composerJson)) {
71
            $path = realpath($composerJson);
72
            if (!$path) {
73
                throw new Exception('Could not find ' . $composerJson);
74
            }
75
            $this->path = dirname($path);
76
            $composerJson = json_decode(file_get_contents($path));
77
            if (!is_object($composerJson)) {
78
                throw new Exception('Could not load ' . $path);
79
            }
80
        }
81
        foreach (get_object_vars($composerJson) as $key => $value) {
82
            $this->$key = $value;
83
        }
84
85
        $this->isRoot = $isRoot;
86
    }
87
88
    /**
89
     * Load lazy properties
90
     *
91
     * @param string $name The property name
92
     *
93
     * @return mixed
94
     */
95
    public function __get($name)
96
    {
97
        switch ($name) {
98
        case 'git':
99
            $gitDir = $this->path . '/.git';
100
            $this->git = file_exists($gitDir) && is_dir($gitDir);
101
            break;
102
        case 'branches':
103
        case 'upstreams':
104
        case 'branch':
105
            $this->loadGitInformation();
106
            break;
107
        case 'tag':
108
            $this->loadTag();
109
            break;
110
        case 'remote':
111
            $this->loadRemote();
112
            break;
113
        case 'requires':
114
            $this->loadRequires();
115
            break;
116
        default:
117
            throw new Exception('Invalid property ' . $name);
118
119
        }
120
        return $this->$name;
121
    }
122
123
    /**
124
     * Mark lazy properties as present
125
     *
126
     * @param string $name The name
127
     *
128
     * @return bool
129
     */
130
    public function __isset($name)
131
    {
132
        return in_array($name, ['branches', 'upstreams', 'branch', 'tag', 'git', 'remote', 'requires'], true);
133
    }
134
135
    /**
136
     * Load the requires - removes inline aliases
137
     *
138
     * @return void
139
     */
140
    protected function loadRequires()
141
    {
142
        $this->requires = isset($this->require) ? get_object_vars($this->require) : array();
0 ignored issues
show
Bug introduced by
The property require does not seem to exist. Did you mean requires?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
143
        foreach ($this->requires as $package => $constraint) {
144
            if ($pos = strpos($constraint, ' as ')) {
145
                if ($hashPos = strpos($constraint, '#')) {
146
                    // dev-master#old-hash isn't treated by composer, so we don't as well
147
                    $pos = $hashPos;
148
                }
149
                $this->requires[$package] = substr($constraint, 0, $pos);
150
            }
151
        }
152
    }
153
154
    /**
155
     * Reload requires from composer.json
156
     *
157
     * @return $this
158
     */
159
    public function reloadRequires()
160
    {
161
        $file = $this->path . '/composer.json';
162
        if (file_exists($file)) {
163
            $composerJson = json_decode(file_get_contents($file));
164
            unset($this->require);
165
            if (isset($composerJson->require)) {
166
                $this->require = $composerJson->require;
0 ignored issues
show
Bug introduced by
The property require does not seem to exist. Did you mean requires?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
167
            }
168
            $this->loadRequires();
169
        }
170
        return $this;
171
    }
172
173
    /**
174
     * Get the remote
175
     *
176
     * @return void
177
     */
178
    protected function loadRemote()
179
    {
180
        $this->remote = null;
181
        if ($this->git) {
182
            $remote = null;
183
            $remotesString = $this->composer->git('remote', $this->path, ['verbose' => true]);
184
            $lines = explode("\n", trim($remotesString));
185
            foreach ($lines as $line) {
186
                preg_match('/^([^\s]+)\s+(.+) \((fetch|push)\)$/', $line, $match);
187
                array_shift($match);
188
                list($name, $url) = $match;
0 ignored issues
show
Unused Code introduced by
The assignment to $name is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
189
                if ($remote && $remote !== $url) {
190
                    $this->composer->output("<warning>Can not handle multiple remote urls - using $remote</warning>");
191
                    break;
192
                } else {
193
                    $remote = $url;
194
                }
195
            }
196
            $this->remote = $remote;
197
        }
198
    }
199
200
    /**
201
     * Load 'branches', 'upstreams', 'branch', 'tag', 'git'
202
     *
203
     * @throws Exception\ProcessFailedException
204
     *
205
     * @return void
206
     */
207
    protected function loadGitInformation()
208
    {
209
        $this->branches = array();
210
        $this->upstreams = array();
211
        $this->branch = null;
212
        if ($this->git) {
213
            $this->composer->git('fetch', $this->path, array('p' => true, 'origin'));
214
            try {
215
                $format = (self::$forEachRefHeadSupported ? '%(HEAD)' : '') . '|%(refname:short)|%(upstream:short)';
216
                $gitBr = $this->composer->git('for-each-ref', $this->path, ['format' => $format, 'refs/heads/', 'refs/remotes/origin']);
217
            } catch (Exception\ProcessFailedException $e) {
218
                if (trim($e->getProcess()->getErrorOutput()) === 'fatal: unknown field name: HEAD') {
219
                    self::$forEachRefHeadSupported = false;
220
                    $this->loadGitInformation();
221
                    return;
222
                } else {
223
                    throw $e;
224
                }
225
            }
226
            if (!self::$forEachRefHeadSupported) {
227
                $this->branch = $this->composer->git('rev-parse', $this->path, ['abbrev-ref' => true, 'HEAD']) ?: null;
228
                if ($this->branch === 'HEAD') {
229
                    $this->branch = null;
230
                }
231
            }
232
            foreach (explode("\n", trim($gitBr)) as $line) {
233
                list($head, $branch, $upstream) = explode('|', $line);
234
                if ($branch === 'origin/HEAD') {
235
                    continue;
236
                }
237
                $this->branches[] = $branch;
238
                if ($head === '*') {
239
                    $this->branch = $branch;
240
                }
241
                if ($upstream) {
242
                    $this->upstreams[$branch] = $upstream;
243
                }
244
            }
245
        }
246
    }
247
248
    /**
249
     * Load the currently checked out tag
250
     *
251
     * @return void
252
     */
253
    protected function loadTag()
254
    {
255
        try {
256
            $this->tag = trim($this->composer->git('describe', $this->path, array('exact-match' => true, 'tags' => true)));
257
        } catch (Exception\ProcessFailedException $e) {
258
            $this->tag =  null;
259
        }
260
    }
261
}
262
263
?>
264