Completed
Push — master ( e32077...7e7359 )
by Tõnis
02:36
created

QuickGit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __toString() 0 3 1
A toString() 0 18 2
1
<?php
2
3
namespace andmemasin\helpers;
4
5
6
class QuickGit
7
{
8
    /** @var int */
9
    private $major = 1;
10
    /** @var int */
11
    private $minor = 0;
12
    /** @var string */
13
    private $patch = '';
14
    /** @var int */
15
    private $commits = 0;
16
    /** @var string */
17
    private $commit = '';
18
19
    /**
20
     * @method __construct
21
     */
22
    public function __construct()
23
    {
24
        // Collect git data.
25
        exec('git describe --always', $gitHashShort);
26
        $this->patch = $gitHashShort;
27
        exec('git rev-list HEAD | wc -l', $gitCommits);
28
        $this->commits = $gitCommits;
29
        exec('git log -1', $gitHashLong);
30
        $this->commit = $gitHashLong;
31
    }
32
    /**
33
     * @return string
34
     */
35
    public function toString($format = 'short')
36
    {
37
        switch ($format) {
38
            case 'long':
39
                return sprintf(
40
                    '%d.%d.%s (#%d, %s)',
41
                    $this->major,
42
                    $this->minor,
43
                    $this->patch,
44
                    $this->commits,
45
                    $this->commit
46
                );
47
            default:
48
                return sprintf(
49
                    '%d.%d.%s',
50
                    $this->major,
51
                    $this->minor,
52
                    $this->patch
53
                );
54
        }
55
    }
56
57
    /**
58
     * @method __toString
59
     */
60
    public function __toString()
61
    {
62
        return $this->toString();
63
    }
64
}