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
Pull Request — master (#17)
by Oliver
01:48
created

BaseNameResolver   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveBaseNameFromAliasMap() 0 5 2
A purgeBaseName() 0 3 1
A __construct() 0 6 2
A learnAlias() 0 8 4
B resolveBaseName() 0 21 9
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 AliasMap
26
     */
27
    private $aliasMap = [];
28
29
    /**
30
     * @param null|AliasMap $aliasMap
31
     */
32
    public function __construct(AliasMap $aliasMap = null)
33
    {
34
        if ($aliasMap === null) {
35
            $aliasMap = new AliasMap();
36
        }
37
        $this->aliasMap = $aliasMap;
38
    }
39
40
    /**
41
     * @param string $path
42
     * @param int|null $flags
43
     * @return null|string
44
     */
45
    public function resolveBaseName(string $path, int $flags = null)
46
    {
47
        $hasPharPrefix = Helper::hasPharPrefix($path);
48
        $flags = $flags ?? static::RESOLVE_REALPATH | static::RESOLVE_ALIAS;
49
50
        if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
51
            $baseNameFromAliasMap = $this->resolveBaseNameFromAliasMap($path);
52
            if ($baseNameFromAliasMap !== null) {
53
                return $baseNameFromAliasMap->getBaseName();
54
            }
55
        }
56
57
        $baseName = Helper::determineBaseFile($path);
58
        if ($baseName !== null && $flags & static::RESOLVE_REALPATH) {
59
            $baseName = realpath($baseName);
60
        }
61
        if ($baseName !== null && $hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
62
            $this->learnAlias($baseName);
63
        }
64
65
        return $baseName;
66
    }
67
68
    /**
69
     * @param string $baseName
70
     * @return bool
71
     * @todo Enhance design, wrapping does not make much sense in this class
72
     */
73
    public function purgeBaseName(string $baseName): bool
74
    {
75
        return $this->aliasMap->purgeByBaseName($baseName);
76
    }
77
78
    /**
79
     * @param string $path
80
     * @return null|AliasReference
81
     */
82
    private function resolveBaseNameFromAliasMap(string $path)
83
    {
84
        $normalizedPath = Helper::normalizePath($path);
85
        $possibleAlias = strstr($normalizedPath, '/', true);
86
        return $this->aliasMap->findLastByAlias($possibleAlias ?: '');
87
    }
88
89
90
    /**
91
     * Learns (possible) Phar alias for given $baseName.
92
     *
93
     * Phar aliases are intended to be used only inside Phar archives, however
94
     * PharStreamWrapper needs this information exposed outside of Phar as well
95
     *
96
     * It is possible that same alias is used for different $baseName values.
97
     * That's why AliasMap behaves like a stack when resolving base-name for a
98
     * given alias. On the other hand it is not possible that one $baseName is
99
     * referring to multiple aliases.
100
     *
101
     * @param string $basePath
102
     *
103
     * @see https://secure.php.net/manual/en/phar.setalias.php
104
     * @see https://secure.php.net/manual/en/phar.mapphar.php
105
     */
106
    private function learnAlias(string $basePath)
107
    {
108
        if ($this->aliasMap->findFirstByBaseName($basePath) !== null) {
109
            return;
110
        }
111
        $alias = (new Reader($basePath))->resolveContainer()->getAlias();
112
        if ($alias !== '' && $alias !== $basePath) {
113
            $this->aliasMap->append($basePath, $alias);
114
        }
115
    }
116
}
117