ThrusterIO /
promise
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Thruster\Component\Promise; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * @param PromiseInterface|ExtendedPromiseInterface|mixed $promiseOrValue |
||
| 7 | * |
||
| 8 | * @return FulfilledPromise|Promise |
||
| 9 | */ |
||
| 10 | function resolve($promiseOrValue = null) |
||
| 11 | { |
||
| 12 | 369 | if (false === ($promiseOrValue instanceof PromiseInterface)) { |
|
| 13 | 325 | return new FulfilledPromise($promiseOrValue); |
|
| 14 | } |
||
| 15 | |||
| 16 | 254 | if ($promiseOrValue instanceof ExtendedPromiseInterface) { |
|
| 17 | 251 | return $promiseOrValue; |
|
| 18 | } |
||
| 19 | |||
| 20 | 3 | return new Promise( |
|
| 21 | function ($resolve, $reject, $notify) use ($promiseOrValue) { |
||
| 22 | 3 | $promiseOrValue->then($resolve, $reject, $notify); |
|
| 23 | 3 | } |
|
| 24 | ); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param PromiseInterface|ExtendedPromiseInterface|mixed $promiseOrValue |
||
| 29 | * |
||
| 30 | * @return PromiseInterface|RejectedPromise |
||
| 31 | */ |
||
| 32 | function reject($promiseOrValue = null) |
||
| 33 | { |
||
| 34 | 187 | if ($promiseOrValue instanceof PromiseInterface) { |
|
| 35 | 8 | return resolve($promiseOrValue)->then( |
|
| 36 | function ($value) { |
||
| 37 | 4 | return new RejectedPromise($value); |
|
| 38 | 8 | } |
|
| 39 | ); |
||
| 40 | } |
||
| 41 | |||
| 42 | 182 | return new RejectedPromise($promiseOrValue); |
|
| 43 | } |
||
| 44 | |||
| 45 | function all($promisesOrValues) |
||
| 46 | { |
||
| 47 | return map($promisesOrValues, function ($val) { |
||
| 48 | 5 | return $val; |
|
| 49 | 7 | }); |
|
| 50 | } |
||
| 51 | |||
| 52 | function race($promisesOrValues) |
||
| 53 | { |
||
| 54 | 7 | return resolve($promisesOrValues) |
|
| 55 | ->then(function ($array) { |
||
| 56 | 7 | if (!is_array($array) || !$array) { |
|
|
0 ignored issues
–
show
|
|||
| 57 | 2 | return resolve(); |
|
| 58 | } |
||
| 59 | |||
| 60 | return new Promise(function ($resolve, $reject, $notify) use ($array) { |
||
| 61 | 5 | foreach ($array as $promiseOrValue) { |
|
| 62 | 5 | resolve($promiseOrValue) |
|
| 63 | 5 | ->done($resolve, $reject, $notify); |
|
| 64 | } |
||
| 65 | 5 | }); |
|
| 66 | 7 | }); |
|
| 67 | } |
||
| 68 | |||
| 69 | function any($promisesOrValues) |
||
| 70 | { |
||
| 71 | 8 | return some($promisesOrValues, 1) |
|
| 72 | ->then(function ($val) { |
||
| 73 | 7 | return array_shift($val); |
|
| 74 | 8 | }); |
|
| 75 | } |
||
| 76 | |||
| 77 | function some($promisesOrValues, $howMany) |
||
| 78 | { |
||
| 79 | 16 | return resolve($promisesOrValues) |
|
| 80 | ->then(function ($array) use ($howMany) { |
||
| 81 | 16 | if (!is_array($array) || !$array || $howMany < 1) { |
|
|
0 ignored issues
–
show
The expression
$array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 82 | 5 | return resolve([]); |
|
| 83 | } |
||
| 84 | |||
| 85 | return new Promise(function ($resolve, $reject, $notify) use ($array, $howMany) { |
||
| 86 | 11 | $len = count($array); |
|
| 87 | 11 | $toResolve = min($howMany, $len); |
|
| 88 | 11 | $toReject = ($len - $toResolve) + 1; |
|
| 89 | 11 | $values = []; |
|
| 90 | 11 | $reasons = []; |
|
| 91 | |||
| 92 | 11 | foreach ($array as $i => $promiseOrValue) { |
|
| 93 | $fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) { |
||
| 94 | 10 | if ($toResolve < 1 || $toReject < 1) { |
|
| 95 | 8 | return; |
|
| 96 | } |
||
| 97 | |||
| 98 | 10 | $values[$i] = $val; |
|
| 99 | |||
| 100 | 10 | if (0 === --$toResolve) { |
|
| 101 | 9 | $resolve($values); |
|
| 102 | } |
||
| 103 | 11 | }; |
|
| 104 | |||
| 105 | $rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) { |
||
| 106 | 3 | if ($toResolve < 1 || $toReject < 1) { |
|
| 107 | 1 | return; |
|
| 108 | } |
||
| 109 | |||
| 110 | 2 | $reasons[$i] = $reason; |
|
| 111 | |||
| 112 | 2 | if (0 === --$toReject) { |
|
| 113 | 2 | $reject($reasons); |
|
| 114 | } |
||
| 115 | 11 | }; |
|
| 116 | |||
| 117 | 11 | resolve($promiseOrValue) |
|
| 118 | 11 | ->done($fulfiller, $rejecter, $notify); |
|
| 119 | } |
||
| 120 | 11 | }); |
|
| 121 | 16 | }); |
|
| 122 | } |
||
| 123 | |||
| 124 | function map($promisesOrValues, callable $mapFunc) |
||
| 125 | { |
||
| 126 | 14 | return resolve($promisesOrValues) |
|
| 127 | ->then(function ($array) use ($mapFunc) { |
||
| 128 | 14 | if (!is_array($array) || !$array) { |
|
|
0 ignored issues
–
show
The expression
$array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 129 | 3 | return resolve([]); |
|
| 130 | } |
||
| 131 | |||
| 132 | return new Promise(function ($resolve, $reject, $notify) use ($array, $mapFunc) { |
||
| 133 | 11 | $toResolve = count($array); |
|
| 134 | 11 | $values = []; |
|
| 135 | |||
| 136 | 11 | foreach ($array as $i => $promiseOrValue) { |
|
| 137 | 11 | resolve($promiseOrValue) |
|
| 138 | 11 | ->then($mapFunc) |
|
| 139 | 11 | ->done( |
|
| 140 | function ($mapped) use ($i, &$values, &$toResolve, $resolve) { |
||
| 141 | 11 | $values[$i] = $mapped; |
|
| 142 | |||
| 143 | 11 | if (0 === --$toResolve) { |
|
| 144 | 9 | $resolve($values); |
|
| 145 | } |
||
| 146 | 11 | }, |
|
| 147 | $reject, |
||
| 148 | $notify |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | 11 | }); |
|
| 152 | 14 | }); |
|
| 153 | } |
||
| 154 | |||
| 155 | function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null) |
||
| 156 | { |
||
| 157 | 16 | return resolve($promisesOrValues) |
|
| 158 | ->then(function ($array) use ($reduceFunc, $initialValue) { |
||
| 159 | 16 | if (!is_array($array)) { |
|
| 160 | 1 | $array = []; |
|
| 161 | } |
||
| 162 | |||
| 163 | 16 | $total = count($array); |
|
| 164 | 16 | $i = 0; |
|
| 165 | |||
| 166 | // Wrap the supplied $reduceFunc with one that handles promises and then |
||
| 167 | // delegates to the supplied. |
||
| 168 | $wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $total, &$i) { |
||
| 169 | 12 | return resolve($current) |
|
| 170 | ->then(function ($c) use ($reduceFunc, $total, &$i, $val) { |
||
| 171 | 12 | return resolve($val) |
|
| 172 | ->then(function ($value) use ($reduceFunc, $total, &$i, $c) { |
||
| 173 | 12 | return $reduceFunc($c, $value, $i++, $total); |
|
| 174 | 12 | }); |
|
| 175 | 12 | }); |
|
| 176 | 16 | }; |
|
| 177 | |||
| 178 | 16 | return array_reduce($array, $wrappedReduceFunc, $initialValue); |
|
| 179 | 16 | }); |
|
| 180 | } |
||
| 181 | |||
| 182 | // Internal functions |
||
| 183 | function _checkTypehint(callable $callback, $object) |
||
| 184 | { |
||
| 185 | 19 | if (!is_object($object)) { |
|
| 186 | 7 | return true; |
|
| 187 | } |
||
| 188 | |||
| 189 | 12 | if (is_array($callback)) { |
|
| 190 | $callbackReflection = new \ReflectionMethod($callback[0], $callback[1]); |
||
| 191 | 12 | } elseif (is_object($callback) && !$callback instanceof \Closure) { |
|
| 192 | $callbackReflection = new \ReflectionMethod($callback, '__invoke'); |
||
| 193 | } else { |
||
| 194 | 12 | $callbackReflection = new \ReflectionFunction($callback); |
|
| 195 | } |
||
| 196 | |||
| 197 | 12 | $parameters = $callbackReflection->getParameters(); |
|
| 198 | |||
| 199 | 12 | if (!isset($parameters[0])) { |
|
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | 12 | $expectedException = $parameters[0]; |
|
| 204 | |||
| 205 | 12 | if (!$expectedException->getClass()) { |
|
| 206 | 4 | return true; |
|
| 207 | } |
||
| 208 | |||
| 209 | 8 | return $expectedException->getClass()->isInstance($object); |
|
| 210 | } |
||
| 211 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.