Revision   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 54
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCommit() 0 3 1
A doName() 0 3 1
A getName() 0 3 2
A getRevision() 0 7 2
A __toString() 0 10 3
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of Biurad opensource projects.
5
 *
6
 * @copyright 2022 Biurad Group (https://biurad.com/)
7
 * @license   https://opensource.org/licenses/BSD-3-Clause License
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Biurad\Git;
14
15
use Symfony\Component\Process\Exception\ExceptionInterface;
16
17
/**
18
 * An object representing a git refname.
19
 *
20
 * @author Divine Niiquaye Ibok <[email protected]>
21
 */
22
class Revision extends GitObject
23
{
24
    protected string $name, $revision;
25
26
    /**
27
     * @param string $revision The revision name eg. (refs/heads/master)
28
     */
29
    public function __construct(Repository $repository, string $revision, string $commitHash = null)
30
    {
31
        parent::__construct($repository, $commitHash ?? '');
32
        $this->revision = $revision;
33
        $this->name = $this->doName();
34
    }
35
36
    public function __toString(): string
37
    {
38
        if (null === $this->hash) {
39
            try {
40
                $this->hash = $this->repository->run('rev-parse', ['--verify', $this->revision]);
41
            } catch (ExceptionInterface) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
            }
43
        }
44
45
        return parent::__toString();
46
    }
47
48
    /**
49
     * The name of the revision (e.g. "refs/heads/master").
50
     */
51
    public function getRevision(): string
52
    {
53
        if (!\str_starts_with($this->revision, 'refs/')) {
54
            throw new \RuntimeException(\sprintf('Invalid revision provided "%s", expected revision to start with a "refs/"', $this->revision));
55
        }
56
57
        return $this->revision;
58
    }
59
60
    /**
61
     * The filtered name of the revision (e.g. "master").
62
     */
63
    public function getName(): string
64
    {
65
        return $this->name ?: throw new \RuntimeException('Not implemented yet.');
66
    }
67
68
    public function getCommit(): ?Commit
69
    {
70
        return $this->repository->getCommit($this->__toString());
71
    }
72
73
    protected function doName(): string
74
    {
75
        return ''; // Should be extendedly implemented
76
    }
77
}
78