99use Throwable ;
1010
1111class Promise implements PromiseInterface {
12+ use ExecutesPromiseChain;
13+
1214 private bool $ resolvedValueSet = false ;
1315 private bool $ stopChain = false ;
1416
1517 private mixed $ resolvedValue ;
1618 private mixed $ originalResolvedValue ;
1719 private Throwable $ rejectedReason ;
20+ /** @var array<Chainable> */
1821 private array $ chain ;
19- private array $ uncalledCatchChain ;
22+ /** @var array<Throwable> */
2023 private array $ handledRejections ;
2124 /** @var callable */
2225 private $ executor ;
2326
2427 public function __construct (callable $ executor ) {
2528 $ this ->chain = [];
26- $ this ->uncalledCatchChain = [];
2729 $ this ->handledRejections = [];
2830 $ this ->executor = $ executor ;
2931 $ this ->callExecutor ();
@@ -131,64 +133,11 @@ private function tryComplete():void {
131133 }
132134 }
133135
134- private function complete ():void {
135- usort (
136- $ this ->chain ,
137- function (Chainable $ a , Chainable $ b ) {
138- if ($ a instanceof FinallyChain && !($ b instanceof FinallyChain)) return 1 ;
139- if ($ b instanceof FinallyChain && !($ a instanceof FinallyChain)) return -1 ;
140- return 0 ;
141- }
142- );
143-
144- while ($ this ->getState () !== PromiseState::PENDING ) {
145- $ chainItem = $ this ->getNextChainItem ();
146- if (!$ chainItem ) break ;
147-
148- if ($ chainItem instanceof ThenChain || $ chainItem instanceof FinallyChain) {
149- try {
150- if ($ this ->resolvedValueSet && isset ($ this ->resolvedValue )) {
151- $ chainItem ->checkResolutionCallbackType ($ this ->resolvedValue );
152- }
153- }
154- catch (ChainFunctionTypeError ) {
155- continue ;
156- }
157-
158- if ($ chainItem instanceof ThenChain) {
159- if ($ this ->handleThen ($ chainItem )) {
160- $ this ->emptyChain ();
161- }
162- }
163- elseif ($ chainItem instanceof FinallyChain) {
164- if ($ this ->handleFinally ($ chainItem )) {
165- $ this ->emptyChain ();
166- }
167- }
168- }
169- elseif ($ chainItem instanceof CatchChain) {
170- try {
171- if (isset ($ this ->rejectedReason )) {
172- $ chainItem ->checkRejectionCallbackType ($ this ->rejectedReason );
173- }
174- if ($ handled = $ this ->handleCatch ($ chainItem )) {
175- array_push ($ this ->handledRejections , $ handled );
176- }
177- }
178- catch (ChainFunctionTypeError ) {
179- continue ;
180- }
181- }
182- }
183-
184- $ this ->throwUnhandledRejection ();
185- }
186-
187136 private function getNextChainItem ():?Chainable {
188137 return array_shift ($ this ->chain );
189138 }
190139
191- private function handleThen (ThenChain $ then ):bool {
140+ protected function handleThen (ThenChain $ then ):bool {
192141 if ($ this ->getState () !== PromiseState::RESOLVED ) {
193142 return false ;
194143 }
@@ -204,7 +153,7 @@ private function handleThen(ThenChain $then):bool {
204153 return false ;
205154 }
206155
207- private function handleFinally (FinallyChain $ finally ):bool {
156+ protected function handleFinally (FinallyChain $ finally ):bool {
208157 if ($ this ->getState () === PromiseState::RESOLVED ) {
209158 $ result = $ finally ->callOnResolved ($ this ->resolvedValue );
210159 return $ this ->handleResolvedResult ($ result );
@@ -234,9 +183,8 @@ private function handleResolvedResult(mixed $result):bool {
234183 return false ;
235184 }
236185
237- private function handleCatch (CatchChain $ catch ):?Throwable {
186+ protected function handleCatch (CatchChain $ catch ):?Throwable {
238187 if ($ this ->getState () !== PromiseState::REJECTED ) {
239- array_push ($ this ->uncalledCatchChain , $ catch );
240188 return null ;
241189 }
242190 try {
0 commit comments