forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBleedingEdgeToggle.php
More file actions
51 lines (41 loc) · 1.32 KB
/
Copy pathBleedingEdgeToggle.php
File metadata and controls
51 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php declare(strict_types = 1);
namespace PHPStan\DependencyInjection;
use Generator;
use PHPStan\ShouldNotHappenException;
final class BleedingEdgeToggle
{
private static bool $bleedingEdge = false;
public static function isBleedingEdge(): bool
{
return self::$bleedingEdge;
}
public static function setBleedingEdge(bool $bleedingEdge): void
{
self::$bleedingEdge = $bleedingEdge;
}
/**
* Runs the callback with the toggle set to $bleedingEdge and restores the previous
* value before returning, so the global toggle is never observable as mutated outside
* this call. When used from a data provider, the data sets must be produced by the
* callback so that the contained objects are constructed while the toggle is set -
* holding the toggle across a `yield` would otherwise leak it into unrelated tests.
*
* @template T
* @param callable(): T $callback
* @return T
*/
public static function withBleedingEdge(bool $bleedingEdge, callable $callback)
{
$backup = self::$bleedingEdge;
self::$bleedingEdge = $bleedingEdge;
try {
$result = $callback();
if ($result instanceof Generator) {
throw new ShouldNotHappenException('callback is not allowed to yield, to prevent leaking the toggle into unrelated tests.');
}
return $result;
} finally {
self::$bleedingEdge = $backup;
}
}
}