Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework;
11
 
12
use function array_filter;
13
use function array_map;
14
use function array_values;
15
use function count;
16
use function explode;
17
use function in_array;
18
use function strpos;
19
use function trim;
20
 
21
/**
22
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
23
 */
24
final class ExecutionOrderDependency
25
{
26
    /**
27
     * @var string
28
     */
29
    private $className = '';
30
 
31
    /**
32
     * @var string
33
     */
34
    private $methodName = '';
35
 
36
    /**
37
     * @var bool
38
     */
39
    private $useShallowClone = false;
40
 
41
    /**
42
     * @var bool
43
     */
44
    private $useDeepClone = false;
45
 
46
    public static function createFromDependsAnnotation(string $className, string $annotation): self
47
    {
48
        // Split clone option and target
49
        $parts = explode(' ', trim($annotation), 2);
50
 
51
        if (count($parts) === 1) {
52
            $cloneOption = '';
53
            $target      = $parts[0];
54
        } else {
55
            $cloneOption = $parts[0];
56
            $target      = $parts[1];
57
        }
58
 
59
        // Prefix provided class for targets assumed to be in scope
60
        if ($target !== '' && strpos($target, '::') === false) {
61
            $target = $className . '::' . $target;
62
        }
63
 
64
        return new self($target, null, $cloneOption);
65
    }
66
 
67
    /**
68
     * @psalm-param list<ExecutionOrderDependency> $dependencies
69
     *
70
     * @psalm-return list<ExecutionOrderDependency>
71
     */
72
    public static function filterInvalid(array $dependencies): array
73
    {
74
        return array_values(
75
            array_filter(
76
                $dependencies,
77
                static function (self $d)
78
                {
79
                    return $d->isValid();
80
                }
81
            )
82
        );
83
    }
84
 
85
    /**
86
     * @psalm-param list<ExecutionOrderDependency> $existing
87
     * @psalm-param list<ExecutionOrderDependency> $additional
88
     *
89
     * @psalm-return list<ExecutionOrderDependency>
90
     */
91
    public static function mergeUnique(array $existing, array $additional): array
92
    {
93
        $existingTargets = array_map(
94
            static function ($dependency)
95
            {
96
                return $dependency->getTarget();
97
            },
98
            $existing
99
        );
100
 
101
        foreach ($additional as $dependency) {
102
            if (in_array($dependency->getTarget(), $existingTargets, true)) {
103
                continue;
104
            }
105
 
106
            $existingTargets[] = $dependency->getTarget();
107
            $existing[]        = $dependency;
108
        }
109
 
110
        return $existing;
111
    }
112
 
113
    /**
114
     * @psalm-param list<ExecutionOrderDependency> $left
115
     * @psalm-param list<ExecutionOrderDependency> $right
116
     *
117
     * @psalm-return list<ExecutionOrderDependency>
118
     */
119
    public static function diff(array $left, array $right): array
120
    {
121
        if ($right === []) {
122
            return $left;
123
        }
124
 
125
        if ($left === []) {
126
            return [];
127
        }
128
 
129
        $diff         = [];
130
        $rightTargets = array_map(
131
            static function ($dependency)
132
            {
133
                return $dependency->getTarget();
134
            },
135
            $right
136
        );
137
 
138
        foreach ($left as $dependency) {
139
            if (in_array($dependency->getTarget(), $rightTargets, true)) {
140
                continue;
141
            }
142
 
143
            $diff[] = $dependency;
144
        }
145
 
146
        return $diff;
147
    }
148
 
149
    public function __construct(string $classOrCallableName, ?string $methodName = null, ?string $option = null)
150
    {
151
        if ($classOrCallableName === '') {
152
            return;
153
        }
154
 
155
        if (strpos($classOrCallableName, '::') !== false) {
156
            [$this->className, $this->methodName] = explode('::', $classOrCallableName);
157
        } else {
158
            $this->className  = $classOrCallableName;
159
            $this->methodName = !empty($methodName) ? $methodName : 'class';
160
        }
161
 
162
        if ($option === 'clone') {
163
            $this->useDeepClone = true;
164
        } elseif ($option === 'shallowClone') {
165
            $this->useShallowClone = true;
166
        }
167
    }
168
 
169
    public function __toString(): string
170
    {
171
        return $this->getTarget();
172
    }
173
 
174
    public function isValid(): bool
175
    {
176
        // Invalid dependencies can be declared and are skipped by the runner
177
        return $this->className !== '' && $this->methodName !== '';
178
    }
179
 
180
    public function useShallowClone(): bool
181
    {
182
        return $this->useShallowClone;
183
    }
184
 
185
    public function useDeepClone(): bool
186
    {
187
        return $this->useDeepClone;
188
    }
189
 
190
    public function targetIsClass(): bool
191
    {
192
        return $this->methodName === 'class';
193
    }
194
 
195
    public function getTarget(): string
196
    {
197
        return $this->isValid()
198
            ? $this->className . '::' . $this->methodName
199
            : '';
200
    }
201
 
202
    public function getTargetClassName(): string
203
    {
204
        return $this->className;
205
    }
206
}