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 ( 936a10...ffcee8 )
by Oliver
52s queued 11s
created

PharInvocationResolver::assertInternalInvocation()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 6
nop 2
dl 0
loc 25
rs 8.4444
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 = Helper::determineBaseFile($path);
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
     * @return null|PharInvocation
86
     */
87
    private function findByAlias(string $path)
88
    {
89
        $normalizedPath = Helper::normalizePath($path);
90
        $possibleAlias = strstr($normalizedPath, '/', true);
91
        if (empty($possibleAlias)) {
92
            return null;
93
        }
94
        return Manager::instance()->getCollection()->findByCallback(
95
            function (PharInvocation $candidate) use ($possibleAlias) {
96
                return $candidate->getAlias() === $possibleAlias;
97
            },
98
            true
99
        );
100
    }
101
102
    /**
103
     * @param PharInvocation $invocation
104
     * @param int $flags
105
     * @return bool
106
     * @experimental
107
     */
108
    private function assertInternalInvocation(PharInvocation $invocation, int $flags): bool
109
    {
110
        if (!($flags & static::ASSERT_INTERNAL_INVOCATION)) {
111
            return true;
112
        }
113
114
        $trace = debug_backtrace(0);
115
        $firstIndex = count($trace) - 1;
116
        // initial invocation, most probably a CLI tool
117
        if (($trace[$firstIndex]['file'] ?? null) === $invocation->getBaseName()) {
118
            return true;
119
        }
120
        // otherwise search for include/require invocations
121
        foreach ($trace as $item) {
122
            if (!isset($item['function']) || !isset($item['args'][0])) {
123
                continue;
124
            }
125
            if ($item['args'][0] === $invocation->getBaseName()
126
                && in_array($item['function'], $this->invocationFunctionNames, true)
127
            ) {
128
                return true;
129
            }
130
        }
131
132
        return false;
133
    }
134
}
135