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.

ComposerPackage::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * ComposerPackage.php
4
 *
5
 * @package         ProjectVersioner
6
 * @subpackage      Reader
7
 */
8
9
namespace Naneau\ProjectVersioner\Reader;
10
11
use Naneau\ProjectVersioner\ReaderInterface;
12
13
/**
14
 * ComposerPackage
15
 *
16
 * Finds the version of a specific Composer package from the lock file
17
 *
18
 * @category        Naneau
19
 * @package         ProjectVersioner
20
 * @subpackage      Reader
21
 */
22
class ComposerPackage implements ReaderInterface
23
{
24
    /**
25
     * The page name to look for
26
     *
27
     * @var string
28
     **/
29
    private $package;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param  string $package
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     **/
37
    public function __construct($package)
38
    {
39
        $this->setPackage($package);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     **/
45
    public function canRead($directory)
46
    {
47
        if (!is_readable($directory . '/composer.lock')) {
48
            return false;
49
        }
50
51
        $package = $this->getPackageFromLockFile($directory);
52
53
        if ($package === false) {
54
            return false;
55
        }
56
57
        return !empty($package->version);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     **/
63
    public function read($directory)
64
    {
65
        $package = $this->getPackageFromLockFile($directory);
66
67
        return $package->version;
68
    }
69
70
    /**
71
     * Get the package
72
     *
73
     * @return string
74
     */
75
    public function getPackage()
76
    {
77
        return $this->package;
78
    }
79
80
    /**
81
     * Set the package
82
     *
83
     * @param  string          $package
84
     * @return ComposerPackage
85
     */
86
    public function setPackage($package)
87
    {
88
        $this->package = $package;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get the package from the lockfile
95
     *
96
     * @return stdClass|bool returns false when package can not be found
97
     **/
98
    private function getPackageFromLockFile($directory)
99
    {
100
        $parsed = json_decode(file_get_contents($directory . '/composer.lock'));
101
        if (!isset($parsed->packages)) {
102
            return false;
103
        }
104
105
        foreach ($parsed->packages as $package) {
106
            if ($package->name === $this->getPackage()) {
107
                return $package;
108
            }
109
        }
110
111
        return false;
112
    }
113
}
114