-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCachedCallableProxy.php
73 lines (64 loc) · 1.81 KB
/
CachedCallableProxy.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* PHPUnitThrowableAssertions - Throwable-related PHPUnit assertions.
*
* @copyright Copyright (c) 2021, Daniel Rudolf (<https://www.daniel-rudolf.de>)
*
* This file is copyrighted by the contributors recorded in the version control
* history of the file, available from the following original location:
*
* <https://github.com/PhrozenByte/phpunit-throwable-asserts/blob/master/src/CachedCallableProxy.php>
*
* @license http://opensource.org/licenses/MIT The MIT License
*
* SPDX-License-Identifier: MIT
* License-Filename: LICENSE
*/
declare(strict_types=1);
namespace PhrozenByte\PHPUnitThrowableAsserts;
use Throwable;
/**
* A simple proxy class for Callables with return value and Throwable caching.
*
* @see CallableProxy
*/
class CachedCallableProxy extends CallableProxy
{
/** @var mixed|null */
protected $returnValue;
/** @var Throwable|null */
protected $throwable;
/**
* {@inheritDoc}
*/
public function __invoke()
{
$this->returnValue = null;
$this->throwable = null;
try {
$this->returnValue = parent::__invoke();
return $this->returnValue;
} catch (Throwable $throwable) {
$this->throwable = $throwable;
throw $this->throwable;
}
}
/**
* Returns the cached return value of the Callable from its last invocation.
*
* @return mixed|null the cached return value of the Callable, or NULL
*/
public function getReturnValue()
{
return $this->returnValue;
}
/**
* Returns the Throwable that was thrown at the Callable's last invocation.
*
* @return Throwable|null the cached Throwable the Callable threw, or NULL
*/
public function getThrowable(): ?Throwable
{
return $this->throwable;
}
}