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

PharInvocationCollection::assertUniqueInvocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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