Api::getPhpfastcacheVersion()   C
last analyzed

Complexity

Conditions 12
Paths 18

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 18
nop 2
dl 0
loc 41
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache;
18
19
use Phpfastcache\Exceptions\PhpfastcacheIOException;
20
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
21
use Phpfastcache\Helper\UninstanciableObjectTrait;
22
23
/**
24
 * Class Api
25
 * @package Phpfastcache
26
 */
27
class Api
28
{
29
    use UninstanciableObjectTrait;
30
31
    protected static string $version = '4.3.0';
32
33
    /**
34
     * This method will return the current
35
     * API version, the API version will be
36
     * updated by following the semantic versioning
37
     * based on changes of:
38
     * - ExtendedCacheItemPoolInterface
39
     * - ExtendedCacheItemInterface
40
     * - AggregatablePoolInterface
41
     * - AggregatorInterface
42
     * - ClusterPoolInterface
43
     * - EventManagerInterface
44
     *
45
     * @see https://semver.org/
46
     * @return string
47
     */
48
    public static function getVersion(): string
49
    {
50
        return self::$version;
51
    }
52
53
    /**
54
     * @param bool $fallbackOnChangelog
55
     * @param bool $cacheable
56
     * @return string
57
     * @throws PhpfastcacheLogicException
58
     * @throws PhpfastcacheIOException
59
     */
60
    public static function getPhpfastcacheVersion(bool $fallbackOnChangelog = true, bool $cacheable = true): string
61
    {
62
        /**
63
         * Cache the version statically to improve
64
         * performances on multiple calls
65
         */
66
        static $version;
67
68
        if ($version && $cacheable) {
69
            return $version;
70
        }
71
72
        if (\function_exists('shell_exec')) {
73
            $command = 'git -C "' . __DIR__ . '" describe --abbrev=0 --tags ' . (DIRECTORY_SEPARATOR === '\\' ? '2>nul' : '2>/dev/null');
74
            $stdout = \shell_exec($command);
75
            if (\is_string($stdout)) {
76
                return trim($stdout);
77
            }
78
            if (!$fallbackOnChangelog) {
79
                throw new PhpfastcacheLogicException('The git command used to retrieve the Phpfastcache version has failed.');
80
            }
81
        }
82
83
        if (!$fallbackOnChangelog) {
84
            throw new PhpfastcacheLogicException('shell_exec is disabled therefore the Phpfastcache version cannot be retrieved.');
85
        }
86
87
        $changelogFilename = __DIR__ . '/../../CHANGELOG.md';
88
        if (\file_exists($changelogFilename)) {
89
            $semverRegexp = '/^([\d]+)\.([\d]+)\.([\d]+)(?:-([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?(?:\+[\dA-Za-z-]+)?$/';
90
            $changelog = \explode("\n", self::getPhpfastcacheChangelog());
91
            foreach ($changelog as $line) {
92
                $trimmedLine = \trim($line, " \t\n\r\0\x0B#");
93
                if (\str_starts_with($line, '#') && \preg_match($semverRegexp, $trimmedLine)) {
94
                    return $trimmedLine;
95
                }
96
            }
97
            throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache version through the CHANGELOG.md as no valid string were found in it.');
98
        }
99
        throw new PhpfastcacheLogicException(
100
            'shell_exec being disabled we attempted to retrieve the Phpfastcache version through the CHANGELOG.md file but it is not readable or has been removed.'
101
        );
102
    }
103
104
    /**
105
     * Return the Phpfastcache changelog, as a string.
106
     * @return string
107
     * @throws PhpfastcacheLogicException
108
     * @throws PhpfastcacheIOException
109
     */
110
    public static function getPhpfastcacheChangelog(): string
111
    {
112
        $changelogFilename = __DIR__ . '/../../CHANGELOG.md';
113
        if (\file_exists($changelogFilename)) {
114
            $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename)));
115
            if ($string) {
116
                return $string;
117
            }
118
            throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache changelog as it seems to be empty.');
119
        }
120
        throw new PhpfastcacheIOException('The CHANGELOG.md file is not readable or has been removed.');
121
    }
122
123
    /**
124
     * @param bool $cacheable
125
     * @return string
126
     */
127
    public static function getPhpfastcacheGitHeadHash(bool $cacheable = true): string
128
    {
129
        static $hash;
130
131
        if ($hash && $cacheable) {
132
            return $hash;
133
        }
134
135
        if (\function_exists('shell_exec')) {
136
            $stdout = \shell_exec('git rev-parse --short HEAD ' . (DIRECTORY_SEPARATOR === '\\' ? '2>nul' : '2>/dev/null'));
137
            if (\is_string($stdout)) {
138
                return '#' . \trim($stdout);
139
            }
140
        }
141
        return '';
142
    }
143
144
    /**
145
     * Return the API changelog, as a string.
146
     * @return string
147
     * @throws PhpfastcacheLogicException
148
     * @throws PhpfastcacheIOException
149
     */
150
    public static function getChangelog(): string
151
    {
152
        $changelogFilename = __DIR__ . '/../../CHANGELOG_API.md';
153
        if (\file_exists($changelogFilename)) {
154
            $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename)));
155
            if ($string) {
156
                return $string;
157
            }
158
            throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache API changelog as it seems to be empty.');
159
        }
160
        throw new PhpfastcacheIOException('The CHANGELOG_API.md file is not readable or has been removed.');
161
    }
162
}
163