1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prophecy\Prediction; |
13
|
|
|
|
14
|
|
|
use Prophecy\Call\Call; |
15
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
16
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
17
|
|
|
use Prophecy\Exception\InvalidArgumentException; |
18
|
|
|
use Closure; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Callback prediction. |
22
|
|
|
* |
23
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
class CallbackPrediction implements PredictionInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
private $callback; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initializes callback prediction. |
31
|
|
|
* |
32
|
|
|
* @param callable $callback Custom callback |
33
|
|
|
* |
34
|
|
|
* @throws \Prophecy\Exception\InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct($callback) |
37
|
|
|
{ |
38
|
|
|
if (!is_callable($callback)) { |
39
|
|
|
throw new InvalidArgumentException(sprintf( |
40
|
|
|
'Callable expected as an argument to CallbackPrediction, but got %s.', |
41
|
|
|
gettype($callback) |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->callback = $callback; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Executes preset callback. |
50
|
|
|
* |
51
|
|
|
* @param Call[] $calls |
52
|
|
|
* @param ObjectProphecy $object |
53
|
|
|
* @param MethodProphecy $method |
54
|
|
|
*/ |
55
|
|
|
public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
56
|
|
|
{ |
57
|
|
|
$callback = $this->callback; |
58
|
|
|
|
59
|
|
|
if ($callback instanceof Closure && method_exists('Closure', 'bind')) { |
60
|
|
|
$callback = Closure::bind($callback, $object); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
call_user_func($callback, $calls, $object, $method); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.