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.

Versioner::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * Versioner.php
4
 *
5
 * @package         ProjectVersioner
6
 * @subpackage      Versioner
7
 */
8
9
namespace Naneau\ProjectVersioner;
10
11
use Naneau\ProjectVersioner\ReaderInterface as Reader;
12
13
use \RuntimeException;
14
15
/**
16
 * Versioner
17
 *
18
 * Uses a set of readers to fetch versions from directories
19
 *
20
 * @category        Naneau
21
 * @package         ProjectVersioner
22
 * @subpackage      Versioner
23
 */
24
class Versioner
25
{
26
    /**
27
     * The readers
28
     *
29
     * @var Reader[]
30
     **/
31
    private $readers;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param  Reader[] $readers
37
     * @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...
38
     **/
39
    public function __construct(array $readers = array())
40
    {
41
        $this->setReaders($readers);
42
    }
43
44
    /**
45
     * Get the version for a directory
46
     *
47
     * @param  string $directory
48
     * @return string
49
     **/
50
    public function get($directory)
51
    {
52
        foreach ($this->getReaders() as $reader) {
53
            if ($reader->canRead($directory)) {
54
                return $reader->read($directory);
55
            }
56
        }
57
58
        throw new RuntimeException(sprintf(
59
            'Can not read version from directory "%s"',
60
            $directory
61
        ));
62
    }
63
64
    /**
65
     * Get the version for a directory
66
     *
67
     * Combining the output of all writers using the given separator
68
     *
69
     * Version will be considered "found" if at least one versioner returns
70
     * output.
71
     *
72
     * @param  string $directory
73
     * @param  string $separator
74
     * @return string
75
     **/
76
    public function getCombined($directory, $separator = '-')
77
    {
78
        $found = array();
79
        foreach ($this->getReaders() as $reader) {
80
            if ($reader->canRead($directory)) {
81
                $found[] = $reader->read($directory);
82
            }
83
        }
84
85
        if (count($found) === 0) {
86
            throw new RuntimeException(sprintf(
87
                'Can not read version from directory "%s"',
88
                $directory
89
            ));
90
        }
91
92
        return implode($separator, $found);
93
    }
94
95
    /**
96
     * Does a directory have a version?
97
     *
98
     * @param string $directory
99
     * @return bool
100
     **/
101
    public function has($directory)
102
    {
103
        foreach ($this->getReaders() as $reader) {
104
            if ($reader->canRead($directory)) {
105
                return true;
106
            }
107
        }
108
109
        return false;
110
    }
111
112
    /**
113
     * Get the set of readers
114
     *
115
     * @return Reader[]
116
     */
117
    public function getReaders()
118
    {
119
        return $this->readers;
120
    }
121
122
    /**
123
     * Set the set of readers
124
     *
125
     * @param  Reader[]  $readers
126
     * @return Versioner
127
     */
128
    public function setReaders(array $readers)
129
    {
130
        $this->readers = $readers;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Add a reader
137
     *
138
     * @param  Reader[]  $reader
139
     * @return Versioner
140
     */
141
    public function addReader(Reader $reader)
142
    {
143
        $this->readers[] = $reader;
144
145
        return $this;
146
    }
147
}
148