GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#42)
by joseph
06:00
created

Helper::getComposerJsonDecoded()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.8333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\PHPQA;
4
5
use Composer\Autoload\ClassLoader;
6
7
class Helper
8
{
9
    /**
10
     * @var string
11
     */
12
    private static $projectRootDirectory;
13
14
    /**
15
     * Get the absolute path to the root of the current project
16
     *
17
     * It does this by working from the Composer autoloader which we know will be in a certain place in `vendor`
18
     *
19
     * @return string
20
     * @throws \Exception
21
     */
22 2
    public static function getProjectRootDirectory(): string
23
    {
24 2
        if (null === self::$projectRootDirectory) {
0 ignored issues
show
introduced by
The condition null === self::projectRootDirectory is always false. If null === self::projectRootDirectory can have other possible types, add them to src/Helper.php:10
Loading history...
25 1
            $reflection                 = new \ReflectionClass(ClassLoader::class);
26 1
            self::$projectRootDirectory = \dirname((string)$reflection->getFileName(), 3);
27
        }
28
29 2
        return self::$projectRootDirectory;
30
    }
31
32
    /**
33
     * @param string|null $path
34
     *
35
     * @return array
36
     * @throws \Exception
37
     * @SuppressWarnings(PHPMD.StaticAccess)
38
     */
39 2
    public static function getComposerJsonDecoded(string $path = null): array
40
    {
41 2
        $path     = $path ?? self::getProjectRootDirectory().'/composer.json';
42 2
        $contents = (string)\file_get_contents($path);
43 2
        if ('' === $contents) {
44
            throw new \RuntimeException('composer.json is empty');
45
        }
46 2
        $decoded = \json_decode($contents, true);
47 2
        if (JSON_ERROR_NONE !== \json_last_error()) {
48 1
            throw new \RuntimeException('Failed loading composer.json: '.\json_last_error_msg());
49
        }
50
51 1
        return $decoded;
52
    }
53
}
54