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 (#17)
by Oliver
09:06
created

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