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 (#22)
by Oliver
01:51
created

PharInvocationResolver::resolvePossibleAlias()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 1
dl 0
loc 4
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\Manager;
17
use TYPO3\PharStreamWrapper\Phar\Reader;
18
use TYPO3\PharStreamWrapper\Resolvable;
19
20
class PharInvocationResolver implements Resolvable
21
{
22
    const RESOLVE_REALPATH = 1;
23
    const RESOLVE_ALIAS = 2;
24
    const ASSERT_INTERNAL_INVOCATION = 32;
25
26
    /**
27
     * @var string[]
28
     */
29
    private $invocationFunctionNames = [
30
        'include',
31
        'include_once',
32
        'require',
33
        'require_once'
34
    ];
35
36
    /**
37
     * Resolves PharInvocation value object (baseName and optional alias).
38
     *
39
     * Phar aliases are intended to be used only inside Phar archives, however
40
     * PharStreamWrapper needs this information exposed outside of Phar as well
41
     * It is possible that same alias is used for different $baseName values.
42
     * That's why PharInvocationCollection behaves like a stack when resolving
43
     * base-name for a given alias. On the other hand it is not possible that
44
     * one $baseName is referring to multiple aliases.
45
     * @see https://secure.php.net/manual/en/phar.setalias.php
46
     * @see https://secure.php.net/manual/en/phar.mapphar.php
47
     *
48
     * @param string $path
49
     * @param int|null $flags
50
     * @return null|PharInvocation
51
     */
52
    public function resolve(string $path, int $flags = null)
53
    {
54
        $hasPharPrefix = Helper::hasPharPrefix($path);
55
        $flags = $flags ?? static::RESOLVE_REALPATH | static::RESOLVE_ALIAS | static::ASSERT_INTERNAL_INVOCATION;
56
57
        if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
58
            $invocation = $this->findByAlias($path);
59
            if ($invocation !== null && $this->assertInternalInvocation($invocation, $flags)) {
60
                return $invocation;
61
            } elseif ($invocation !== null) {
62
                return null;
63
            }
64
        }
65
66
        $baseName = $this->resolveBaseName($path, $flags);
67
        if ($baseName === null) {
68
            return null;
69
        }
70
71
        if ($flags & static::RESOLVE_REALPATH) {
72
            $baseName = realpath($baseName);
73
        }
74
        if ($flags & static::RESOLVE_ALIAS) {
75
            $alias = (new Reader($baseName))->resolveContainer()->getAlias();
76
        } else {
77
            $alias = '';
78
        }
79
80
        return new PharInvocation($baseName, $alias);
81
    }
82
83
    /**
84
     * @param string $path
85
     * @param int $flags
86
     * @return null|string
87
     */
88
    private function resolveBaseName(string $path, int $flags)
89
    {
90
        $baseName = Helper::determineBaseFile($path);
91
        if ($baseName !== null) {
92
            return $baseName;
93
        }
94
95
        $possibleAlias = $this->resolvePossibleAlias($path);
96
        if (!($flags & static::RESOLVE_ALIAS) || $possibleAlias === null) {
97
            return null;
98
        }
99
100
        $trace = debug_backtrace();
101
        foreach ($trace as $item) {
102
            if (!isset($item['function']) || !isset($item['args'][0])
103
                || !in_array($item['function'], $this->invocationFunctionNames, true)) {
104
                continue;
105
            }
106
            $currentPath = $item['args'][0];
107
            if (Helper::hasPharPrefix($currentPath)) {
108
                continue;
109
            }
110
            $currentBaseName = Helper::determineBaseFile($currentPath);
111
            if ($currentBaseName === null) {
112
                continue;
113
            }
114
            // ensure the possible alias name (how we have been called initially) matches
115
            // the resolved alias name that was retrieved by the current possible base name
116
            $currentAlias = (new Reader($currentBaseName))->resolveContainer()->getAlias();
117
            if ($currentAlias !== $possibleAlias) {
118
                continue;
119
            }
120
            return $currentBaseName;
121
        }
122
123
        return null;
124
    }
125
126
    /**
127
     * @param string $path
128
     * @return null|string
129
     */
130
    private function resolvePossibleAlias(string $path)
131
    {
132
        $normalizedPath = Helper::normalizePath($path);
133
        return strstr($normalizedPath, '/', true) ?: null;
134
    }
135
136
    /**
137
     * @param string $path
138
     * @return null|PharInvocation
139
     */
140
    private function findByAlias(string $path)
141
    {
142
        $possibleAlias = $this->resolvePossibleAlias($path);
143
        if ($possibleAlias === null) {
144
            return null;
145
        }
146
        return Manager::instance()->getCollection()->findByCallback(
147
            function (PharInvocation $candidate) use ($possibleAlias) {
148
                return $candidate->getAlias() === $possibleAlias;
149
            },
150
            true
151
        );
152
    }
153
154
    /**
155
     * @param PharInvocation $invocation
156
     * @param int $flags
157
     * @return bool
158
     * @experimental
159
     */
160
    private function assertInternalInvocation(PharInvocation $invocation, int $flags): bool
161
    {
162
        if (!($flags & static::ASSERT_INTERNAL_INVOCATION)) {
163
            return true;
164
        }
165
166
        $trace = debug_backtrace(0);
167
        $firstIndex = count($trace) - 1;
168
        // initial invocation, most probably a CLI tool
169
        if (($trace[$firstIndex]['file'] ?? null) === $invocation->getBaseName()) {
170
            return true;
171
        }
172
        // otherwise search for include/require invocations
173
        foreach ($trace as $item) {
174
            if (!isset($item['function']) || !isset($item['args'][0])) {
175
                continue;
176
            }
177
            if ($item['args'][0] === $invocation->getBaseName()
178
                && in_array($item['function'], $this->invocationFunctionNames, true)
179
            ) {
180
                return true;
181
            }
182
        }
183
184
        return false;
185
    }
186
}
187