Skip to content

Commit 26f1ac5

Browse files
committed
Update coding style
1 parent 7ba229e commit 26f1ac5

File tree

14 files changed

+196
-121
lines changed

14 files changed

+196
-121
lines changed

Helper/HooksHelper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Goomento\Core\Helper;
1010

11+
use Goomento\Core\Model\Transport;
1112
use Goomento\Core\Traits\TraitStaticInstances;
1213
use Goomento\Core\Model\HookManager;
1314

@@ -39,12 +40,13 @@ public static function addAction(string $tag, $callbackToAdd, int $sortOrder = 1
3940
/**
4041
* Hooks a function or method to a specific filter action.
4142
*
42-
* @param string $tag The name of the filter to hook the $function_to_add callback to.
43+
* @param string $tag The name of the filter to hook the $callbackToAdd callback to.
4344
* @param callable|string|array $callbackToAdd The callback to be run when the filter is applied.
4445
* @param int $sortOrder The order in which the functions associated with a
4546
* particular action are executed. Lower numbers correspond with
4647
* earlier execution, and functions with the same priority are executed
4748
* in the order in which they were added to the action.
49+
* @return Transport
4850
*
4951
*/
5052
public static function addFilter(
@@ -55,7 +57,7 @@ public static function addFilter(
5557
{
5658
/** @var HookManager $instance */
5759
$instance = self::getInstance(HookManager::class);
58-
$instance->addFilter($tag, $callbackToAdd, $sortOrder);
60+
return $instance->addFilter($tag, $callbackToAdd, $sortOrder);
5961
}
6062

6163
/**
@@ -69,6 +71,7 @@ public static function addFilter(
6971
* The function also allows for multiple additional arguments to be passed to hooks.
7072
* @param string $tag The name of the filter hook.
7173
* @param mixed $value The value to filter.
74+
* @return Transport
7275
*/
7376
public static function applyFilters(string $tag, ...$value)
7477
{
@@ -143,7 +146,7 @@ public static function removeAction(string $tag, $callbackToRemove, int $sortOrd
143146
* @param string $tag The name of the filter hook. Used for building
144147
* the callback ID when SPL is not available. Default empty.
145148
* @param callable|null $functionToCheck Optional. The callback to check for. Default false.
146-
* @return bool|int The priority of that hook is returned, or false if the function is not attached.
149+
* @return bool The priority of that hook is returned, or false if the function is not attached.
147150
*/
148151
public static function hasFilter($tag, $functionToCheck = null)
149152
{

Model/AssetDependencies.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,18 @@ abstract class AssetDependencies
5050
* @var array
5151
*/
5252
protected $groups = [];
53+
5354
/**
5455
* @var HookManager
56+
* @deprecated
5557
*/
5658
protected $hookManager;
5759

5860
/**
5961
* ScriptsManager constructor.
60-
* @param HookManager $hookManager
6162
*/
62-
public function __construct(
63-
HookManager $hookManager
64-
) {
65-
$this->hookManager = $hookManager;
63+
public function __construct()
64+
{
6665
$this->init();
6766
}
6867

@@ -132,31 +131,31 @@ public function allDeps($handles, $recursion = false, $group = false)
132131
}
133132

134133
foreach ($handles as $handle) {
135-
$handle_parts = explode('?', $handle);
136-
$handle = $handle_parts[0];
134+
$handleParts = explode('?', $handle);
135+
$handle = $handleParts[0];
137136
$queued = in_array($handle, $this->toDo, true);
138137

139138
if (in_array($handle, $this->done, true)) { // Already done
140139
continue;
141140
}
142141

143142
$moved = $this->setGroup($handle, $recursion, $group);
144-
$new_group = $this->groups[ $handle ];
143+
$newGroup = $this->groups[ $handle ];
145144

146145
if ($queued && ! $moved) { // already queued and in the right group
147146
continue;
148147
}
149148

150-
$keep_going = true;
149+
$keepGoing = true;
151150
if (! isset($this->registered[ $handle ])) {
152-
$keep_going = false; // Item doesn't exist.
151+
$keepGoing = false; // Item doesn't exist.
153152
} elseif ($this->registered[ $handle ]['deps'] && array_diff($this->registered[ $handle ]['deps'], array_keys($this->registered))) {
154-
$keep_going = false; // Item requires dependencies that don't exist.
155-
} elseif ($this->registered[ $handle ]['deps'] && ! $this->allDeps($this->registered[ $handle ]['deps'], true, $new_group)) {
156-
$keep_going = false; // Item requires dependencies that don't exist.
153+
$keepGoing = false; // Item requires dependencies that don't exist.
154+
} elseif ($this->registered[ $handle ]['deps'] && ! $this->allDeps($this->registered[ $handle ]['deps'], true, $newGroup)) {
155+
$keepGoing = false; // Item requires dependencies that don't exist.
157156
}
158157

159-
if (! $keep_going) { // Either item or its dependencies don't exist.
158+
if (! $keepGoing) { // Either item or its dependencies don't exist.
160159
if ($recursion) {
161160
return false; // Abort this branch.
162161
} else {
@@ -168,8 +167,8 @@ public function allDeps($handles, $recursion = false, $group = false)
168167
continue;
169168
}
170169

171-
if (isset($handle_parts[1])) {
172-
$this->args[ $handle ] = $handle_parts[1];
170+
if (isset($handleParts[1])) {
171+
$this->args[ $handle ] = $handleParts[1];
173172
}
174173

175174
$this->toDo[] = $handle;

Model/Hook.php

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ class Hook implements Iterator, ArrayAccess
5151
*/
5252
private $doingAction = false;
5353

54+
/**
55+
* @var Transport
56+
*/
57+
private $transport;
58+
59+
/**
60+
* Hook Constructor
61+
*/
62+
public function __construct()
63+
{
64+
$this->transport = new Transport([]);
65+
}
66+
5467
/**
5568
* Reveal the callbacks
5669
*
@@ -63,56 +76,59 @@ public function getCallBacks()
6376

6477
/**
6578
* Hooks a function or method to a specific filter action.
79+
* @return Transport
6680
*/
6781
public function addFilter(string $tag, $functionToAdd, int $priority)
6882
{
6983
$idx = $this->uniqueId($tag, $functionToAdd, $priority);
70-
$priority_existed = isset($this->callbacks[$priority]);
84+
$priorityExisted = isset($this->callbacks[$priority]);
7185

7286
$this->callbacks[$priority][$idx] = [
7387
'function' => $functionToAdd,
7488
];
7589

7690
// if we're adding a new priority to the list, put them back in sorted order
77-
if (!$priority_existed && count($this->callbacks) > 1) {
91+
if (!$priorityExisted && count($this->callbacks) > 1) {
7892
ksort($this->callbacks, SORT_NUMERIC);
7993
}
8094

8195
if ($this->nestingLevel > 0) {
82-
$this->resortActiveIterations($priority, $priority_existed);
96+
$this->resortActiveIterations($priority, $priorityExisted);
8397
}
98+
99+
return $this->transport;
84100
}
85101

86102
/**
87103
* Handles resetting callback priority keys mid-iteration.
88104
*
89-
* @param bool|int $new_priority Optional. The priority of the new filter being added. Default false,
105+
* @param bool|int $newPriority Optional. The priority of the new filter being added. Default false,
90106
* for no priority being added.
91107
* @param bool $priorityExisted Optional. Flag for whether the priority already existed before the new
92108
* filter was added. Default false.
93109
*
94110
*/
95-
private function resortActiveIterations($new_priority = false, bool $priorityExisted = false)
111+
private function resortActiveIterations($newPriority = false, bool $priorityExisted = false)
96112
{
97-
$new_priorities = array_keys($this->callbacks);
113+
$newPriorities = array_keys($this->callbacks);
98114

99115
// If there are no remaining hooks, clear out all running iterations.
100-
if (!$new_priorities) {
116+
if (!$newPriorities) {
101117
foreach ($this->iterations as $index => $iteration) {
102-
$this->iterations[$index] = $new_priorities;
118+
$this->iterations[$index] = $newPriorities;
103119
}
104120
return;
105121
}
106122

107-
$min = min($new_priorities);
123+
$min = min($newPriorities);
108124
foreach ($this->iterations as $index => &$iteration) {
109125
$current = current($iteration);
110126
// If we're already at the end of this iteration, just leave the array pointer where it is.
111127
if (false === $current) {
112128
continue;
113129
}
114130

115-
$iteration = $new_priorities;
131+
$iteration = $newPriorities;
116132

117133
if ($current < $min) {
118134
array_unshift($iteration, $current);
@@ -125,7 +141,7 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
125141
}
126142
}
127143

128-
if ($new_priority === $this->currentPriority[$index] && !$priorityExisted) {
144+
if ($newPriority === $this->currentPriority[$index] && !$priorityExisted) {
129145
/*
130146
* ... and the new priority is the same as what $this->iterations thinks is the previous
131147
* priority, we need to move back to it.
@@ -141,7 +157,7 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
141157
if (false === $prev) {
142158
// Start of the array. Reset, and go about our day.
143159
reset($iteration);
144-
} elseif ($new_priority !== $prev) {
160+
} elseif ($newPriority !== $prev) {
145161
// Previous wasn't the same. Move forward again.
146162
next($iteration);
147163
}
@@ -162,11 +178,11 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
162178
*/
163179
public function removeFilter(string $tag, callable $functionToRemove, int $priority)
164180
{
165-
$function_key = $this->uniqueId($tag, $functionToRemove, $priority);
181+
$functionKey = $this->uniqueId($tag, $functionToRemove, $priority);
166182

167-
$exists = isset($this->callbacks[$priority][$function_key]);
183+
$exists = isset($this->callbacks[$priority][$functionKey]);
168184
if ($exists) {
169-
unset($this->callbacks[$priority][$function_key]);
185+
unset($this->callbacks[$priority][$functionKey]);
170186
if (!$this->callbacks[$priority]) {
171187
unset($this->callbacks[$priority]);
172188
if ($this->nestingLevel > 0) {
@@ -191,13 +207,13 @@ public function hasFilter(string $tag = '', $functionToCheck = false)
191207
return $this->hasFilters();
192208
}
193209

194-
$function_key = $this->uniqueId($tag, $functionToCheck, false);
195-
if (!$function_key) {
210+
$functionKey = $this->uniqueId($tag, $functionToCheck, false);
211+
if (!$functionKey) {
196212
return false;
197213
}
198214

199215
foreach ($this->callbacks as $priority => $callbacks) {
200-
if (isset($callbacks[$function_key])) {
216+
if (isset($callbacks[$functionKey])) {
201217
return $priority;
202218
}
203219
}
@@ -294,37 +310,63 @@ private function uniqueId(string $tag, $function, $priority)
294310
/**
295311
* @param $value
296312
* @param array $args
297-
* @return false|mixed
313+
* @return Transport
298314
*/
299315
public function applyFilters($value, array $args)
300316
{
317+
$transport = $this->transport;
318+
319+
$transport->setData('_args', $args);
320+
$transport->setData('_value', $value);
321+
322+
$transport->setResult($value);
323+
301324
if (!$this->callbacks) {
302-
return $value;
325+
return $transport;
303326
}
304327

305-
$nesting_level = $this->nestingLevel++;
328+
$nestingLevel = $this->nestingLevel++;
329+
330+
$this->iterations[$nestingLevel] = array_keys($this->callbacks);
306331

307-
$this->iterations[$nesting_level] = array_keys($this->callbacks);
332+
$args[] = $transport;
333+
334+
$valueClass = is_object($value) ? get_class($value) : null;
308335

309336
do {
310-
$this->currentPriority[$nesting_level] = current($this->iterations[$nesting_level]);
311-
$priority = $this->currentPriority[$nesting_level];
337+
$this->currentPriority[$nestingLevel] = current($this->iterations[$nestingLevel]);
338+
339+
$priority = $this->currentPriority[$nestingLevel];
312340

313341
foreach ($this->callbacks[$priority] as $the_) {
314342
if (!$this->doingAction) {
315343
$args[0] = $value;
316344
}
317345

318346
$value = call_user_func_array($the_['function'], $args);
347+
348+
if ($value === null) { // Return void
349+
$value = $transport->getData('_value');
350+
} else {
351+
$valueType = gettype($value);
352+
$resultType = gettype($transport->getResult());
353+
354+
$transport->setResult($value);
355+
356+
if ($valueType !== $resultType || // Not the same type
357+
($valueType === 'object' && $valueClass && get_class($value) !== $valueClass)) { // Not the same class
358+
$value = $transport->getData('_value');
359+
}
360+
}
319361
}
320-
} while (false !== next($this->iterations[$nesting_level]));
362+
} while (false !== next($this->iterations[$nestingLevel]));
321363

322-
unset($this->iterations[$nesting_level]);
323-
unset($this->currentPriority[$nesting_level]);
364+
unset($this->iterations[$nestingLevel]);
365+
unset($this->currentPriority[$nestingLevel]);
324366

325367
$this->nestingLevel--;
326368

327-
return $value;
369+
return $transport;
328370
}
329371

330372
/**
@@ -350,17 +392,17 @@ public function doAction(array $args)
350392
*/
351393
public function doAllHook(array &$args)
352394
{
353-
$nesting_level = $this->nestingLevel++;
354-
$this->iterations[$nesting_level] = array_keys($this->callbacks);
395+
$nestingLevel = $this->nestingLevel++;
396+
$this->iterations[$nestingLevel] = array_keys($this->callbacks);
355397

356398
do {
357-
$priority = current($this->iterations[$nesting_level]);
399+
$priority = current($this->iterations[$nestingLevel]);
358400
foreach ($this->callbacks[$priority] as $the_) {
359401
call_user_func_array($the_['function'], $args);
360402
}
361-
} while (false !== next($this->iterations[$nesting_level]));
403+
} while (false !== next($this->iterations[$nestingLevel]));
362404

363-
unset($this->iterations[$nesting_level]);
405+
unset($this->iterations[$nestingLevel]);
364406
$this->nestingLevel--;
365407
}
366408

0 commit comments

Comments
 (0)