<commit_before><?php

namespace Amp\Internal;

use Amp\Failure;
use Interop\Async\Awaitable;
use Interop\Async\Loop;

/**
 * Trait used by Awaitable implementations. Do not use this trait in your code, instead compose your class from one of
 * the available classes implementing \Interop\Async\Awaitable.
 *
 * @internal
 */
trait Placeholder {
    /**
     * @var bool
     */
    private $resolved = false;

    /**
     * @var mixed
     */
    private $result;
    
    /**
     * @var callable|\Amp\Internal\WhenQueue|null
     */
    private $onResolved;
    
    /**
     * {@inheritdoc}
     */
    public function when(callable $onResolved) {
        if ($this->resolved) {
            if ($this->result instanceof Awaitable) {
                $this->result->when($onResolved);
                return;
            }

            try {
                $onResolved(null, $this->result);
            } catch (\Throwable $exception) {
                Loop::defer(static function () use ($exception) {
                    throw $exception;
                });
            } catch (\Exception $exception) {
                Loop::defer(static function () use ($exception) {
                    throw $exception;
                });
            }
            return;
        }

        if (null === $this->onResolved) {
            $this->onResolved = $onResolved;
        } elseif (!$this->onResolved instanceof WhenQueue) {
            $this->onResolved = new WhenQueue($this->onResolved);
            $this->onResolved->push($onResolved);
        } else {
            $this->onResolved->push($onResolved);
        }
    }

    /**
     * @param mixed $value
     */
    protected function resolve($value = null) {
        if ($this->resolved) {
            throw new \LogicException("Awaitable has already been resolved");
        }

        $this->resolved = true;

        if ($value instanceof Awaitable) {
            if ($this === $value) {
                throw new \InvalidArgumentException("Cannot resolve an awaitable with itself");
            }

            $this->result = $value;

            if ($this->onResolved !== null) {
                $this->result->when($this->onResolved);
            }
        } else {
            $this->result = $value;

            if ($this->onResolved !== null) {
                try {
                    $onResolved = $this->onResolved;
                    $onResolved(null, $this->result);
                } catch (\Throwable $exception) {
                    Loop::defer(static function () use ($exception) {
                        throw $exception;
                    });
                } catch (\Exception $exception) {
                    Loop::defer(static function () use ($exception) {
                        throw $exception;
                    });
                }
            }
        }

        $this->onResolved = null;
    }

    /**<commit_msg>Remove circular reference check<commit_after><?php

namespace Amp\Internal;

use Amp\Failure;
use Interop\Async\Awaitable;
use Interop\Async\Loop;

/**
 * Trait used by Awaitable implementations. Do not use this trait in your code, instead compose your class from one of
 * the available classes implementing \Interop\Async\Awaitable.
 *
 * @internal
 */
trait Placeholder {
    /**
     * @var bool
     */
    private $resolved = false;

    /**
     * @var mixed
     */
    private $result;
    
    /**
     * @var callable|\Amp\Internal\WhenQueue|null
     */
    private $onResolved;
    
    /**
     * {@inheritdoc}
     */
    public function when(callable $onResolved) {
        if ($this->resolved) {
            if ($this->result instanceof Awaitable) {
                $this->result->when($onResolved);
                return;
            }

            try {
                $onResolved(null, $this->result);
            } catch (\Throwable $exception) {
                Loop::defer(static function () use ($exception) {
                    throw $exception;
                });
            } catch (\Exception $exception) {
                Loop::defer(static function () use ($exception) {
                    throw $exception;
                });
            }
            return;
        }

        if (null === $this->onResolved) {
            $this->onResolved = $onResolved;
        } elseif (!$this->onResolved instanceof WhenQueue) {
            $this->onResolved = new WhenQueue($this->onResolved);
            $this->onResolved->push($onResolved);
        } else {
            $this->onResolved->push($onResolved);
        }
    }

    /**
     * @param mixed $value
     */
    protected function resolve($value = null) {
        if ($this->resolved) {
            throw new \LogicException("Awaitable has already been resolved");
        }

        $this->resolved = true;
        $this->result = $value;

        if ($this->onResolved !== null) {
            if ($this->result instanceof Awaitable) {
                $this->result->when($this->onResolved);
            } else {
                try {
                    $onResolved = $this->onResolved;
                    $onResolved(null, $this->result);
                } catch (\Throwable $exception) {
                    Loop::defer(static function () use ($exception) {
                        throw $exception;
                    });
                } catch (\Exception $exception) {
                    Loop::defer(static function () use ($exception) {
                        throw $exception;
                    });
                }
            }

            $this->onResolved = null;
        }
    }

    /**
{'fraction_non_alphanumeric': 0.12615658362989324, 'fraction_numerical': 0.0, 'mean_word_length': 1.4291270527225584, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2461339', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1440, 'n_words': 327}