Skip to content

Commit 729ff8b

Browse files
mock function has been refactored
1 parent 62a9a26 commit 729ff8b

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

src/MockFunction.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use ReflectionObject;
7+
use function call_user_func;
78

89
/**
910
*
@@ -53,22 +54,57 @@ public function __construct()
5354
public function add(string $name, mixed $returnValue): void
5455
{
5556
if (!($returnValue instanceof Closure)) {
56-
$returnValue=static fn() => $returnValue;
57+
$returnValue = static fn() => $returnValue;
5758
}
5859
$this->functions[$name] = $returnValue;
5960
}
6061

62+
/**
63+
* @noinspection PhpUnused
64+
* @param string $name
65+
* @param mixed $returnValue
66+
* @return void
67+
*/
68+
public function addIfNotDefined(string $name, mixed $returnValue): void
69+
{
70+
if (!$this->has($name)) {
71+
$this->add($name, $returnValue);
72+
}
73+
}
74+
75+
/**
76+
* @param string $name
77+
* @return bool
78+
*/
79+
public function has(string $name): bool
80+
{
81+
return isset($this->functions[$name]);
82+
}
83+
6184
/**
6285
* @param string $name
6386
* @param array $args
6487
* @return mixed
6588
*/
6689
private function call(string $name, array $args): mixed
6790
{
68-
$func = $this->functions[$name];
69-
$returnValue = $func(...$args);
70-
$this->incrementCount($name);
71-
return $returnValue;
91+
return $this->executeWithEffect(
92+
call_user_func($this->functions[$name], ...$args),
93+
fn() => $this->incrementCount($name)
94+
);
95+
}
96+
97+
/**
98+
* @param string $name
99+
* @param mixed ...$args
100+
* @return mixed
101+
*/
102+
private function callGlobalFunction(string $name, mixed ...$args): mixed
103+
{
104+
return $this->executeWithEffect(
105+
$name(...$args),
106+
fn() => $this->incrementCount($name)
107+
);
72108
}
73109

74110
/**
@@ -112,10 +148,14 @@ private function generate(string $namespace): string
112148
return $code;
113149
}
114150

115-
private function callGlobalFunction(string $name, mixed ...$args): mixed
151+
/**
152+
* @param mixed $returnValue
153+
* @param Closure $effect
154+
* @return mixed
155+
*/
156+
private function executeWithEffect(mixed $returnValue, Closure $effect): mixed
116157
{
117-
$returnValue = $name(...$args);
118-
$this->incrementCountOutOfScope($name);
158+
$effect();
119159
return $returnValue;
120160
}
121161

@@ -139,8 +179,7 @@ public function scope(?string $namespace = null): void
139179
*/
140180
private function getNamespaceFromTrace(array $trace): string
141181
{
142-
$file = $trace[0]['file'];
143-
if (preg_match('/\bnamespace\s+([^;]+);/', file_get_contents($file), $matches)) {
182+
if (preg_match('/\bnamespace\s+([^;]+);/', file_get_contents($trace[0]['file']), $matches)) {
144183
return trim($matches[1]);
145184
}
146185
throw new NamespaceNotFound('the namespace keyword could not find');
@@ -183,6 +222,10 @@ public function getCalledCountOutScope(string $functionName): int
183222
return $this->getCalledCount($functionName)['out_scope'];
184223
}
185224

225+
/**
226+
* @param string $functionName
227+
* @return int
228+
*/
186229
public function getTotalCount(string $functionName): int
187230
{
188231
$calledCount = $this->getCalledCount($functionName);
@@ -222,16 +265,6 @@ public function runWithMock(object $object, Closure $closure): mixed
222265
return $result;
223266
}
224267

225-
/**
226-
* @param string $name
227-
* @return void
228-
*/
229-
private function incrementCountOutOfScope(string $name): void
230-
{
231-
$this->calledCount[$name][$name] ??= 0;
232-
++$this->calledCount[$name]['out_scope'];
233-
}
234-
235268
/**
236269
* @param string $method
237270
* @param array $arguments

0 commit comments

Comments
 (0)