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
02:27
created

PharInvocationStack::getInvocations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
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
class PharInvocationStack
16
{
17
    const UNIQUE_INVOCATION = 1;
18
    const UNIQUE_BASE_NAME = 2;
19
    const DUPLICATE_ALIAS_WARNING = 32;
20
21
    /**
22
     * @var PharInvocation[]
23
     */
24
    private $invocations = [];
25
26
    /**
27
     * @param PharInvocation $invocation
28
     * @param null|int $flags
29
     * @return bool
30
     */
31
    public function learn(PharInvocation $invocation, int $flags = null): bool
32
    {
33
        if ($flags === null) {
34
            $flags = static::UNIQUE_INVOCATION | static::DUPLICATE_ALIAS_WARNING;
35
        }
36
        if ($invocation->getBaseName() === ''
37
            || $invocation->getAlias() === ''
38
            || !$this->assertUniqueBaseName($invocation, $flags)
39
            || !$this->assertUniqueInvocation($invocation, $flags)
40
        ) {
41
            return false;
42
        }
43
        if ($flags & static::DUPLICATE_ALIAS_WARNING) {
44
            $this->triggerDuplicateAliasWarning($invocation);
45
        }
46
47
        $this->invocations[] = $invocation;
48
        return true;
49
    }
50
51
    /**
52
     * @param string $baseName
53
     * @param bool $reverse
54
     * @return null|PharInvocation
55
     */
56
    public function findByBaseName(string $baseName, bool $reverse = false)
57
    {
58
        if ($baseName === '') {
59
            return null;
60
        }
61
        foreach ($this->getInvocations($reverse) as $invocation) {
62
            if ($invocation->getBaseName() === $baseName) {
63
                return $invocation;
64
            }
65
        }
66
        return null;
67
    }
68
69
    /**
70
     * @param string $alias
71
     * @param bool $reverse
72
     * @return null|PharInvocation
73
     */
74
    public function findByAlias(string $alias, bool $reverse = false)
75
    {
76
        if ($alias === '') {
77
            return null;
78
        }
79
        foreach ($this->getInvocations($reverse) as $invocation) {
80
            if ($invocation->getAlias() === $alias) {
81
                return $invocation;
82
            }
83
        }
84
        return null;
85
    }
86
87
    /**
88
     * @param callable $callback
89
     * @param bool $reverse
90
     * @return null|PharInvocation
91
     */
92
    public function findByCallback(callable $callback, $reverse = false)
93
    {
94
        foreach ($this->getInvocations($reverse) as $invocation) {
95
            if (call_user_func($callback, $invocation) === true) {
96
                return $invocation;
97
            }
98
        }
99
        return null;
100
    }
101
102
    /**
103
     * Asserts that base-name is unique. This disallows having multiple invocations for
104
     * same base-name but having different alias names.
105
     *
106
     * @param PharInvocation $invocation
107
     * @param int $flags
108
     * @return bool
109
     */
110
    private function assertUniqueBaseName(PharInvocation $invocation, int $flags): bool
111
    {
112
        if (!($flags & static::UNIQUE_BASE_NAME)) {
113
            return true;
114
        }
115
        return $this->findByBaseName($invocation->getBaseName()) === null;
116
    }
117
118
    /**
119
     * Asserts that combination of base-name and alias is unique. This allows having multiple
120
     * invocations for same base-name but having different alias names (for whatever reason).
121
     *
122
     * @param PharInvocation $invocation
123
     * @param int $flags
124
     * @return bool
125
     */
126
    private function assertUniqueInvocation(PharInvocation $invocation, int $flags): bool
127
    {
128
        if (!($flags & static::UNIQUE_INVOCATION)) {
129
            return true;
130
        }
131
        return $this->findByCallback(
132
            function (PharInvocation $candidate) use ($invocation) {
133
                return $candidate->equals($invocation);
134
            }
135
        ) === null;
136
    }
137
138
    /**
139
     * @param PharInvocation $invocation
140
     */
141
    private function triggerDuplicateAliasWarning(PharInvocation $invocation)
142
    {
143
        $sameAliasInvocation = $this->findByAlias($invocation->getAlias(), true);
144
        if ($sameAliasInvocation !== null) {
145
            trigger_error(
146
                sprintf(
147
                    'Alias %s cannot be used by %s, already used by %s',
148
                    $invocation->getAlias(),
149
                    $invocation->getBaseName(),
150
                    $sameAliasInvocation->getBaseName()
151
                ),
152
                E_USER_WARNING
153
            );
154
        }
155
    }
156
157
    /**
158
     * @param bool $reverse
159
     * @return PharInvocation[]
160
     */
161
    private function getInvocations(bool $reverse = false): array
162
    {
163
        if ($reverse) {
164
            return array_reverse($this->invocations);
165
        }
166
        return $this->invocations;
167
    }
168
}
169