GitObject   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepository() 0 3 1
A __construct() 0 4 1
A __toString() 0 9 4
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
/**
16
 * Git object for handling git objects.
17
 *
18
 * @author Divine Niiquaye Ibok <[email protected]>
19
 */
20
abstract class GitObject implements \Stringable
21
{
22
    protected ?string $hash;
23
    protected Repository $repository;
24
25
    public function __construct(Repository $repository, string $commitHash)
26
    {
27
        $this->repository = $repository;
28
        $this->hash = $commitHash;
29
    }
30
31
    /**
32
     * Returns the SHA1 hash of the commit.
33
     */
34
    public function __toString(): string
35
    {
36
        $hash = $this->hash;
37
38
        if (empty($hash) || !\preg_match('/^[0-9a-f]{40}$/', $hash)) {
39
            throw new \InvalidArgumentException(\sprintf('Invalid commit hash%s', empty($hash) ? '. Empty hash provided' : " \"$hash\""));
40
        }
41
42
        return $hash;
43
    }
44
45
    public function getRepository(): Repository
46
    {
47
        return $this->repository;
48
    }
49
}
50