Skip to content

Commit 8f27e5f

Browse files
committed
Fix the issue with PHP 8 throwing an error for method_exists when it's an array
1 parent 07564db commit 8f27e5f

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

src/Factories/VuexFactory.php

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,33 @@ public function __construct(Container $container)
3131
* @var array Base (non-namespaced) vuex state.
3232
* @var array Base (non-namespaced) lazily evaluated vuex state.
3333
*/
34-
protected $_state = [],
35-
$_lazyState = [];
34+
protected $_state = [];
35+
36+
protected $_lazyState = [];
3637

3738
/**
3839
* @var array Vuex modules.
3940
* @var array Lazily evaluated Vuex modules.
4041
*/
41-
protected $_modules = [],
42-
$_lazyModules = [];
42+
protected $_modules = [];
43+
44+
protected $_lazyModules = [];
4345

4446
/**
4547
* @var array Mutations to be committed.
4648
* @var array Lazy Evaluated mutations to be committed.
4749
*/
48-
protected $_mutations = [],
49-
$_lazyMutations = [];
50+
protected $_mutations = [];
51+
52+
protected $_lazyMutations = [];
5053

5154
/**
5255
* @var array Actions to be dispatched.
5356
* @var array Lazily Evaluated actions to be dispatched.
5457
*/
55-
protected $_actions = [],
56-
$_lazyActions = [];
58+
protected $_actions = [];
59+
60+
protected $_lazyActions = [];
5761

5862
/** @var array Registered Classes for vuex ModuleLoaders. */
5963
protected $registeredMappings = [];
@@ -75,7 +79,7 @@ public function register($mappings)
7579

7680
return [
7781
$loader->getNamespace() => [
78-
'class' => $location,
82+
'class' => $location,
7983
'methods' => get_class_methods($loader),
8084
],
8185
];
@@ -126,11 +130,11 @@ protected function loadModule(string $namespace, $keys, array $args, bool $lazy)
126130
$keys = [$keys => $args];
127131
}
128132

129-
if (! isset($this->registeredMappings[$namespace])) {
133+
if (!isset($this->registeredMappings[$namespace])) {
130134
throw new VuexInvalidModuleException("VuexLoader '{$namespace}' has not been properly registered.");
131135
}
132136

133-
if (! is_array($keys)) {
137+
if (!is_array($keys)) {
134138
throw new VuexInvalidKeyException('Invalid keys were passed to Vuex::load.');
135139
}
136140

@@ -142,15 +146,15 @@ protected function loadModule(string $namespace, $keys, array $args, bool $lazy)
142146
return is_int($key) ? [$value => null] : [$key => $value];
143147
})
144148
->each(function ($args, $key) use ($namespace, $moduleLoader, $lazy) {
145-
if (! in_array($key, $this->registeredMappings[$namespace]['methods'])) {
149+
if (!in_array($key, $this->registeredMappings[$namespace]['methods'])) {
146150
throw new VuexInvalidKeyException("Method '{$key}' does not exist on '{$this->registeredMappings[$namespace]['class']}'");
147151
}
148152

149-
if (! is_array($args)) {
153+
if (!is_array($args)) {
150154
$args = [$args];
151155
}
152156

153-
$params = $this->resolveClassMethodDependencies( $args, $moduleLoader, $key );
157+
$params = $this->resolveClassMethodDependencies($args, $moduleLoader, $key);
154158

155159
if ($lazy) {
156160
Vuex::module(
@@ -159,7 +163,7 @@ protected function loadModule(string $namespace, $keys, array $args, bool $lazy)
159163
$key => function () use ($moduleLoader, $key, $params) {
160164
return $moduleLoader->{$key}(...array_values($params));
161165
},
162-
]
166+
]
163167
);
164168
} else {
165169
Vuex::module(
@@ -214,7 +218,7 @@ public function state($state)
214218
*/
215219
public function module(string $namespace, $state)
216220
{
217-
if (! is_string($namespace) || empty($namespace)) {
221+
if (!is_string($namespace) || empty($namespace)) {
218222
throw new VuexInvalidModuleException('$namespace must be a string.');
219223
}
220224

@@ -237,61 +241,61 @@ public function toArray()
237241
{
238242
$store = [];
239243

240-
if (! empty($this->_state) || ! empty($this->_lazyState)) {
244+
if (!empty($this->_state) || !empty($this->_lazyState)) {
241245
$store['state'] = [];
242246
}
243247

244-
if (! empty($this->_modules) || ! empty($this->_lazyModules)) {
248+
if (!empty($this->_modules) || !empty($this->_lazyModules)) {
245249
$store['modules'] = [];
246250
}
247251

248-
if (! empty($this->_mutations) || ! empty($this->_lazyMutations)) {
252+
if (!empty($this->_mutations) || !empty($this->_lazyMutations)) {
249253
$store['mutations'] = [];
250254
}
251255

252-
if (! empty($this->_actions) || ! empty($this->_lazyActions)) {
256+
if (!empty($this->_actions) || !empty($this->_lazyActions)) {
253257
$store['actions'] = [];
254258
}
255259

256-
if (! empty($this->_state)) {
260+
if (!empty($this->_state)) {
257261
$store['state'] = $this->reduceData($this->_state, $store['state'], function ($acc, $cur) {
258262
return array_merge_phase($acc, $this->generateState($cur));
259263
});
260264
}
261265

262-
if (! empty($this->_lazyState)) {
266+
if (!empty($this->_lazyState)) {
263267
$store['state'] = $this->reduceData($this->_lazyState, $store['state'], function ($acc, $cur) {
264268
return array_merge_phase($acc, $this->generateLazyState($cur));
265269
});
266270
}
267271

268-
if (! empty($this->_modules)) {
272+
if (!empty($this->_modules)) {
269273
foreach ($this->_modules as $module) {
270274
$store['modules'] = array_merge_phase($store['modules'], $this->generateNamespacedModules($module));
271275
}
272276
}
273277

274-
if (! empty($this->_lazyModules)) {
278+
if (!empty($this->_lazyModules)) {
275279
foreach ($this->_lazyModules as $module) {
276280
$store['modules'] = array_merge_phase($store['modules'], $this->generateLazyNamespacedModules($module));
277281
}
278282
}
279283

280-
if (! empty($this->_mutations)) {
284+
if (!empty($this->_mutations)) {
281285
$store['mutations'] = $this->_mutations;
282286
}
283287

284-
if (! empty($this->_lazyMutations)) {
288+
if (!empty($this->_lazyMutations)) {
285289
foreach ($this->_lazyMutations as $mutation => $value) {
286290
array_push($store['mutations'], [$mutation, $value()]);
287291
}
288292
}
289293

290-
if (! empty($this->_actions)) {
294+
if (!empty($this->_actions)) {
291295
$store['actions'] = $this->_actions;
292296
}
293297

294-
if (! empty($this->_lazyActions)) {
298+
if (!empty($this->_lazyActions)) {
295299
foreach ($this->_lazyActions as $action => $value) {
296300
array_push($store['actions'], [$action, $value()]);
297301
}
@@ -300,27 +304,29 @@ public function toArray()
300304
return $store;
301305
}
302306

303-
public function recursiveGet(array $selectors, array $data) {
307+
public function recursiveGet(array $selectors, array $data)
308+
{
304309
// Grab the first item
305310
$first = array_shift($selectors);
306311

307312
if (isset($data['state'][$first])) {
308313
// return from state if possible
309314
return $data['state'][$first];
310-
} else if (isset($data['modules'][$first])) {
315+
} elseif (isset($data['modules'][$first])) {
311316
// pass to nested module and check its state
312317
return $this->recursiveGet($selectors, $data['modules'][$first]);
313-
}
318+
}
314319

315-
// all unfound items will happily return null (no errors)
316-
return null;
320+
// all unfound items will happily return null (no errors)
321+
return null;
317322
}
318323

319324
/**
320325
* Usage: to get this.$store.state.users.active.name
321326
* Vuex::get('users.active.name')
322327
*/
323-
public function get(string $selector) {
328+
public function get(string $selector)
329+
{
324330
$parts = explode('.', $selector);
325331
return $this->recursiveGet($parts, $this->toArray());
326332
}
@@ -409,16 +415,16 @@ public function toResponse()
409415
*/
410416
public function verifyState($state)
411417
{
412-
if (method_exists($state, 'toArray')) {
413-
$state = $state->toArray();
414-
} elseif (is_array($state)) {
418+
if (is_array($state)) {
415419
$state = collect($state)->toArray();
416-
} elseif (! is_callable($state)) {
420+
} elseif (method_exists($state, 'toArray')) {
421+
$state = $state->toArray();
422+
} elseif (!is_callable($state)) {
417423
throw new VuexInvalidStateException('$state must be an array or a Collection.');
418424
}
419425

420426
foreach ($state as $key => $value) {
421-
if (method_exists($value, 'toArray')) {
427+
if (method_exists((object) $value, 'toArray')) {
422428
$state[$key] = $value->toArray();
423429
}
424430
}
@@ -476,7 +482,7 @@ protected function generateLazyState($state)
476482
protected function generateNamespacedModules($module)
477483
{
478484
foreach ($module as $namespace => $state) {
479-
if (! Str::contains($namespace, '/')) { // simple module namespace
485+
if (!Str::contains($namespace, '/')) { // simple module namespace
480486
return [$namespace => ['state' => $state]];
481487
} else { // complex nested modules namespace
482488
$namespaces = array_reverse(collect(explode('/', $namespace))->toArray());
@@ -499,7 +505,7 @@ protected function generateNamespacedModules($module)
499505
protected function generateLazyNamespacedModules($module)
500506
{
501507
foreach ($module as $namespace => $state) {
502-
if (! Str::contains($namespace, '/')) { // simple module namespace
508+
if (!Str::contains($namespace, '/')) { // simple module namespace
503509
if (is_callable($state)) {
504510
$state = $state();
505511
}
@@ -542,11 +548,11 @@ protected function generateLazyNamespacedModules($module)
542548
*/
543549
public function dispatch($action, $value = null)
544550
{
545-
if (! is_string($action) || empty($action)) {
551+
if (!is_string($action) || empty($action)) {
546552
throw new VuexInvalidModuleException('$mutation must be a string.');
547553
}
548554

549-
if (! isset($value)) {
555+
if (!isset($value)) {
550556
array_push($this->_actions, [$action]);
551557
} elseif (is_string($value) || is_bool($value) || is_numeric($value)) {
552558
array_push($this->_actions, [$action, $value]);
@@ -567,11 +573,11 @@ public function dispatch($action, $value = null)
567573
*/
568574
public function commit($mutation, $value = null)
569575
{
570-
if (! is_string($mutation) || empty($mutation)) {
576+
if (!is_string($mutation) || empty($mutation)) {
571577
throw new VuexInvalidModuleException('$mutation must be a string.');
572578
}
573579

574-
if (! isset($value)) {
580+
if (!isset($value)) {
575581
array_push($this->_mutations, [$mutation]);
576582
} elseif (is_string($value) || is_bool($value) || is_numeric($value)) {
577583
array_push($this->_mutations, [$mutation, $value]);

0 commit comments

Comments
 (0)