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.
Passed
Push — master ( bc0b80...a9512b )
by Oliver
05:44
created

BaseNameResolver::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3\PharStreamWrapper\Resolver;
4
5
/*
6
 * This file is part of the TYPO3 project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under the terms
9
 * of the MIT License (MIT). For the full copyright and license information,
10
 * please read the LICENSE file that was distributed with this source code.
11
 *
12
 * The TYPO3 project - inspiring people to share!
13
 */
14
15
use TYPO3\PharStreamWrapper\Helper;
16
use TYPO3\PharStreamWrapper\Phar\Reader;
17
use TYPO3\PharStreamWrapper\Resolvable;
18
19
class BaseNameResolver implements Resolvable
20
{
21
    const RESOLVE_REALPATH = 1;
22
    const RESOLVE_ALIAS = 2;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $aliasMap = [];
28
29
    /**
30
     * @param string $path
31
     * @param int|null $flags
32
     * @return null|string
33
     */
34
    public function resolveBaseName(string $path, int $flags = null)
35
    {
36
        $hasPharPrefix = Helper::hasPharPrefix($path);
37
        $flags = $flags ?? static::RESOLVE_REALPATH | static::RESOLVE_ALIAS;
38
39
        if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
40
            $baseNameFromAliasMap = $this->resolveBaseNameFromAliasMap($path);
41
            if ($baseNameFromAliasMap !== null) {
42
                return $baseNameFromAliasMap;
43
            }
44
        }
45
46
        $baseName = Helper::determineBaseFile($path);
47
        if ($baseName !== null && $flags & static::RESOLVE_REALPATH) {
48
            $baseName = realpath($baseName);
49
        }
50
        if ($baseName !== null && $hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
51
            $this->learnAlias($baseName);
52
        }
53
54
        return $baseName;
55
    }
56
57
    /**
58
     * @param string $path
59
     * @return null|string
60
     */
61
    private function resolveBaseNameFromAliasMap(string $path)
62
    {
63
        $normalizedBaseName = Helper::normalizePath($path);
64
        $possibleAlias = strstr($normalizedBaseName, '/', true);
65
        return $this->getAlias($possibleAlias ?: '');
66
    }
67
68
69
    /**
70
     * @param string $basePath
71
     */
72
    private function learnAlias(string $basePath)
73
    {
74
        $alias = (new Reader($basePath))->resolveContainer()->getAlias();
75
        if ($alias !== '' && $alias !== $basePath) {
76
            $this->setAlias($alias, $basePath);
77
        }
78
    }
79
80
    /**
81
     * @param string $alias
82
     * @return null|string
83
     */
84
    private function getAlias(string $alias)
85
    {
86
        return $this->aliasMap[$alias] ?? null;
87
    }
88
89
    /**
90
     * Updates alias map for current Phar archive.
91
     *
92
     * Since Phar aliases are intended to be used only inside Phar archives,
93
     * there is no further check for duplicates in our alias map.
94
     *
95
     * @param string $alias
96
     * @param string $basePath
97
     * @see https://secure.php.net/manual/en/phar.setalias.php
98
     * @see https://secure.php.net/manual/en/phar.mapphar.php
99
     */
100
    private function setAlias(string $alias, string $basePath)
101
    {
102
        $this->aliasMap[$alias] = $basePath;
103
    }
104
}
105