Lines 46.15% 12 / 26
Functions and Methods 0.00% 0 / 3
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Git 46.15% 12 / 26 0.00% 0 / 3 29.89 0.00% 0 / 1
 readCurrentHead 36.36% 4 / 11 0.00% 0 / 1 8.12
 readCurrentHash 40.00% 4 / 10 0.00% 0 / 1 10.40
 readCurrentVersion 80.00% 4 / 5 0.00% 0 / 1 2.03
1<?php
2
3/**
4 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
5 **/
6
7namespace BO\Slim;
8
9/**
10  * Helper for GIT integration
11  *
12  */
13class Git
14{
15    public static function readCurrentHead(): string|null
16    {
17        $headString = "no version control";
18        $githead = \App::APP_PATH . '/.git/HEAD';
19        if (is_readable($githead)) {
20            $handle = fopen($githead, 'r');
21            if ($handle !== false) {
22                $line = fgets($handle);
23                fclose($handle);
24                if ($line !== false) {
25                    $headString = trim($line);
26                    $headString = preg_replace('#^.* ([^\s]+)$#', '$1', $headString) ?? $headString;
27                }
28            }
29        }
30        return $headString;
31    }
32
33    public static function readCurrentHash(): string|null
34    {
35        $headString = static::readCurrentHead();
36        $githashFile = \App::APP_PATH . '/.git/' . ($headString ?? '');
37        if ($headString !== null && is_readable($githashFile)) {
38            $handle = fopen($githashFile, 'r');
39            if ($handle !== false) {
40                $line = fgets($handle);
41                fclose($handle);
42                if ($line !== false) {
43                    return trim($line);
44                }
45            }
46        }
47
48        return $headString;
49    }
50
51    /**
52     * @return null|string|string[]
53     *
54     */
55    public static function readCurrentVersion(): array|string|null
56    {
57        $headString = static::readCurrentHead();
58        if ($headString === null) {
59            return null;
60        }
61        $headString = preg_replace('#refs/heads/#', '', $headString) ?? $headString;
62        return $headString;
63    }
64}