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.
Test Failed
Pull Request — master (#17)
by Oliver
02:05
created

PharInvocationResolver::findByAlias()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
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 PharInvocationResolver implements Resolvable
20
{
21
    const RESOLVE_REALPATH = 1;
22
    const RESOLVE_ALIAS = 2;
23
    const ASSERT_INTERNAL_INVOCATION = 32;
24
25
    /**
26
     * @var PharInvocationStack
27
     */
28
    private $stack;
29
30
    /**
31
     * @var string[]
32
     */
33
    private $invocationFunctionNames = [
34
        'include',
35
        'include_once',
36
        'require',
37
        'require_once'
38
    ];
39
40
    /**
41
     * @param PharInvocationStack $stack
42
     */
43
    public function __construct(PharInvocationStack $stack)
44
    {
45
        $this->stack = $stack;
46
    }
47
48
    /**
49
     * Resolves PharInvocation value object (baseName and optional alias).
50
     *
51
     * Phar aliases are intended to be used only inside Phar archives, however
52
     * PharStreamWrapper needs this information exposed outside of Phar as well
53
     * It is possible that same alias is used for different $baseName values.
54
     * That's why AliasMap behaves like a stack when resolving base-name for a
55
     * given alias. On the other hand it is not possible that one $baseName is
56
     * referring to multiple aliases.
57
     * @see https://secure.php.net/manual/en/phar.setalias.php
58
     * @see https://secure.php.net/manual/en/phar.mapphar.php
59
     *
60
     * @param string $path
61
     * @param int|null $flags
62
     * @return null|PharInvocation
63
     */
64
    public function resolve(string $path, int $flags = null)
65
    {
66
        $hasPharPrefix = Helper::hasPharPrefix($path);
67
        $flags = $flags ?? static::RESOLVE_REALPATH | static::RESOLVE_ALIAS | static::ASSERT_INTERNAL_INVOCATION;
68
69
        if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
70
            $invocation = $this->findByAlias($path);
71
            if ($invocation !== null && $this->assertInternalInvocation($invocation, $flags)) {
72
                return $invocation;
73
            } elseif ($invocation !== null) {
74
                return null;
75
            }
76
        }
77
78
        $baseName = Helper::determineBaseFile($path);
79
        if ($baseName === null) {
80
            return null;
81
        }
82
83
        if ($flags & static::RESOLVE_REALPATH) {
84
            $baseName = realpath($baseName);
85
        }
86
        if ($flags & static::RESOLVE_ALIAS) {
87
            $alias = (new Reader($baseName))->resolveContainer()->getAlias();
88
        } else {
89
            $alias = '';
90
        }
91
92
        return new PharInvocation($baseName, $alias);
93
    }
94
95
    /**
96
     * @param string $path
97
     * @return null|PharInvocation
98
     */
99
    private function findByAlias(string $path)
100
    {
101
        $normalizedPath = Helper::normalizePath($path);
102
        $possibleAlias = strstr($normalizedPath, '/', true);
103
        return $this->stack->findByAlias($possibleAlias ?: '', true);
104
    }
105
106
    /**
107
     * @param PharInvocation $invocation
108
     * @param int $flags
109
     * @return bool
110
     * @experimental
111
     */
112
    private function assertInternalInvocation(PharInvocation $invocation, int $flags): bool
113
    {
114
        if ($flags ^ static::ASSERT_INTERNAL_INVOCATION) {
115
            return true;
116
        }
117
118
        $trace = debug_backtrace(0);
119
        $firstIndex = count($trace) - 1;
120
        // initial invocation, most probably a CLI tool
121
        if (($trace[$firstIndex]['file'] ?? null) === $invocation->getBaseName()) {
122
            return true;
123
        }
124
        // otherwise search for include/require invocations
125
        foreach ($trace as $item) {
126
            if (!isset($item['function']) || !isset($item['args'][0])) {
127
                continue;
128
            }
129
            if ($item['args'][0] === $invocation->getBaseName()
130
                && in_array($item['function'], $this->invocationFunctionNames, true)
131
            ) {
132
                return true;
133
            }
134
        }
135
136
        return false;
137
    }
138
}
139