Skip to content

Commit 76d044b

Browse files
committed
Update codebase to PHP 7.4
1 parent 124fa59 commit 76d044b

28 files changed

+381
-377
lines changed

RoboFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
require_once 'vendor/autoload.php';
34

45
class Robofile extends \Robo\Tasks

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "codeception/aspect-mock",
33
"description": "Experimental Mocking Framework powered by Aspects",
4+
"license": "MIT",
45
"authors": [
56
{
67
"name": "Michael Bodnarchuk",
@@ -21,7 +22,7 @@
2122
"require-dev": {
2223
"codeception/codeception": "^4.1",
2324
"codeception/verify": "^2.1",
24-
"codeception/specify": "^1.4"
25-
},
26-
"license": "MIT"
25+
"codeception/specify": "^1.4",
26+
"consolidation/robo": "^3.0"
27+
}
2728
}

src/AspectMock/Core/Mocker.php

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace AspectMock\Core;
46

57
use AspectMock\Intercept\FunctionInjector;
6-
use Go\Aop\Aspect;
78
use AspectMock\Intercept\MethodInvocation;
9+
use Closure;
10+
use Go\Aop\Aspect;
811

912
class Mocker implements Aspect
1013
{
1114

12-
protected $classMap = [];
13-
protected $objectMap = [];
14-
protected $funcMap = [];
15-
protected $methodMap = ['__call', '__callStatic'];
16-
protected $dynamicMethods = ['__call', '__callStatic'];
15+
protected array $classMap = [];
16+
17+
protected array $objectMap = [];
18+
19+
protected array $funcMap = [];
20+
21+
protected array $methodMap = ['__call', '__callStatic'];
22+
23+
protected array $dynamicMethods = ['__call', '__callStatic'];
1724

1825
public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $params, $static)
1926
{
2027
$result = __AM_CONTINUE__;
2128
$invocation = null;
2229

2330
if (in_array($method, $this->methodMap)) {
24-
$invocation = new \AspectMock\Intercept\MethodInvocation();
31+
$invocation = new MethodInvocation();
2532
$invocation->setThis($class);
2633
$invocation->setMethod($method);
2734
$invocation->setArguments($params);
@@ -39,40 +46,42 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
3946
if (isset($this->objectMap[spl_object_hash($class)])) {
4047
Registry::registerInstanceCall($class, $method, $params);
4148
}
49+
4250
$class = get_class($class);
4351
}
4452

4553
if (isset($this->classMap[$class])) {
4654
Registry::registerClassCall($class, $method, $params);
4755
}
56+
4857
if ($class != $declaredClass && isset($this->classMap[$declaredClass])) {
4958
Registry::registerClassCall($declaredClass, $method, $params);
5059
}
5160

52-
if ($invocation instanceof \AspectMock\Intercept\MethodInvocation) {
61+
if ($invocation instanceof MethodInvocation) {
5362
$result = $this->invokeFakedMethods($invocation);
5463
}
55-
64+
5665
return $result;
5766
}
5867

5968
public function fakeFunctionAndRegisterCalls($namespace, $function, $args)
6069
{
6170
$result = __AM_CONTINUE__;
62-
$fullFuncName = "$namespace\\$function";
71+
$fullFuncName = sprintf('%s\%s', $namespace, $function);
6372
Registry::registerFunctionCall($fullFuncName, $args);
6473

6574
if (isset($this->funcMap[$fullFuncName])) {
6675
$func = $this->funcMap[$fullFuncName];
67-
if (is_callable($func)) {
68-
$result = call_user_func_array($func, $args);
69-
} else {
70-
$result = $func;
71-
}
76+
$result = is_callable($func) ? call_user_func_array($func, $args) : $func;
7277
}
78+
7379
return $result;
7480
}
7581

82+
/**
83+
* @return mixed
84+
*/
7685
protected function invokeFakedMethods(MethodInvocation $invocation)
7786
{
7887
$method = $invocation->getMethod();
@@ -155,6 +164,7 @@ protected function invokeFakedMethods(MethodInvocation $invocation)
155164
}
156165
}
157166
}
167+
158168
return __AM_CONTINUE__;
159169
}
160170

@@ -164,10 +174,12 @@ protected function getObjectMethodStubParams($obj, $method_name)
164174
if (!isset($this->objectMap[$oid])) {
165175
return false;
166176
}
177+
167178
$params = $this->objectMap[$oid];
168179
if (!array_key_exists($method_name, $params)) {
169180
return false;
170181
}
182+
171183
return $params;
172184
}
173185

@@ -176,10 +188,12 @@ protected function getClassMethodStubParams($class_name, $method_name)
176188
if (!isset($this->classMap[$class_name])) {
177189
return false;
178190
}
191+
179192
$params = $this->classMap[$class_name];
180193
if (!array_key_exists($method_name, $params)) {
181194
return false;
182195
}
196+
183197
return $params;
184198
}
185199

@@ -192,14 +206,15 @@ protected function stub(MethodInvocation $invocation, $params)
192206
$replacedMethod = $this->turnToClosure($replacedMethod);
193207

194208
if ($invocation->isStatic()) {
195-
$replacedMethod = \Closure::bind($replacedMethod, null, $invocation->getThis());
209+
$replacedMethod = Closure::bind($replacedMethod, null, $invocation->getThis());
196210
} else {
197211
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
198212
}
213+
199214
return call_user_func_array($replacedMethod, $invocation->getArguments());
200215
}
201216

202-
protected function stubMagicMethod(MethodInvocation $invocation, $params)
217+
protected function stubMagicMethod(MethodInvocation $invocation, array $params)
203218
{
204219
$args = $invocation->getArguments();
205220
$name = array_shift($args);
@@ -208,56 +223,62 @@ protected function stubMagicMethod(MethodInvocation $invocation, $params)
208223
$replacedMethod = $this->turnToClosure($replacedMethod);
209224

210225
if ($invocation->isStatic()) {
211-
\Closure::bind($replacedMethod, null, $invocation->getThis());
226+
Closure::bind($replacedMethod, null, $invocation->getThis());
212227
} else {
213228
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
214229
}
230+
215231
return call_user_func_array($replacedMethod, $args);
216232
}
217233

218234

219-
protected function turnToClosure($returnValue)
235+
protected function turnToClosure($returnValue): Closure
220236
{
221-
if ($returnValue instanceof \Closure) {
237+
if ($returnValue instanceof Closure) {
222238
return $returnValue;
223239
}
224-
return function () use ($returnValue) {
225-
return $returnValue;
226-
};
240+
241+
return fn() => $returnValue;
227242
}
228243

229-
public function registerClass($class, $params = [])
244+
public function registerClass(string $class, array $params = []): void
230245
{
231246
$class = ltrim($class, '\\');
232247
if (isset($this->classMap[$class])) {
233248
$params = array_merge($this->classMap[$class], $params);
234249
}
250+
235251
$this->methodMap = array_merge($this->methodMap, array_keys($params));
236252
$this->classMap[$class] = $params;
237253
}
238254

239-
public function registerObject($object, $params = [])
255+
public function registerObject(object $object, array $params = []): void
240256
{
241257
$hash = spl_object_hash($object);
242258
if (isset($this->objectMap[$hash])) {
243259
$params = array_merge($this->objectMap[$hash], $params);
244260
}
261+
245262
$this->objectMap[$hash] = $params;
246263
$this->methodMap = array_merge($this->methodMap, array_keys($params));
247264
}
248265

249-
public function registerFunc($namespace, $func, $body)
266+
/**
267+
* @param string|Closure $func
268+
*/
269+
public function registerFunc(string $namespace, $func, $body): void
250270
{
251271
$namespace = ltrim($namespace, '\\');
252-
if (!function_exists("$namespace\\$func")) {
272+
if (!function_exists("{$namespace}\\{$func}")) {
253273
$injector = new FunctionInjector($namespace, $func);
254274
$injector->save();
255275
$injector->inject();
256276
}
257-
$this->funcMap["$namespace\\$func"] = $body;
277+
278+
$this->funcMap["{$namespace}\\{$func}"] = $body;
258279
}
259280

260-
public function clean($objectOrClass = null)
281+
public function clean($objectOrClass = null): void
261282
{
262283
if (!$objectOrClass) {
263284
$this->classMap = [];
@@ -270,5 +291,4 @@ public function clean($objectOrClass = null)
270291
unset($this->classMap[$objectOrClass]);
271292
}
272293
}
273-
274294
}

src/AspectMock/Core/Registry.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace AspectMock\Core;
6+
37
use AspectMock\Proxy\ClassProxy;
48
use AspectMock\Proxy\InstanceProxy;
59

@@ -9,55 +13,50 @@
913
* Class Registry
1014
* @package AspectMock
1115
*/
12-
class Registry {
16+
class Registry
17+
{
18+
protected static array $classCalls = [];
1319

14-
protected static $classCalls = [];
15-
protected static $instanceCalls = [];
16-
protected static $funcCalls = [];
20+
protected static array $instanceCalls = [];
1721

18-
/**
19-
* @var Mocker
20-
*/
21-
public static $mocker;
22+
protected static array $funcCalls = [];
2223

23-
static function registerClass($name, $params = array())
24+
public static ?Mocker $mocker = null;
25+
26+
public static function registerClass($name, $params = array()): void
2427
{
2528
self::$mocker->registerClass($name, $params);
2629
}
2730

28-
static function registerObject($object, $params = array())
31+
public static function registerObject($object, $params = array()): void
2932
{
3033
self::$mocker->registerObject($object, $params);
3134
}
3235

33-
static function registerFunc($namespace, $function, $resultOrClosure)
36+
public static function registerFunc($namespace, $function, $resultOrClosure): void
3437
{
3538
self::$mocker->registerFunc($namespace, $function, $resultOrClosure);
3639
}
3740

38-
static function getClassCallsFor($class)
41+
public static function getClassCallsFor($class)
3942
{
4043
$class = ltrim($class,'\\');
41-
return isset(self::$classCalls[$class])
42-
? self::$classCalls[$class]
43-
: [];
44+
return self::$classCalls[$class] ?? [];
4445
}
4546

46-
static function getInstanceCallsFor($instance)
47+
public static function getInstanceCallsFor($instance)
4748
{
4849
$oid = spl_object_hash($instance);
49-
return isset(self::$instanceCalls[$oid])
50-
? self::$instanceCalls[$oid]
51-
: [];
50+
return self::$instanceCalls[$oid] ?? [];
5251
}
5352

54-
static function getFuncCallsFor($func)
53+
public static function getFuncCallsFor($func)
5554
{
5655
$func = ltrim($func,'\\');
57-
return isset(self::$funcCalls[$func]) ? self::$funcCalls[$func] : [];
56+
return self::$funcCalls[$func] ?? [];
5857
}
5958

60-
static function clean($classOrInstance = null)
59+
public static function clean($classOrInstance = null): void
6160
{
6261
$classOrInstance = self::getRealClassOrObject($classOrInstance);
6362
self::$mocker->clean($classOrInstance);
@@ -73,14 +72,14 @@ static function clean($classOrInstance = null)
7372
}
7473
}
7574

76-
static function cleanInvocations()
75+
public static function cleanInvocations(): void
7776
{
7877
self::$instanceCalls = [];
7978
self::$classCalls = [];
8079
self::$funcCalls = [];
8180
}
8281

83-
static function registerInstanceCall($instance, $method, $args = array())
82+
public static function registerInstanceCall($instance, $method, $args = array()): void
8483
{
8584
$oid = spl_object_hash($instance);
8685
if (!isset(self::$instanceCalls[$oid])) self::$instanceCalls[$oid] = [];
@@ -91,7 +90,7 @@ static function registerInstanceCall($instance, $method, $args = array())
9190

9291
}
9392

94-
static function registerClassCall($class, $method, $args = array())
93+
public static function registerClassCall($class, $method, $args = array()): void
9594
{
9695
if (!isset(self::$classCalls[$class])) self::$classCalls[$class] = [];
9796

@@ -101,7 +100,7 @@ static function registerClassCall($class, $method, $args = array())
101100

102101
}
103102

104-
static function registerFunctionCall($functionName, $args)
103+
public static function registerFunctionCall($functionName, $args): void
105104
{
106105
if (!isset(self::$funcCalls[$functionName])) self::$funcCalls[$functionName] = [];
107106

@@ -113,16 +112,14 @@ static function registerFunctionCall($functionName, $args)
113112
public static function getRealClassOrObject($classOrObject)
114113
{
115114
if ($classOrObject instanceof ClassProxy) return $classOrObject->className;
115+
116116
if ($classOrObject instanceof InstanceProxy) return $classOrObject->getObject();
117+
117118
return $classOrObject;
118119
}
119120

120-
/**
121-
* @param mixed $mocker
122-
*/
123-
public static function setMocker(Mocker $mocker)
121+
public static function setMocker(Mocker $mocker): void
124122
{
125123
self::$mocker = $mocker;
126124
}
127-
128125
}

0 commit comments

Comments
 (0)