-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvocableClass.php
82 lines (72 loc) · 2.18 KB
/
InvocableClass.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
74
75
76
77
78
79
80
81
82
<?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/tests/Utils/InvocableClass.php>
*
* @license http://opensource.org/licenses/MIT The MIT License
*
* SPDX-License-Identifier: MIT
* License-Filename: LICENSE
*/
declare(strict_types=1);
namespace PhrozenByte\PHPUnitThrowableAsserts\Tests\Utils;
use Throwable;
/**
* InvocableClass is a simple implementation of a invocable class.
*/
class InvocableClass
{
/** @var string|null */
protected $throwableClassName;
/** @var array */
protected $throwableArguments = [];
/**
* InvocableClass constructor.
*
* @param string|null $throwableClassName the Throwable's class name to thrown when invoked
* @param mixed ...$throwableArguments the arguments to pass to the Throwable
*/
public function __construct(string $throwableClassName = null, ...$throwableArguments)
{
$this->throwableClassName = $throwableClassName;
$this->throwableArguments = $throwableArguments;
}
/**
* Invokes the class and either returns all arguments passed or throws.
*
* @param mixed ...$arguments the values to return
*
* @return array list of the arguments passed
*
* @throws Throwable the expected Throwable
*/
public function __invoke(...$arguments): array
{
if ($this->throwableClassName !== null) {
/** @psalm-var class-string<Throwable> $className */
$className = $this->throwableClassName;
throw new $className(...$this->throwableArguments);
}
return $arguments;
}
/**
* Empty declaration of a method.
*/
public function otherMethod(): void
{
// do nothing
}
/**
* Empty declaration of a static method.
*/
public static function otherStaticMethod(): void
{
// do nothing
}
}