[
  {
    "path": ".editorconfig",
    "content": "root = true\n\n[*]\ncharset = utf-8\nend_of_line = lf\nindent_size = 4\nindent_style = space\ninsert_final_newline = true\ntrim_trailing_whitespace = true\n"
  },
  {
    "path": ".gitattributes",
    "content": ".editorconfig           export-ignore\n.gitattributes          export-ignore\n/.github/               export-ignore\n.gitignore              export-ignore\n/.php-cs-fixer.dist.php export-ignore\n/phpstan-baseline.neon  export-ignore\n/phpstan.neon.dist      export-ignore\n/phpunit.xml.dist       export-ignore\n/psalm-baseline.xml     export-ignore\n/psalm.xml              export-ignore\n/tests/                 export-ignore\n/vendor-bin/            export-ignore\n/Makefile               export-ignore\n"
  },
  {
    "path": ".github/.editorconfig",
    "content": "[*.yml]\nindent_size = 2\n"
  },
  {
    "path": ".github/FUNDING.yml",
    "content": "github: [Nyholm, GrahamCampbell]\ntidelift: \"packagist/guzzlehttp/promises\"\n"
  },
  {
    "path": ".github/stale.yml",
    "content": "daysUntilStale: 120\ndaysUntilClose: 14\nexemptLabels:\n  - lifecycle/keep-open\n  - lifecycle/ready-for-merge\n# Label to use when marking an issue as stale\nstaleLabel: lifecycle/stale\n# Comment to post when marking an issue as stale. Set to `false` to disable\nmarkComment: >\n  This issue has been automatically marked as stale because it has not had\n  recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you\n  for your contributions.\n# Comment to post when closing a stale issue. Set to `false` to disable\ncloseComment: false\n"
  },
  {
    "path": ".github/workflows/checks.yml",
    "content": "name: Checks\n\non:\n  push:\n    branches:\n  pull_request:\n\npermissions:\n  contents: read\n\njobs:\n  composer-normalize:\n    name: Composer Normalize\n    runs-on: ubuntu-24.04\n\n    steps:\n      - name: Checkout code\n        uses: actions/checkout@v6\n\n      - name: Composer normalize\n        uses: docker://ergebnis/composer-normalize-action\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: CI\n\non:\n  push:\n    branches:\n  pull_request:\n\npermissions:\n  contents: read\n\njobs:\n  build-lowest-version:\n    name: Build lowest version\n    runs-on: ubuntu-24.04\n\n    steps:\n      - name: Set up PHP\n        uses: shivammathur/setup-php@v2\n        with:\n          php-version: '7.2'\n          ini-values: error_reporting=E_ALL\n          coverage: 'none'\n          extensions: mbstring\n\n      - name: Checkout code\n        uses: actions/checkout@v6\n\n      - name: Install dependencies\n        run: composer update --no-interaction --prefer-stable --prefer-lowest --no-progress\n\n      - name: Run tests\n        run: vendor/bin/phpunit \n\n  build:\n    name: Build\n    runs-on: ubuntu-24.04\n    strategy:\n      fail-fast: false\n      max-parallel: 10\n      matrix:\n        php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']\n\n    steps:\n      - name: Set up PHP\n        uses: shivammathur/setup-php@v2\n        with:\n          php-version: ${{ matrix.php }}\n          ini-values: error_reporting=E_ALL\n          coverage: 'none'\n          extensions: mbstring\n\n      - name: Checkout code\n        uses: actions/checkout@v6\n\n      - name: Install dependencies\n        run: composer update --no-interaction --no-progress\n\n      - name: Run tests\n        run: vendor/bin/phpunit \n"
  },
  {
    "path": ".github/workflows/static.yml",
    "content": "name: Static analysis\n\non:\n  push:\n    branches:\n  pull_request:\n\npermissions:\n  contents: read\n\njobs:\n  phpstan:\n    name: PHPStan\n    runs-on: ubuntu-24.04\n\n    steps:\n      - name: Checkout code\n        uses: actions/checkout@v6\n\n      - name: Setup PHP\n        uses: shivammathur/setup-php@v2\n        with:\n          php-version: '8.5'\n          coverage: none\n          extensions: mbstring\n\n      - name: Download dependencies\n        run: composer update --no-interaction --no-progress\n\n      - name: Download PHPStan\n        run: composer bin phpstan update --no-interaction --no-progress\n\n      - name: Execute PHPStan\n        run: vendor/bin/phpstan analyze --no-progress\n\n  php-cs-fixer:\n    name: PHP-CS-Fixer\n    runs-on: ubuntu-24.04\n\n    steps:\n      - name: Checkout code\n        uses: actions/checkout@v6\n\n      - name: Setup PHP\n        uses: shivammathur/setup-php@v2\n        with:\n          php-version: '7.4'\n          coverage: none\n          extensions: mbstring\n\n      - name: Download dependencies\n        run: composer update --no-interaction --no-progress\n\n      - name: Download PHP CS Fixer\n        run: composer bin php-cs-fixer update --no-interaction --no-progress\n\n      - name: Execute PHP CS Fixer\n        run: vendor/bin/php-cs-fixer fix --diff --dry-run\n"
  },
  {
    "path": ".gitignore",
    "content": "artifacts/\nvendor/\ncomposer.lock\nphpunit.xml\n.php-cs-fixer.php\n.php-cs-fixer.cache\n.phpunit.result.cache\n"
  },
  {
    "path": ".php-cs-fixer.dist.php",
    "content": "<?php\n\n$config = (new PhpCsFixer\\Config())\n    ->setRiskyAllowed(true)\n    ->setRules([\n        '@PHP71Migration:risky' => true,\n        '@PHPUnit75Migration:risky' => true,\n        '@PSR12:risky' => true,\n        '@Symfony' => true,\n        'global_namespace_import' => false,\n        'no_superfluous_phpdoc_tags' => [\n            'allow_mixed' => true,\n        ],\n        'phpdoc_annotation_without_dot' => false,\n        'phpdoc_summary' => false,\n        'phpdoc_to_comment' => false,\n        'single_line_throw' => false,\n        'yoda_style' => false,\n    ])\n    ->setFinder(\n        PhpCsFixer\\Finder::create()\n            ->in(__DIR__.'/src')\n            ->in(__DIR__.'/tests')\n            ->name('*.php')\n    )\n;\n\nreturn $config;\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# CHANGELOG\n\n\n## 2.3.0 - 2025-08-22\n\n### Added\n\n- PHP 8.5 support\n\n\n## 2.2.0 - 2025-03-27\n\n### Fixed\n\n- Revert \"Allow an empty EachPromise to be resolved by running the queue\"\n\n\n## 2.1.0 - 2025-03-27\n\n### Added\n\n- Allow an empty EachPromise to be resolved by running the queue\n\n\n## 2.0.4 - 2024-10-17\n\n### Fixed\n\n- Once settled, don't allow further rejection of additional promises\n\n\n## 2.0.3 - 2024-07-18\n\n### Changed\n\n- PHP 8.4 support\n\n\n## 2.0.2 - 2023-12-03\n\n### Changed\n\n- Replaced `call_user_func*` with native calls\n\n\n## 2.0.1 - 2023-08-03\n\n### Changed\n\n- PHP 8.3 support\n\n\n## 2.0.0 - 2023-05-21\n\n### Added\n\n- Added PHP 7 type hints\n\n### Changed\n\n- All previously non-final non-exception classes have been marked as soft-final\n\n### Removed\n\n- Dropped PHP < 7.2 support\n- All functions in the `GuzzleHttp\\Promise` namespace\n\n\n## 1.5.3 - 2023-05-21\n\n### Changed\n\n- Removed remaining usage of deprecated functions\n\n\n## 1.5.2 - 2022-08-07\n\n### Changed\n\n- Officially support PHP 8.2\n\n\n## 1.5.1 - 2021-10-22\n\n### Fixed\n\n- Revert \"Call handler when waiting on fulfilled/rejected Promise\"\n- Fix pool memory leak when empty array of promises provided\n\n\n## 1.5.0 - 2021-10-07\n\n### Changed\n\n- Call handler when waiting on fulfilled/rejected Promise\n- Officially support PHP 8.1\n\n### Fixed\n\n- Fix manually settle promises generated with `Utils::task`\n\n\n## 1.4.1 - 2021-02-18\n\n### Fixed\n\n- Fixed `each_limit` skipping promises and failing\n\n\n## 1.4.0 - 2020-09-30\n\n### Added\n\n- Support for PHP 8\n- Optional `$recursive` flag to `all`\n- Replaced functions by static methods\n\n### Fixed\n\n- Fix empty `each` processing\n- Fix promise handling for Iterators of non-unique keys\n- Fixed `method_exists` crashes on PHP 8\n- Memory leak on exceptions\n\n\n## 1.3.1 - 2016-12-20\n\n### Fixed\n\n- `wait()` foreign promise compatibility\n\n\n## 1.3.0 - 2016-11-18\n\n### Added\n\n- Adds support for custom task queues.\n\n### Fixed\n\n- Fixed coroutine promise memory leak.\n\n\n## 1.2.0 - 2016-05-18\n\n### Changed\n\n- Update to now catch `\\Throwable` on PHP 7+\n\n\n## 1.1.0 - 2016-03-07\n\n### Changed\n\n- Update EachPromise to prevent recurring on a iterator when advancing, as this\n  could trigger fatal generator errors.\n- Update Promise to allow recursive waiting without unwrapping exceptions.\n\n\n## 1.0.3 - 2015-10-15\n\n### Changed\n\n- Update EachPromise to immediately resolve when the underlying promise iterator\n  is empty. Previously, such a promise would throw an exception when its `wait`\n  function was called.\n\n\n## 1.0.2 - 2015-05-15\n\n### Changed\n\n- Conditionally require functions.php.\n\n\n## 1.0.1 - 2015-06-24\n\n### Changed\n\n- Updating EachPromise to call next on the underlying promise iterator as late\n  as possible to ensure that generators that generate new requests based on\n  callbacks are not iterated until after callbacks are invoked.\n\n\n## 1.0.0 - 2015-05-12\n\n- Initial release\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 Michael Dowling <mtdowling@gmail.com>\nCopyright (c) 2015 Graham Campbell <hello@gjcampbell.co.uk>\nCopyright (c) 2017 Tobias Schultze <webmaster@tubo-world.de>\nCopyright (c) 2020 Tobias Nyholm <tobias.nyholm@gmail.com>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "all: clean test\n\ntest:\n\tvendor/bin/phpunit\n\ncoverage:\n\tvendor/bin/phpunit --coverage-html=artifacts/coverage\n\nview-coverage:\n\topen artifacts/coverage/index.html\n\nclean:\n\trm -rf artifacts/*\n"
  },
  {
    "path": "README.md",
    "content": "# Guzzle Promises\n\n[Promises/A+](https://promisesaplus.com/) implementation that handles promise\nchaining and resolution iteratively, allowing for \"infinite\" promise chaining\nwhile keeping the stack size constant. Read [this blog post](https://blog.domenic.me/youre-missing-the-point-of-promises/)\nfor a general introduction to promises.\n\n- [Features](#features)\n- [Quick start](#quick-start)\n- [Synchronous wait](#synchronous-wait)\n- [Cancellation](#cancellation)\n- [API](#api)\n  - [Promise](#promise)\n  - [FulfilledPromise](#fulfilledpromise)\n  - [RejectedPromise](#rejectedpromise)\n- [Promise interop](#promise-interop)\n- [Implementation notes](#implementation-notes)\n\n\n## Features\n\n- [Promises/A+](https://promisesaplus.com/) implementation.\n- Promise resolution and chaining is handled iteratively, allowing for\n  \"infinite\" promise chaining.\n- Promises have a synchronous `wait` method.\n- Promises can be cancelled.\n- Works with any object that has a `then` function.\n- C# style async/await coroutine promises using\n  `GuzzleHttp\\Promise\\Coroutine::of()`.\n\n\n## Installation\n\n```shell\ncomposer require guzzlehttp/promises\n```\n\n\n## Version Guidance\n\n| Version | Status              | PHP Version  |\n|---------|---------------------|--------------|\n| 1.x     | Security fixes only | >=5.5,<8.3   |\n| 2.x     | Latest              | >=7.2.5,<8.6 |\n\n\n## Quick Start\n\nA *promise* represents the eventual result of an asynchronous operation. The\nprimary way of interacting with a promise is through its `then` method, which\nregisters callbacks to receive either a promise's eventual value or the reason\nwhy the promise cannot be fulfilled.\n\n### Callbacks\n\nCallbacks are registered with the `then` method by providing an optional \n`$onFulfilled` followed by an optional `$onRejected` function.\n\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$promise->then(\n    // $onFulfilled\n    function ($value) {\n        echo 'The promise was fulfilled.';\n    },\n    // $onRejected\n    function ($reason) {\n        echo 'The promise was rejected.';\n    }\n);\n```\n\n*Resolving* a promise means that you either fulfill a promise with a *value* or\nreject a promise with a *reason*. Resolving a promise triggers callbacks\nregistered with the promise's `then` method. These callbacks are triggered\nonly once and in the order in which they were added.\n\n### Resolving a Promise\n\nPromises are fulfilled using the `resolve($value)` method. Resolving a promise\nwith any value other than a `GuzzleHttp\\Promise\\RejectedPromise` will trigger\nall of the onFulfilled callbacks (resolving a promise with a rejected promise\nwill reject the promise and trigger the `$onRejected` callbacks).\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$promise\n    ->then(function ($value) {\n        // Return a value and don't break the chain\n        return \"Hello, \" . $value;\n    })\n    // This then is executed after the first then and receives the value\n    // returned from the first then.\n    ->then(function ($value) {\n        echo $value;\n    });\n\n// Resolving the promise triggers the $onFulfilled callbacks and outputs\n// \"Hello, reader.\"\n$promise->resolve('reader.');\n```\n\n### Promise Forwarding\n\nPromises can be chained one after the other. Each then in the chain is a new\npromise. The return value of a promise is what's forwarded to the next\npromise in the chain. Returning a promise in a `then` callback will cause the\nsubsequent promises in the chain to only be fulfilled when the returned promise\nhas been fulfilled. The next promise in the chain will be invoked with the\nresolved value of the promise.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$nextPromise = new Promise();\n\n$promise\n    ->then(function ($value) use ($nextPromise) {\n        echo $value;\n        return $nextPromise;\n    })\n    ->then(function ($value) {\n        echo $value;\n    });\n\n// Triggers the first callback and outputs \"A\"\n$promise->resolve('A');\n// Triggers the second callback and outputs \"B\"\n$nextPromise->resolve('B');\n```\n\n### Promise Rejection\n\nWhen a promise is rejected, the `$onRejected` callbacks are invoked with the\nrejection reason.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$promise->then(null, function ($reason) {\n    echo $reason;\n});\n\n$promise->reject('Error!');\n// Outputs \"Error!\"\n```\n\n### Rejection Forwarding\n\nIf an exception is thrown in an `$onRejected` callback, subsequent\n`$onRejected` callbacks are invoked with the thrown exception as the reason.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$promise->then(null, function ($reason) {\n    throw new Exception($reason);\n})->then(null, function ($reason) {\n    assert($reason->getMessage() === 'Error!');\n});\n\n$promise->reject('Error!');\n```\n\nYou can also forward a rejection down the promise chain by returning a\n`GuzzleHttp\\Promise\\RejectedPromise` in either an `$onFulfilled` or\n`$onRejected` callback.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\n\n$promise = new Promise();\n$promise->then(null, function ($reason) {\n    return new RejectedPromise($reason);\n})->then(null, function ($reason) {\n    assert($reason === 'Error!');\n});\n\n$promise->reject('Error!');\n```\n\nIf an exception is not thrown in a `$onRejected` callback and the callback\ndoes not return a rejected promise, downstream `$onFulfilled` callbacks are\ninvoked using the value returned from the `$onRejected` callback.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise();\n$promise\n    ->then(null, function ($reason) {\n        return \"It's ok\";\n    })\n    ->then(function ($value) {\n        assert($value === \"It's ok\");\n    });\n\n$promise->reject('Error!');\n```\n\n\n## Synchronous Wait\n\nYou can synchronously force promises to complete using a promise's `wait`\nmethod. When creating a promise, you can provide a wait function that is used\nto synchronously force a promise to complete. When a wait function is invoked\nit is expected to deliver a value to the promise or reject the promise. If the\nwait function does not deliver a value, then an exception is thrown. The wait\nfunction provided to a promise constructor is invoked when the `wait` function\nof the promise is called.\n\n```php\n$promise = new Promise(function () use (&$promise) {\n    $promise->resolve('foo');\n});\n\n// Calling wait will return the value of the promise.\necho $promise->wait(); // outputs \"foo\"\n```\n\nIf an exception is encountered while invoking the wait function of a promise,\nthe promise is rejected with the exception and the exception is thrown.\n\n```php\n$promise = new Promise(function () use (&$promise) {\n    throw new Exception('foo');\n});\n\n$promise->wait(); // throws the exception.\n```\n\nCalling `wait` on a promise that has been fulfilled will not trigger the wait\nfunction. It will simply return the previously resolved value.\n\n```php\n$promise = new Promise(function () { die('this is not called!'); });\n$promise->resolve('foo');\necho $promise->wait(); // outputs \"foo\"\n```\n\nCalling `wait` on a promise that has been rejected will throw an exception. If\nthe rejection reason is an instance of `\\Exception` the reason is thrown.\nOtherwise, a `GuzzleHttp\\Promise\\RejectionException` is thrown and the reason\ncan be obtained by calling the `getReason` method of the exception.\n\n```php\n$promise = new Promise();\n$promise->reject('foo');\n$promise->wait();\n```\n\n> PHP Fatal error:  Uncaught exception 'GuzzleHttp\\Promise\\RejectionException' with message 'The promise was rejected with value: foo'\n\n### Unwrapping a Promise\n\nWhen synchronously waiting on a promise, you are joining the state of the\npromise into the current state of execution (i.e., return the value of the\npromise if it was fulfilled or throw an exception if it was rejected). This is\ncalled \"unwrapping\" the promise. Waiting on a promise will by default unwrap\nthe promise state.\n\nYou can force a promise to resolve and *not* unwrap the state of the promise\nby passing `false` to the first argument of the `wait` function:\n\n```php\n$promise = new Promise();\n$promise->reject('foo');\n// This will not throw an exception. It simply ensures the promise has\n// been resolved.\n$promise->wait(false);\n```\n\nWhen unwrapping a promise, the resolved value of the promise will be waited\nupon until the unwrapped value is not a promise. This means that if you resolve\npromise A with a promise B and unwrap promise A, the value returned by the\nwait function will be the value delivered to promise B.\n\n**Note**: when you do not unwrap the promise, no value is returned.\n\n\n## Cancellation\n\nYou can cancel a promise that has not yet been fulfilled using the `cancel()`\nmethod of a promise. When creating a promise you can provide an optional\ncancel function that when invoked cancels the action of computing a resolution\nof the promise.\n\n\n## API\n\n### Promise\n\nWhen creating a promise object, you can provide an optional `$waitFn` and\n`$cancelFn`. `$waitFn` is a function that is invoked with no arguments and is\nexpected to resolve the promise. `$cancelFn` is a function with no arguments\nthat is expected to cancel the computation of a promise. It is invoked when the\n`cancel()` method of a promise is called.\n\n```php\nuse GuzzleHttp\\Promise\\Promise;\n\n$promise = new Promise(\n    function () use (&$promise) {\n        $promise->resolve('waited');\n    },\n    function () {\n        // do something that will cancel the promise computation (e.g., close\n        // a socket, cancel a database query, etc...)\n    }\n);\n\nassert('waited' === $promise->wait());\n```\n\nA promise has the following methods:\n\n- `then(callable $onFulfilled, callable $onRejected) : PromiseInterface`\n  \n  Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.\n\n- `otherwise(callable $onRejected) : PromiseInterface`\n  \n  Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.\n\n- `wait($unwrap = true) : mixed`\n\n  Synchronously waits on the promise to complete.\n  \n  `$unwrap` controls whether or not the value of the promise is returned for a\n  fulfilled promise or if an exception is thrown if the promise is rejected.\n  This is set to `true` by default.\n\n- `cancel()`\n\n  Attempts to cancel the promise if possible. The promise being cancelled and\n  the parent most ancestor that has not yet been resolved will also be\n  cancelled. Any promises waiting on the cancelled promise to resolve will also\n  be cancelled.\n\n- `getState() : string`\n\n  Returns the state of the promise. One of `pending`, `fulfilled`, or\n  `rejected`.\n\n- `resolve($value)`\n\n  Fulfills the promise with the given `$value`.\n\n- `reject($reason)`\n\n  Rejects the promise with the given `$reason`.\n\n\n### FulfilledPromise\n\nA fulfilled promise can be created to represent a promise that has been\nfulfilled.\n\n```php\nuse GuzzleHttp\\Promise\\FulfilledPromise;\n\n$promise = new FulfilledPromise('value');\n\n// Fulfilled callbacks are immediately invoked.\n$promise->then(function ($value) {\n    echo $value;\n});\n```\n\n\n### RejectedPromise\n\nA rejected promise can be created to represent a promise that has been\nrejected.\n\n```php\nuse GuzzleHttp\\Promise\\RejectedPromise;\n\n$promise = new RejectedPromise('Error');\n\n// Rejected callbacks are immediately invoked.\n$promise->then(null, function ($reason) {\n    echo $reason;\n});\n```\n\n\n## Promise Interoperability\n\nThis library works with foreign promises that have a `then` method. This means\nyou can use Guzzle promises with [React promises](https://github.com/reactphp/promise)\nfor example. When a foreign promise is returned inside of a then method\ncallback, promise resolution will occur recursively.\n\n```php\n// Create a React promise\n$deferred = new React\\Promise\\Deferred();\n$reactPromise = $deferred->promise();\n\n// Create a Guzzle promise that is fulfilled with a React promise.\n$guzzlePromise = new GuzzleHttp\\Promise\\Promise();\n$guzzlePromise->then(function ($value) use ($reactPromise) {\n    // Do something something with the value...\n    // Return the React promise\n    return $reactPromise;\n});\n```\n\nPlease note that wait and cancel chaining is no longer possible when forwarding\na foreign promise. You will need to wrap a third-party promise with a Guzzle\npromise in order to utilize wait and cancel functions with foreign promises.\n\n\n### Event Loop Integration\n\nIn order to keep the stack size constant, Guzzle promises are resolved\nasynchronously using a task queue. When waiting on promises synchronously, the\ntask queue will be automatically run to ensure that the blocking promise and\nany forwarded promises are resolved. When using promises asynchronously in an\nevent loop, you will need to run the task queue on each tick of the loop. If\nyou do not run the task queue, then promises will not be resolved.\n\nYou can run the task queue using the `run()` method of the global task queue\ninstance.\n\n```php\n// Get the global task queue\n$queue = GuzzleHttp\\Promise\\Utils::queue();\n$queue->run();\n```\n\nFor example, you could use Guzzle promises with React using a periodic timer:\n\n```php\n$loop = React\\EventLoop\\Factory::create();\n$loop->addPeriodicTimer(0, [$queue, 'run']);\n```\n\n\n## Implementation Notes\n\n### Promise Resolution and Chaining is Handled Iteratively\n\nBy shuffling pending handlers from one owner to another, promises are\nresolved iteratively, allowing for \"infinite\" then chaining.\n\n```php\n<?php\nrequire 'vendor/autoload.php';\n\nuse GuzzleHttp\\Promise\\Promise;\n\n$parent = new Promise();\n$p = $parent;\n\nfor ($i = 0; $i < 1000; $i++) {\n    $p = $p->then(function ($v) {\n        // The stack size remains constant (a good thing)\n        echo xdebug_get_stack_depth() . ', ';\n        return $v + 1;\n    });\n}\n\n$parent->resolve(0);\nvar_dump($p->wait()); // int(1000)\n\n```\n\nWhen a promise is fulfilled or rejected with a non-promise value, the promise\nthen takes ownership of the handlers of each child promise and delivers values\ndown the chain without using recursion.\n\nWhen a promise is resolved with another promise, the original promise transfers\nall of its pending handlers to the new promise. When the new promise is\neventually resolved, all of the pending handlers are delivered the forwarded\nvalue.\n\n### A Promise is the Deferred\n\nSome promise libraries implement promises using a deferred object to represent\na computation and a promise object to represent the delivery of the result of\nthe computation. This is a nice separation of computation and delivery because\nconsumers of the promise cannot modify the value that will be eventually\ndelivered.\n\nOne side effect of being able to implement promise resolution and chaining\niteratively is that you need to be able for one promise to reach into the state\nof another promise to shuffle around ownership of handlers. In order to achieve\nthis without making the handlers of a promise publicly mutable, a promise is\nalso the deferred value, allowing promises of the same parent class to reach\ninto and modify the private properties of promises of the same type. While this\ndoes allow consumers of the value to modify the resolution or rejection of the\ndeferred, it is a small price to pay for keeping the stack size constant.\n\n```php\n$promise = new Promise();\n$promise->then(function ($value) { echo $value; });\n// The promise is the deferred value, so you can deliver a value to it.\n$promise->resolve('foo');\n// prints \"foo\"\n```\n\n\n## Upgrading from Function API\n\nA static API was first introduced in 1.4.0, in order to mitigate problems with\nfunctions conflicting between global and local copies of the package. The\nfunction API was removed in 2.0.0. A migration table has been provided here for\nyour convenience:\n\n| Original Function | Replacement Method |\n|----------------|----------------|\n| `queue` | `Utils::queue` |\n| `task` | `Utils::task` |\n| `promise_for` | `Create::promiseFor` |\n| `rejection_for` | `Create::rejectionFor` |\n| `exception_for` | `Create::exceptionFor` |\n| `iter_for` | `Create::iterFor` |\n| `inspect` | `Utils::inspect` |\n| `inspect_all` | `Utils::inspectAll` |\n| `unwrap` | `Utils::unwrap` |\n| `all` | `Utils::all` |\n| `some` | `Utils::some` |\n| `any` | `Utils::any` |\n| `settle` | `Utils::settle` |\n| `each` | `Each::of` |\n| `each_limit` | `Each::ofLimit` |\n| `each_limit_all` | `Each::ofLimitAll` |\n| `!is_fulfilled` | `Is::pending` |\n| `is_fulfilled` | `Is::fulfilled` |\n| `is_rejected` | `Is::rejected` |\n| `is_settled` | `Is::settled` |\n| `coroutine` | `Coroutine::of` |\n\n\n## Security\n\nIf you discover a security vulnerability within this package, please send an email to security@tidelift.com. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced. Please see [Security Policy](https://github.com/guzzle/promises/security/policy) for more information.\n\n\n## License\n\nGuzzle is made available under the MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n\n## For Enterprise\n\nAvailable as part of the Tidelift Subscription\n\nThe maintainers of Guzzle and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-guzzlehttp-promises?utm_source=packagist-guzzlehttp-promises&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)\n"
  },
  {
    "path": "composer.json",
    "content": "{\n    \"name\": \"guzzlehttp/promises\",\n    \"description\": \"Guzzle promises library\",\n    \"keywords\": [\"promise\"],\n    \"license\": \"MIT\",\n    \"authors\": [\n        {\n            \"name\": \"Graham Campbell\",\n            \"email\": \"hello@gjcampbell.co.uk\",\n            \"homepage\": \"https://github.com/GrahamCampbell\"\n        },\n        {\n            \"name\": \"Michael Dowling\",\n            \"email\": \"mtdowling@gmail.com\",\n            \"homepage\": \"https://github.com/mtdowling\"\n        },\n        {\n            \"name\": \"Tobias Nyholm\",\n            \"email\": \"tobias.nyholm@gmail.com\",\n            \"homepage\": \"https://github.com/Nyholm\"\n        },\n        {\n            \"name\": \"Tobias Schultze\",\n            \"email\": \"webmaster@tubo-world.de\",\n            \"homepage\": \"https://github.com/Tobion\"\n        }\n    ],\n    \"require\": {\n        \"php\": \"^7.2.5 || ^8.0\"\n    },\n    \"require-dev\": {\n        \"bamarni/composer-bin-plugin\": \"^1.8.2\",\n        \"phpunit/phpunit\": \"^8.5.44 || ^9.6.25\"\n    },\n    \"autoload\": {\n        \"psr-4\": {\n            \"GuzzleHttp\\\\Promise\\\\\": \"src/\"\n        }\n    },\n    \"autoload-dev\": {\n        \"psr-4\": {\n            \"GuzzleHttp\\\\Promise\\\\Tests\\\\\": \"tests/\"\n        }\n    },\n    \"extra\": {\n        \"bamarni-bin\": {\n            \"bin-links\": true,\n            \"forward-command\": false\n        }\n    },\n    \"config\": {\n        \"allow-plugins\": {\n            \"bamarni/composer-bin-plugin\": true\n        },\n        \"preferred-install\": \"dist\",\n        \"sort-packages\": true\n    }\n}\n"
  },
  {
    "path": "phpstan-baseline.neon",
    "content": "parameters:\n\tignoreErrors:\n\t\t-\n\t\t\tmessage: '#^Dead catch \\- GuzzleHttp\\\\Promise\\\\RejectionException is never thrown in the try block\\.$#'\n\t\t\tidentifier: catch.neverThrown\n\t\t\tcount: 1\n\t\t\tpath: src/Utils.php\n\n"
  },
  {
    "path": "phpstan.neon.dist",
    "content": "includes:\n    - phpstan-baseline.neon\n\nparameters:\n    level: 5\n    paths:\n        - src\n"
  },
  {
    "path": "phpunit.xml.dist",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<phpunit\n    colors=\"true\"\n    beStrictAboutOutputDuringTests=\"true\"\n    beStrictAboutTestsThatDoNotTestAnything=\"true\"\n    bootstrap=\"vendor/autoload.php\"\n>\n    <testsuites>\n        <testsuite name=\"GuzzleHttp Promise Test Suite\">\n            <directory>tests/</directory>\n        </testsuite>\n    </testsuites>\n    <filter>\n        <whitelist>\n            <directory>src/</directory>\n            <exclude>\n                <directory suffix=\"Interface.php\">src/</directory>\n            </exclude>\n        </whitelist>\n    </filter>\n</phpunit>\n"
  },
  {
    "path": "src/AggregateException.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * Exception thrown when too many errors occur in the some() or any() methods.\n */\nclass AggregateException extends RejectionException\n{\n    public function __construct(string $msg, array $reasons)\n    {\n        parent::__construct(\n            $reasons,\n            sprintf('%s; %d rejected promises', $msg, count($reasons))\n        );\n    }\n}\n"
  },
  {
    "path": "src/CancellationException.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * Exception that is set as the reason for a promise that has been cancelled.\n */\nclass CancellationException extends RejectionException\n{\n}\n"
  },
  {
    "path": "src/Coroutine.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\nuse Generator;\nuse Throwable;\n\n/**\n * Creates a promise that is resolved using a generator that yields values or\n * promises (somewhat similar to C#'s async keyword).\n *\n * When called, the Coroutine::of method will start an instance of the generator\n * and returns a promise that is fulfilled with its final yielded value.\n *\n * Control is returned back to the generator when the yielded promise settles.\n * This can lead to less verbose code when doing lots of sequential async calls\n * with minimal processing in between.\n *\n *     use GuzzleHttp\\Promise;\n *\n *     function createPromise($value) {\n *         return new Promise\\FulfilledPromise($value);\n *     }\n *\n *     $promise = Promise\\Coroutine::of(function () {\n *         $value = (yield createPromise('a'));\n *         try {\n *             $value = (yield createPromise($value . 'b'));\n *         } catch (\\Throwable $e) {\n *             // The promise was rejected.\n *         }\n *         yield $value . 'c';\n *     });\n *\n *     // Outputs \"abc\"\n *     $promise->then(function ($v) { echo $v; });\n *\n * @param callable $generatorFn Generator function to wrap into a promise.\n *\n * @return Promise\n *\n * @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration\n */\nfinal class Coroutine implements PromiseInterface\n{\n    /**\n     * @var PromiseInterface|null\n     */\n    private $currentPromise;\n\n    /**\n     * @var Generator\n     */\n    private $generator;\n\n    /**\n     * @var Promise\n     */\n    private $result;\n\n    public function __construct(callable $generatorFn)\n    {\n        $this->generator = $generatorFn();\n        $this->result = new Promise(function (): void {\n            while (isset($this->currentPromise)) {\n                $this->currentPromise->wait();\n            }\n        });\n        try {\n            $this->nextCoroutine($this->generator->current());\n        } catch (Throwable $throwable) {\n            $this->result->reject($throwable);\n        }\n    }\n\n    /**\n     * Create a new coroutine.\n     */\n    public static function of(callable $generatorFn): self\n    {\n        return new self($generatorFn);\n    }\n\n    public function then(\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        return $this->result->then($onFulfilled, $onRejected);\n    }\n\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->result->otherwise($onRejected);\n    }\n\n    public function wait(bool $unwrap = true)\n    {\n        return $this->result->wait($unwrap);\n    }\n\n    public function getState(): string\n    {\n        return $this->result->getState();\n    }\n\n    public function resolve($value): void\n    {\n        $this->result->resolve($value);\n    }\n\n    public function reject($reason): void\n    {\n        $this->result->reject($reason);\n    }\n\n    public function cancel(): void\n    {\n        $this->currentPromise->cancel();\n        $this->result->cancel();\n    }\n\n    private function nextCoroutine($yielded): void\n    {\n        $this->currentPromise = Create::promiseFor($yielded)\n            ->then([$this, '_handleSuccess'], [$this, '_handleFailure']);\n    }\n\n    /**\n     * @internal\n     */\n    public function _handleSuccess($value): void\n    {\n        unset($this->currentPromise);\n        try {\n            $next = $this->generator->send($value);\n            if ($this->generator->valid()) {\n                $this->nextCoroutine($next);\n            } else {\n                $this->result->resolve($value);\n            }\n        } catch (Throwable $throwable) {\n            $this->result->reject($throwable);\n        }\n    }\n\n    /**\n     * @internal\n     */\n    public function _handleFailure($reason): void\n    {\n        unset($this->currentPromise);\n        try {\n            $nextYield = $this->generator->throw(Create::exceptionFor($reason));\n            // The throw was caught, so keep iterating on the coroutine\n            $this->nextCoroutine($nextYield);\n        } catch (Throwable $throwable) {\n            $this->result->reject($throwable);\n        }\n    }\n}\n"
  },
  {
    "path": "src/Create.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\nfinal class Create\n{\n    /**\n     * Creates a promise for a value if the value is not a promise.\n     *\n     * @param mixed $value Promise or value.\n     */\n    public static function promiseFor($value): PromiseInterface\n    {\n        if ($value instanceof PromiseInterface) {\n            return $value;\n        }\n\n        // Return a Guzzle promise that shadows the given promise.\n        if (is_object($value) && method_exists($value, 'then')) {\n            $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null;\n            $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null;\n            $promise = new Promise($wfn, $cfn);\n            $value->then([$promise, 'resolve'], [$promise, 'reject']);\n\n            return $promise;\n        }\n\n        return new FulfilledPromise($value);\n    }\n\n    /**\n     * Creates a rejected promise for a reason if the reason is not a promise.\n     * If the provided reason is a promise, then it is returned as-is.\n     *\n     * @param mixed $reason Promise or reason.\n     */\n    public static function rejectionFor($reason): PromiseInterface\n    {\n        if ($reason instanceof PromiseInterface) {\n            return $reason;\n        }\n\n        return new RejectedPromise($reason);\n    }\n\n    /**\n     * Create an exception for a rejected promise value.\n     *\n     * @param mixed $reason\n     */\n    public static function exceptionFor($reason): \\Throwable\n    {\n        if ($reason instanceof \\Throwable) {\n            return $reason;\n        }\n\n        return new RejectionException($reason);\n    }\n\n    /**\n     * Returns an iterator for the given value.\n     *\n     * @param mixed $value\n     */\n    public static function iterFor($value): \\Iterator\n    {\n        if ($value instanceof \\Iterator) {\n            return $value;\n        }\n\n        if (is_array($value)) {\n            return new \\ArrayIterator($value);\n        }\n\n        return new \\ArrayIterator([$value]);\n    }\n}\n"
  },
  {
    "path": "src/Each.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\nfinal class Each\n{\n    /**\n     * Given an iterator that yields promises or values, returns a promise that\n     * is fulfilled with a null value when the iterator has been consumed or\n     * the aggregate promise has been fulfilled or rejected.\n     *\n     * $onFulfilled is a function that accepts the fulfilled value, iterator\n     * index, and the aggregate promise. The callback can invoke any necessary\n     * side effects and choose to resolve or reject the aggregate if needed.\n     *\n     * $onRejected is a function that accepts the rejection reason, iterator\n     * index, and the aggregate promise. The callback can invoke any necessary\n     * side effects and choose to resolve or reject the aggregate if needed.\n     *\n     * @param mixed $iterable Iterator or array to iterate over.\n     */\n    public static function of(\n        $iterable,\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        return (new EachPromise($iterable, [\n            'fulfilled' => $onFulfilled,\n            'rejected' => $onRejected,\n        ]))->promise();\n    }\n\n    /**\n     * Like of, but only allows a certain number of outstanding promises at any\n     * given time.\n     *\n     * $concurrency may be an integer or a function that accepts the number of\n     * pending promises and returns a numeric concurrency limit value to allow\n     * for dynamic a concurrency size.\n     *\n     * @param mixed        $iterable\n     * @param int|callable $concurrency\n     */\n    public static function ofLimit(\n        $iterable,\n        $concurrency,\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        return (new EachPromise($iterable, [\n            'fulfilled' => $onFulfilled,\n            'rejected' => $onRejected,\n            'concurrency' => $concurrency,\n        ]))->promise();\n    }\n\n    /**\n     * Like limit, but ensures that no promise in the given $iterable argument\n     * is rejected. If any promise is rejected, then the aggregate promise is\n     * rejected with the encountered rejection.\n     *\n     * @param mixed        $iterable\n     * @param int|callable $concurrency\n     */\n    public static function ofLimitAll(\n        $iterable,\n        $concurrency,\n        ?callable $onFulfilled = null\n    ): PromiseInterface {\n        return self::ofLimit(\n            $iterable,\n            $concurrency,\n            $onFulfilled,\n            function ($reason, $idx, PromiseInterface $aggregate): void {\n                $aggregate->reject($reason);\n            }\n        );\n    }\n}\n"
  },
  {
    "path": "src/EachPromise.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * Represents a promise that iterates over many promises and invokes\n * side-effect functions in the process.\n *\n * @final\n */\nclass EachPromise implements PromisorInterface\n{\n    private $pending = [];\n\n    private $nextPendingIndex = 0;\n\n    /** @var \\Iterator|null */\n    private $iterable;\n\n    /** @var callable|int|null */\n    private $concurrency;\n\n    /** @var callable|null */\n    private $onFulfilled;\n\n    /** @var callable|null */\n    private $onRejected;\n\n    /** @var Promise|null */\n    private $aggregate;\n\n    /** @var bool|null */\n    private $mutex;\n\n    /**\n     * Configuration hash can include the following key value pairs:\n     *\n     * - fulfilled: (callable) Invoked when a promise fulfills. The function\n     *   is invoked with three arguments: the fulfillment value, the index\n     *   position from the iterable list of the promise, and the aggregate\n     *   promise that manages all of the promises. The aggregate promise may\n     *   be resolved from within the callback to short-circuit the promise.\n     * - rejected: (callable) Invoked when a promise is rejected. The\n     *   function is invoked with three arguments: the rejection reason, the\n     *   index position from the iterable list of the promise, and the\n     *   aggregate promise that manages all of the promises. The aggregate\n     *   promise may be resolved from within the callback to short-circuit\n     *   the promise.\n     * - concurrency: (integer) Pass this configuration option to limit the\n     *   allowed number of outstanding concurrently executing promises,\n     *   creating a capped pool of promises. There is no limit by default.\n     *\n     * @param mixed $iterable Promises or values to iterate.\n     * @param array $config   Configuration options\n     */\n    public function __construct($iterable, array $config = [])\n    {\n        $this->iterable = Create::iterFor($iterable);\n\n        if (isset($config['concurrency'])) {\n            $this->concurrency = $config['concurrency'];\n        }\n\n        if (isset($config['fulfilled'])) {\n            $this->onFulfilled = $config['fulfilled'];\n        }\n\n        if (isset($config['rejected'])) {\n            $this->onRejected = $config['rejected'];\n        }\n    }\n\n    /** @psalm-suppress InvalidNullableReturnType */\n    public function promise(): PromiseInterface\n    {\n        if ($this->aggregate) {\n            return $this->aggregate;\n        }\n\n        try {\n            $this->createPromise();\n            /** @psalm-assert Promise $this->aggregate */\n            $this->iterable->rewind();\n            $this->refillPending();\n        } catch (\\Throwable $e) {\n            $this->aggregate->reject($e);\n        }\n\n        /**\n         * @psalm-suppress NullableReturnStatement\n         */\n        return $this->aggregate;\n    }\n\n    private function createPromise(): void\n    {\n        $this->mutex = false;\n        $this->aggregate = new Promise(function (): void {\n            if ($this->checkIfFinished()) {\n                return;\n            }\n            reset($this->pending);\n            // Consume a potentially fluctuating list of promises while\n            // ensuring that indexes are maintained (precluding array_shift).\n            while ($promise = current($this->pending)) {\n                next($this->pending);\n                $promise->wait();\n                if (Is::settled($this->aggregate)) {\n                    return;\n                }\n            }\n        });\n\n        // Clear the references when the promise is resolved.\n        $clearFn = function (): void {\n            $this->iterable = $this->concurrency = $this->pending = null;\n            $this->onFulfilled = $this->onRejected = null;\n            $this->nextPendingIndex = 0;\n        };\n\n        $this->aggregate->then($clearFn, $clearFn);\n    }\n\n    private function refillPending(): void\n    {\n        if (!$this->concurrency) {\n            // Add all pending promises.\n            while ($this->addPending() && $this->advanceIterator()) {\n            }\n\n            return;\n        }\n\n        // Add only up to N pending promises.\n        $concurrency = is_callable($this->concurrency)\n            ? ($this->concurrency)(count($this->pending))\n            : $this->concurrency;\n        $concurrency = max($concurrency - count($this->pending), 0);\n        // Concurrency may be set to 0 to disallow new promises.\n        if (!$concurrency) {\n            return;\n        }\n        // Add the first pending promise.\n        $this->addPending();\n        // Note this is special handling for concurrency=1 so that we do\n        // not advance the iterator after adding the first promise. This\n        // helps work around issues with generators that might not have the\n        // next value to yield until promise callbacks are called.\n        while (--$concurrency\n            && $this->advanceIterator()\n            && $this->addPending()) {\n        }\n    }\n\n    private function addPending(): bool\n    {\n        if (!$this->iterable || !$this->iterable->valid()) {\n            return false;\n        }\n\n        $promise = Create::promiseFor($this->iterable->current());\n        $key = $this->iterable->key();\n\n        // Iterable keys may not be unique, so we use a counter to\n        // guarantee uniqueness\n        $idx = $this->nextPendingIndex++;\n\n        $this->pending[$idx] = $promise->then(\n            function ($value) use ($idx, $key): void {\n                if ($this->onFulfilled) {\n                    ($this->onFulfilled)(\n                        $value,\n                        $key,\n                        $this->aggregate\n                    );\n                }\n                $this->step($idx);\n            },\n            function ($reason) use ($idx, $key): void {\n                if ($this->onRejected) {\n                    ($this->onRejected)(\n                        $reason,\n                        $key,\n                        $this->aggregate\n                    );\n                }\n                $this->step($idx);\n            }\n        );\n\n        return true;\n    }\n\n    private function advanceIterator(): bool\n    {\n        // Place a lock on the iterator so that we ensure to not recurse,\n        // preventing fatal generator errors.\n        if ($this->mutex) {\n            return false;\n        }\n\n        $this->mutex = true;\n\n        try {\n            $this->iterable->next();\n            $this->mutex = false;\n\n            return true;\n        } catch (\\Throwable $e) {\n            $this->aggregate->reject($e);\n            $this->mutex = false;\n\n            return false;\n        }\n    }\n\n    private function step(int $idx): void\n    {\n        // If the promise was already resolved, then ignore this step.\n        if (Is::settled($this->aggregate)) {\n            return;\n        }\n\n        unset($this->pending[$idx]);\n\n        // Only refill pending promises if we are not locked, preventing the\n        // EachPromise to recursively invoke the provided iterator, which\n        // cause a fatal error: \"Cannot resume an already running generator\"\n        if ($this->advanceIterator() && !$this->checkIfFinished()) {\n            // Add more pending promises if possible.\n            $this->refillPending();\n        }\n    }\n\n    private function checkIfFinished(): bool\n    {\n        if (!$this->pending && !$this->iterable->valid()) {\n            // Resolve the promise if there's nothing left to do.\n            $this->aggregate->resolve(null);\n\n            return true;\n        }\n\n        return false;\n    }\n}\n"
  },
  {
    "path": "src/FulfilledPromise.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * A promise that has been fulfilled.\n *\n * Thenning off of this promise will invoke the onFulfilled callback\n * immediately and ignore other callbacks.\n *\n * @final\n */\nclass FulfilledPromise implements PromiseInterface\n{\n    private $value;\n\n    /**\n     * @param mixed $value\n     */\n    public function __construct($value)\n    {\n        if (is_object($value) && method_exists($value, 'then')) {\n            throw new \\InvalidArgumentException(\n                'You cannot create a FulfilledPromise with a promise.'\n            );\n        }\n\n        $this->value = $value;\n    }\n\n    public function then(\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        // Return itself if there is no onFulfilled function.\n        if (!$onFulfilled) {\n            return $this;\n        }\n\n        $queue = Utils::queue();\n        $p = new Promise([$queue, 'run']);\n        $value = $this->value;\n        $queue->add(static function () use ($p, $value, $onFulfilled): void {\n            if (Is::pending($p)) {\n                try {\n                    $p->resolve($onFulfilled($value));\n                } catch (\\Throwable $e) {\n                    $p->reject($e);\n                }\n            }\n        });\n\n        return $p;\n    }\n\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->then(null, $onRejected);\n    }\n\n    public function wait(bool $unwrap = true)\n    {\n        return $unwrap ? $this->value : null;\n    }\n\n    public function getState(): string\n    {\n        return self::FULFILLED;\n    }\n\n    public function resolve($value): void\n    {\n        if ($value !== $this->value) {\n            throw new \\LogicException('Cannot resolve a fulfilled promise');\n        }\n    }\n\n    public function reject($reason): void\n    {\n        throw new \\LogicException('Cannot reject a fulfilled promise');\n    }\n\n    public function cancel(): void\n    {\n        // pass\n    }\n}\n"
  },
  {
    "path": "src/Is.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\nfinal class Is\n{\n    /**\n     * Returns true if a promise is pending.\n     */\n    public static function pending(PromiseInterface $promise): bool\n    {\n        return $promise->getState() === PromiseInterface::PENDING;\n    }\n\n    /**\n     * Returns true if a promise is fulfilled or rejected.\n     */\n    public static function settled(PromiseInterface $promise): bool\n    {\n        return $promise->getState() !== PromiseInterface::PENDING;\n    }\n\n    /**\n     * Returns true if a promise is fulfilled.\n     */\n    public static function fulfilled(PromiseInterface $promise): bool\n    {\n        return $promise->getState() === PromiseInterface::FULFILLED;\n    }\n\n    /**\n     * Returns true if a promise is rejected.\n     */\n    public static function rejected(PromiseInterface $promise): bool\n    {\n        return $promise->getState() === PromiseInterface::REJECTED;\n    }\n}\n"
  },
  {
    "path": "src/Promise.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * Promises/A+ implementation that avoids recursion when possible.\n *\n * @see https://promisesaplus.com/\n *\n * @final\n */\nclass Promise implements PromiseInterface\n{\n    private $state = self::PENDING;\n    private $result;\n    private $cancelFn;\n    private $waitFn;\n    private $waitList;\n    private $handlers = [];\n\n    /**\n     * @param callable $waitFn   Fn that when invoked resolves the promise.\n     * @param callable $cancelFn Fn that when invoked cancels the promise.\n     */\n    public function __construct(\n        ?callable $waitFn = null,\n        ?callable $cancelFn = null\n    ) {\n        $this->waitFn = $waitFn;\n        $this->cancelFn = $cancelFn;\n    }\n\n    public function then(\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        if ($this->state === self::PENDING) {\n            $p = new Promise(null, [$this, 'cancel']);\n            $this->handlers[] = [$p, $onFulfilled, $onRejected];\n            $p->waitList = $this->waitList;\n            $p->waitList[] = $this;\n\n            return $p;\n        }\n\n        // Return a fulfilled promise and immediately invoke any callbacks.\n        if ($this->state === self::FULFILLED) {\n            $promise = Create::promiseFor($this->result);\n\n            return $onFulfilled ? $promise->then($onFulfilled) : $promise;\n        }\n\n        // It's either cancelled or rejected, so return a rejected promise\n        // and immediately invoke any callbacks.\n        $rejection = Create::rejectionFor($this->result);\n\n        return $onRejected ? $rejection->then(null, $onRejected) : $rejection;\n    }\n\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->then(null, $onRejected);\n    }\n\n    public function wait(bool $unwrap = true)\n    {\n        $this->waitIfPending();\n\n        if ($this->result instanceof PromiseInterface) {\n            return $this->result->wait($unwrap);\n        }\n        if ($unwrap) {\n            if ($this->state === self::FULFILLED) {\n                return $this->result;\n            }\n            // It's rejected so \"unwrap\" and throw an exception.\n            throw Create::exceptionFor($this->result);\n        }\n    }\n\n    public function getState(): string\n    {\n        return $this->state;\n    }\n\n    public function cancel(): void\n    {\n        if ($this->state !== self::PENDING) {\n            return;\n        }\n\n        $this->waitFn = $this->waitList = null;\n\n        if ($this->cancelFn) {\n            $fn = $this->cancelFn;\n            $this->cancelFn = null;\n            try {\n                $fn();\n            } catch (\\Throwable $e) {\n                $this->reject($e);\n            }\n        }\n\n        // Reject the promise only if it wasn't rejected in a then callback.\n        /** @psalm-suppress RedundantCondition */\n        if ($this->state === self::PENDING) {\n            $this->reject(new CancellationException('Promise has been cancelled'));\n        }\n    }\n\n    public function resolve($value): void\n    {\n        $this->settle(self::FULFILLED, $value);\n    }\n\n    public function reject($reason): void\n    {\n        $this->settle(self::REJECTED, $reason);\n    }\n\n    private function settle(string $state, $value): void\n    {\n        if ($this->state !== self::PENDING) {\n            // Ignore calls with the same resolution.\n            if ($state === $this->state && $value === $this->result) {\n                return;\n            }\n            throw $this->state === $state\n                ? new \\LogicException(\"The promise is already {$state}.\")\n                : new \\LogicException(\"Cannot change a {$this->state} promise to {$state}\");\n        }\n\n        if ($value === $this) {\n            throw new \\LogicException('Cannot fulfill or reject a promise with itself');\n        }\n\n        // Clear out the state of the promise but stash the handlers.\n        $this->state = $state;\n        $this->result = $value;\n        $handlers = $this->handlers;\n        $this->handlers = null;\n        $this->waitList = $this->waitFn = null;\n        $this->cancelFn = null;\n\n        if (!$handlers) {\n            return;\n        }\n\n        // If the value was not a settled promise or a thenable, then resolve\n        // it in the task queue using the correct ID.\n        if (!is_object($value) || !method_exists($value, 'then')) {\n            $id = $state === self::FULFILLED ? 1 : 2;\n            // It's a success, so resolve the handlers in the queue.\n            Utils::queue()->add(static function () use ($id, $value, $handlers): void {\n                foreach ($handlers as $handler) {\n                    self::callHandler($id, $value, $handler);\n                }\n            });\n        } elseif ($value instanceof Promise && Is::pending($value)) {\n            // We can just merge our handlers onto the next promise.\n            $value->handlers = array_merge($value->handlers, $handlers);\n        } else {\n            // Resolve the handlers when the forwarded promise is resolved.\n            $value->then(\n                static function ($value) use ($handlers): void {\n                    foreach ($handlers as $handler) {\n                        self::callHandler(1, $value, $handler);\n                    }\n                },\n                static function ($reason) use ($handlers): void {\n                    foreach ($handlers as $handler) {\n                        self::callHandler(2, $reason, $handler);\n                    }\n                }\n            );\n        }\n    }\n\n    /**\n     * Call a stack of handlers using a specific callback index and value.\n     *\n     * @param int   $index   1 (resolve) or 2 (reject).\n     * @param mixed $value   Value to pass to the callback.\n     * @param array $handler Array of handler data (promise and callbacks).\n     */\n    private static function callHandler(int $index, $value, array $handler): void\n    {\n        /** @var PromiseInterface $promise */\n        $promise = $handler[0];\n\n        // The promise may have been cancelled or resolved before placing\n        // this thunk in the queue.\n        if (Is::settled($promise)) {\n            return;\n        }\n\n        try {\n            if (isset($handler[$index])) {\n                /*\n                 * If $f throws an exception, then $handler will be in the exception\n                 * stack trace. Since $handler contains a reference to the callable\n                 * itself we get a circular reference. We clear the $handler\n                 * here to avoid that memory leak.\n                 */\n                $f = $handler[$index];\n                unset($handler);\n                $promise->resolve($f($value));\n            } elseif ($index === 1) {\n                // Forward resolution values as-is.\n                $promise->resolve($value);\n            } else {\n                // Forward rejections down the chain.\n                $promise->reject($value);\n            }\n        } catch (\\Throwable $reason) {\n            $promise->reject($reason);\n        }\n    }\n\n    private function waitIfPending(): void\n    {\n        if ($this->state !== self::PENDING) {\n            return;\n        } elseif ($this->waitFn) {\n            $this->invokeWaitFn();\n        } elseif ($this->waitList) {\n            $this->invokeWaitList();\n        } else {\n            // If there's no wait function, then reject the promise.\n            $this->reject('Cannot wait on a promise that has '\n                .'no internal wait function. You must provide a wait '\n                .'function when constructing the promise to be able to '\n                .'wait on a promise.');\n        }\n\n        Utils::queue()->run();\n\n        /** @psalm-suppress RedundantCondition */\n        if ($this->state === self::PENDING) {\n            $this->reject('Invoking the wait callback did not resolve the promise');\n        }\n    }\n\n    private function invokeWaitFn(): void\n    {\n        try {\n            $wfn = $this->waitFn;\n            $this->waitFn = null;\n            $wfn(true);\n        } catch (\\Throwable $reason) {\n            if ($this->state === self::PENDING) {\n                // The promise has not been resolved yet, so reject the promise\n                // with the exception.\n                $this->reject($reason);\n            } else {\n                // The promise was already resolved, so there's a problem in\n                // the application.\n                throw $reason;\n            }\n        }\n    }\n\n    private function invokeWaitList(): void\n    {\n        $waitList = $this->waitList;\n        $this->waitList = null;\n\n        foreach ($waitList as $result) {\n            do {\n                $result->waitIfPending();\n                $result = $result->result;\n            } while ($result instanceof Promise);\n\n            if ($result instanceof PromiseInterface) {\n                $result->wait(false);\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "src/PromiseInterface.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * A promise represents the eventual result of an asynchronous operation.\n *\n * The primary way of interacting with a promise is through its then method,\n * which registers callbacks to receive either a promise’s eventual value or\n * the reason why the promise cannot be fulfilled.\n *\n * @see https://promisesaplus.com/\n */\ninterface PromiseInterface\n{\n    public const PENDING = 'pending';\n    public const FULFILLED = 'fulfilled';\n    public const REJECTED = 'rejected';\n\n    /**\n     * Appends fulfillment and rejection handlers to the promise, and returns\n     * a new promise resolving to the return value of the called handler.\n     *\n     * @param callable $onFulfilled Invoked when the promise fulfills.\n     * @param callable $onRejected  Invoked when the promise is rejected.\n     */\n    public function then(\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface;\n\n    /**\n     * Appends a rejection handler callback to the promise, and returns a new\n     * promise resolving to the return value of the callback if it is called,\n     * or to its original fulfillment value if the promise is instead\n     * fulfilled.\n     *\n     * @param callable $onRejected Invoked when the promise is rejected.\n     */\n    public function otherwise(callable $onRejected): PromiseInterface;\n\n    /**\n     * Get the state of the promise (\"pending\", \"rejected\", or \"fulfilled\").\n     *\n     * The three states can be checked against the constants defined on\n     * PromiseInterface: PENDING, FULFILLED, and REJECTED.\n     */\n    public function getState(): string;\n\n    /**\n     * Resolve the promise with the given value.\n     *\n     * @param mixed $value\n     *\n     * @throws \\RuntimeException if the promise is already resolved.\n     */\n    public function resolve($value): void;\n\n    /**\n     * Reject the promise with the given reason.\n     *\n     * @param mixed $reason\n     *\n     * @throws \\RuntimeException if the promise is already resolved.\n     */\n    public function reject($reason): void;\n\n    /**\n     * Cancels the promise if possible.\n     *\n     * @see https://github.com/promises-aplus/cancellation-spec/issues/7\n     */\n    public function cancel(): void;\n\n    /**\n     * Waits until the promise completes if possible.\n     *\n     * Pass $unwrap as true to unwrap the result of the promise, either\n     * returning the resolved value or throwing the rejected exception.\n     *\n     * If the promise cannot be waited on, then the promise will be rejected.\n     *\n     * @return mixed\n     *\n     * @throws \\LogicException if the promise has no wait function or if the\n     *                         promise does not settle after waiting.\n     */\n    public function wait(bool $unwrap = true);\n}\n"
  },
  {
    "path": "src/PromisorInterface.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * Interface used with classes that return a promise.\n */\ninterface PromisorInterface\n{\n    /**\n     * Returns a promise.\n     */\n    public function promise(): PromiseInterface;\n}\n"
  },
  {
    "path": "src/RejectedPromise.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * A promise that has been rejected.\n *\n * Thenning off of this promise will invoke the onRejected callback\n * immediately and ignore other callbacks.\n *\n * @final\n */\nclass RejectedPromise implements PromiseInterface\n{\n    private $reason;\n\n    /**\n     * @param mixed $reason\n     */\n    public function __construct($reason)\n    {\n        if (is_object($reason) && method_exists($reason, 'then')) {\n            throw new \\InvalidArgumentException(\n                'You cannot create a RejectedPromise with a promise.'\n            );\n        }\n\n        $this->reason = $reason;\n    }\n\n    public function then(\n        ?callable $onFulfilled = null,\n        ?callable $onRejected = null\n    ): PromiseInterface {\n        // If there's no onRejected callback then just return self.\n        if (!$onRejected) {\n            return $this;\n        }\n\n        $queue = Utils::queue();\n        $reason = $this->reason;\n        $p = new Promise([$queue, 'run']);\n        $queue->add(static function () use ($p, $reason, $onRejected): void {\n            if (Is::pending($p)) {\n                try {\n                    // Return a resolved promise if onRejected does not throw.\n                    $p->resolve($onRejected($reason));\n                } catch (\\Throwable $e) {\n                    // onRejected threw, so return a rejected promise.\n                    $p->reject($e);\n                }\n            }\n        });\n\n        return $p;\n    }\n\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->then(null, $onRejected);\n    }\n\n    public function wait(bool $unwrap = true)\n    {\n        if ($unwrap) {\n            throw Create::exceptionFor($this->reason);\n        }\n\n        return null;\n    }\n\n    public function getState(): string\n    {\n        return self::REJECTED;\n    }\n\n    public function resolve($value): void\n    {\n        throw new \\LogicException('Cannot resolve a rejected promise');\n    }\n\n    public function reject($reason): void\n    {\n        if ($reason !== $this->reason) {\n            throw new \\LogicException('Cannot reject a rejected promise');\n        }\n    }\n\n    public function cancel(): void\n    {\n        // pass\n    }\n}\n"
  },
  {
    "path": "src/RejectionException.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * A special exception that is thrown when waiting on a rejected promise.\n *\n * The reason value is available via the getReason() method.\n */\nclass RejectionException extends \\RuntimeException\n{\n    /** @var mixed Rejection reason. */\n    private $reason;\n\n    /**\n     * @param mixed       $reason      Rejection reason.\n     * @param string|null $description Optional description.\n     */\n    public function __construct($reason, ?string $description = null)\n    {\n        $this->reason = $reason;\n\n        $message = 'The promise was rejected';\n\n        if ($description) {\n            $message .= ' with reason: '.$description;\n        } elseif (is_string($reason)\n            || (is_object($reason) && method_exists($reason, '__toString'))\n        ) {\n            $message .= ' with reason: '.$this->reason;\n        } elseif ($reason instanceof \\JsonSerializable) {\n            $message .= ' with reason: '.json_encode($this->reason, JSON_PRETTY_PRINT);\n        }\n\n        parent::__construct($message);\n    }\n\n    /**\n     * Returns the rejection reason.\n     *\n     * @return mixed\n     */\n    public function getReason()\n    {\n        return $this->reason;\n    }\n}\n"
  },
  {
    "path": "src/TaskQueue.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\n/**\n * A task queue that executes tasks in a FIFO order.\n *\n * This task queue class is used to settle promises asynchronously and\n * maintains a constant stack size. You can use the task queue asynchronously\n * by calling the `run()` function of the global task queue in an event loop.\n *\n *     GuzzleHttp\\Promise\\Utils::queue()->run();\n *\n * @final\n */\nclass TaskQueue implements TaskQueueInterface\n{\n    private $enableShutdown = true;\n    private $queue = [];\n\n    public function __construct(bool $withShutdown = true)\n    {\n        if ($withShutdown) {\n            register_shutdown_function(function (): void {\n                if ($this->enableShutdown) {\n                    // Only run the tasks if an E_ERROR didn't occur.\n                    $err = error_get_last();\n                    if (!$err || ($err['type'] ^ E_ERROR)) {\n                        $this->run();\n                    }\n                }\n            });\n        }\n    }\n\n    public function isEmpty(): bool\n    {\n        return !$this->queue;\n    }\n\n    public function add(callable $task): void\n    {\n        $this->queue[] = $task;\n    }\n\n    public function run(): void\n    {\n        while ($task = array_shift($this->queue)) {\n            /** @var callable $task */\n            $task();\n        }\n    }\n\n    /**\n     * The task queue will be run and exhausted by default when the process\n     * exits IFF the exit is not the result of a PHP E_ERROR error.\n     *\n     * You can disable running the automatic shutdown of the queue by calling\n     * this function. If you disable the task queue shutdown process, then you\n     * MUST either run the task queue (as a result of running your event loop\n     * or manually using the run() method) or wait on each outstanding promise.\n     *\n     * Note: This shutdown will occur before any destructors are triggered.\n     */\n    public function disableShutdown(): void\n    {\n        $this->enableShutdown = false;\n    }\n}\n"
  },
  {
    "path": "src/TaskQueueInterface.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\ninterface TaskQueueInterface\n{\n    /**\n     * Returns true if the queue is empty.\n     */\n    public function isEmpty(): bool;\n\n    /**\n     * Adds a task to the queue that will be executed the next time run is\n     * called.\n     */\n    public function add(callable $task): void;\n\n    /**\n     * Execute all of the pending task in the queue.\n     */\n    public function run(): void;\n}\n"
  },
  {
    "path": "src/Utils.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise;\n\nfinal class Utils\n{\n    /**\n     * Get the global task queue used for promise resolution.\n     *\n     * This task queue MUST be run in an event loop in order for promises to be\n     * settled asynchronously. It will be automatically run when synchronously\n     * waiting on a promise.\n     *\n     * <code>\n     * while ($eventLoop->isRunning()) {\n     *     GuzzleHttp\\Promise\\Utils::queue()->run();\n     * }\n     * </code>\n     *\n     * @param TaskQueueInterface|null $assign Optionally specify a new queue instance.\n     */\n    public static function queue(?TaskQueueInterface $assign = null): TaskQueueInterface\n    {\n        static $queue;\n\n        if ($assign) {\n            $queue = $assign;\n        } elseif (!$queue) {\n            $queue = new TaskQueue();\n        }\n\n        return $queue;\n    }\n\n    /**\n     * Adds a function to run in the task queue when it is next `run()` and\n     * returns a promise that is fulfilled or rejected with the result.\n     *\n     * @param callable $task Task function to run.\n     */\n    public static function task(callable $task): PromiseInterface\n    {\n        $queue = self::queue();\n        $promise = new Promise([$queue, 'run']);\n        $queue->add(function () use ($task, $promise): void {\n            try {\n                if (Is::pending($promise)) {\n                    $promise->resolve($task());\n                }\n            } catch (\\Throwable $e) {\n                $promise->reject($e);\n            }\n        });\n\n        return $promise;\n    }\n\n    /**\n     * Synchronously waits on a promise to resolve and returns an inspection\n     * state array.\n     *\n     * Returns a state associative array containing a \"state\" key mapping to a\n     * valid promise state. If the state of the promise is \"fulfilled\", the\n     * array will contain a \"value\" key mapping to the fulfilled value of the\n     * promise. If the promise is rejected, the array will contain a \"reason\"\n     * key mapping to the rejection reason of the promise.\n     *\n     * @param PromiseInterface $promise Promise or value.\n     */\n    public static function inspect(PromiseInterface $promise): array\n    {\n        try {\n            return [\n                'state' => PromiseInterface::FULFILLED,\n                'value' => $promise->wait(),\n            ];\n        } catch (RejectionException $e) {\n            return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];\n        } catch (\\Throwable $e) {\n            return ['state' => PromiseInterface::REJECTED, 'reason' => $e];\n        }\n    }\n\n    /**\n     * Waits on all of the provided promises, but does not unwrap rejected\n     * promises as thrown exception.\n     *\n     * Returns an array of inspection state arrays.\n     *\n     * @see inspect for the inspection state array format.\n     *\n     * @param PromiseInterface[] $promises Traversable of promises to wait upon.\n     */\n    public static function inspectAll($promises): array\n    {\n        $results = [];\n        foreach ($promises as $key => $promise) {\n            $results[$key] = self::inspect($promise);\n        }\n\n        return $results;\n    }\n\n    /**\n     * Waits on all of the provided promises and returns the fulfilled values.\n     *\n     * Returns an array that contains the value of each promise (in the same\n     * order the promises were provided). An exception is thrown if any of the\n     * promises are rejected.\n     *\n     * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.\n     *\n     * @throws \\Throwable on error\n     */\n    public static function unwrap($promises): array\n    {\n        $results = [];\n        foreach ($promises as $key => $promise) {\n            $results[$key] = $promise->wait();\n        }\n\n        return $results;\n    }\n\n    /**\n     * Given an array of promises, return a promise that is fulfilled when all\n     * the items in the array are fulfilled.\n     *\n     * The promise's fulfillment value is an array with fulfillment values at\n     * respective positions to the original array. If any promise in the array\n     * rejects, the returned promise is rejected with the rejection reason.\n     *\n     * @param mixed $promises  Promises or values.\n     * @param bool  $recursive If true, resolves new promises that might have been added to the stack during its own resolution.\n     */\n    public static function all($promises, bool $recursive = false): PromiseInterface\n    {\n        $results = [];\n        $promise = Each::of(\n            $promises,\n            function ($value, $idx) use (&$results): void {\n                $results[$idx] = $value;\n            },\n            function ($reason, $idx, Promise $aggregate): void {\n                if (Is::pending($aggregate)) {\n                    $aggregate->reject($reason);\n                }\n            }\n        )->then(function () use (&$results) {\n            ksort($results);\n\n            return $results;\n        });\n\n        if (true === $recursive) {\n            $promise = $promise->then(function ($results) use ($recursive, &$promises) {\n                foreach ($promises as $promise) {\n                    if (Is::pending($promise)) {\n                        return self::all($promises, $recursive);\n                    }\n                }\n\n                return $results;\n            });\n        }\n\n        return $promise;\n    }\n\n    /**\n     * Initiate a competitive race between multiple promises or values (values\n     * will become immediately fulfilled promises).\n     *\n     * When count amount of promises have been fulfilled, the returned promise\n     * is fulfilled with an array that contains the fulfillment values of the\n     * winners in order of resolution.\n     *\n     * This promise is rejected with a {@see AggregateException} if the number\n     * of fulfilled promises is less than the desired $count.\n     *\n     * @param int   $count    Total number of promises.\n     * @param mixed $promises Promises or values.\n     */\n    public static function some(int $count, $promises): PromiseInterface\n    {\n        $results = [];\n        $rejections = [];\n\n        return Each::of(\n            $promises,\n            function ($value, $idx, PromiseInterface $p) use (&$results, $count): void {\n                if (Is::settled($p)) {\n                    return;\n                }\n                $results[$idx] = $value;\n                if (count($results) >= $count) {\n                    $p->resolve(null);\n                }\n            },\n            function ($reason) use (&$rejections): void {\n                $rejections[] = $reason;\n            }\n        )->then(\n            function () use (&$results, &$rejections, $count) {\n                if (count($results) !== $count) {\n                    throw new AggregateException(\n                        'Not enough promises to fulfill count',\n                        $rejections\n                    );\n                }\n                ksort($results);\n\n                return array_values($results);\n            }\n        );\n    }\n\n    /**\n     * Like some(), with 1 as count. However, if the promise fulfills, the\n     * fulfillment value is not an array of 1 but the value directly.\n     *\n     * @param mixed $promises Promises or values.\n     */\n    public static function any($promises): PromiseInterface\n    {\n        return self::some(1, $promises)->then(function ($values) {\n            return $values[0];\n        });\n    }\n\n    /**\n     * Returns a promise that is fulfilled when all of the provided promises have\n     * been fulfilled or rejected.\n     *\n     * The returned promise is fulfilled with an array of inspection state arrays.\n     *\n     * @see inspect for the inspection state array format.\n     *\n     * @param mixed $promises Promises or values.\n     */\n    public static function settle($promises): PromiseInterface\n    {\n        $results = [];\n\n        return Each::of(\n            $promises,\n            function ($value, $idx) use (&$results): void {\n                $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];\n            },\n            function ($reason, $idx) use (&$results): void {\n                $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];\n            }\n        )->then(function () use (&$results) {\n            ksort($results);\n\n            return $results;\n        });\n    }\n}\n"
  },
  {
    "path": "tests/AggregateExceptionTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\AggregateException;\nuse PHPUnit\\Framework\\TestCase;\n\nclass AggregateExceptionTest extends TestCase\n{\n    public function testHasReason(): void\n    {\n        $e = new AggregateException('foo', ['baz', 'bar']);\n        $this->assertStringContainsString('foo', $e->getMessage());\n        $this->assertSame(['baz', 'bar'], $e->getReason());\n    }\n}\n"
  },
  {
    "path": "tests/CoroutineTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\Coroutine;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\PromiseInterface;\nuse PHPUnit\\Framework\\TestCase;\nuse ReflectionClass;\n\nclass CoroutineTest extends TestCase\n{\n    public function testReturnsCoroutine(): void\n    {\n        $fn = function () { yield 'foo'; };\n        $this->assertInstanceOf(Coroutine::class, Coroutine::of($fn));\n    }\n\n    /**\n     * @dataProvider promiseInterfaceMethodProvider\n     *\n     * @param string $method\n     * @param array  $args\n     */\n    public function testShouldProxyPromiseMethodsToResultPromise($method, $args = []): void\n    {\n        $coroutine = new Coroutine(function () { yield 0; });\n        $mockPromise = $this->getMockForAbstractClass(PromiseInterface::class);\n        $mockPromise->expects($this->once())->method($method)->with(...$args);\n\n        $resultPromiseProp = (new ReflectionClass(Coroutine::class))->getProperty('result');\n\n        if (PHP_VERSION_ID < 80100) {\n            $resultPromiseProp->setAccessible(true);\n        }\n\n        $resultPromiseProp->setValue($coroutine, $mockPromise);\n\n        $coroutine->{$method}(...$args);\n    }\n\n    public static function promiseInterfaceMethodProvider()\n    {\n        return [\n            ['then', [null, null]],\n            ['otherwise', [function (): void {}]],\n            ['wait', [true]],\n            ['getState', []],\n            ['resolve', [null]],\n            ['reject', [null]],\n        ];\n    }\n\n    public function testShouldCancelResultPromiseAndOutsideCurrentPromise(): void\n    {\n        $coroutine = new Coroutine(function () { yield 0; });\n\n        $mockPromises = [\n            'result' => $this->getMockForAbstractClass(PromiseInterface::class),\n            'currentPromise' => $this->getMockForAbstractClass(PromiseInterface::class),\n        ];\n        foreach ($mockPromises as $propName => $mockPromise) {\n            /**\n             * @var \\PHPUnit_Framework_MockObject_MockObject $mockPromise\n             */\n            $mockPromise->expects($this->once())\n                ->method('cancel')\n                ->with();\n\n            $promiseProp = (new ReflectionClass(Coroutine::class))->getProperty($propName);\n\n            if (PHP_VERSION_ID < 80100) {\n                $promiseProp->setAccessible(true);\n            }\n\n            $promiseProp->setValue($coroutine, $mockPromise);\n        }\n\n        $coroutine->cancel();\n    }\n\n    public function testWaitShouldResolveChainedCoroutines(): void\n    {\n        $promisor = function () {\n            return Coroutine::of(function () {\n                yield $promise = new Promise(function () use (&$promise): void {\n                    $promise->resolve(1);\n                });\n            });\n        };\n\n        $promise = $promisor()->then($promisor)->then($promisor);\n\n        $this->assertSame(1, $promise->wait());\n    }\n\n    public function testWaitShouldHandleIntermediateErrors(): void\n    {\n        $promise = Coroutine::of(function () {\n            yield $promise = new Promise(function () use (&$promise): void {\n                $promise->resolve(1);\n            });\n        })\n        ->then(function () {\n            return Coroutine::of(function () {\n                yield $promise = new Promise(function () use (&$promise): void {\n                    $promise->reject(new \\Exception());\n                });\n            });\n        })\n        ->otherwise(function (?\\Exception $error = null) {\n            if (!$error) {\n                self::fail('Error did not propagate.');\n            }\n\n            return 3;\n        });\n\n        $this->assertSame(3, $promise->wait());\n    }\n}\n"
  },
  {
    "path": "tests/CreateTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\PromiseInterface;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse PHPUnit\\Framework\\TestCase;\n\nclass CreateTest extends TestCase\n{\n    public function testCreatesPromiseForValue(): void\n    {\n        $p = P\\Create::promiseFor('foo');\n        $this->assertInstanceOf(FulfilledPromise::class, $p);\n    }\n\n    public function testReturnsPromiseForPromise(): void\n    {\n        $p = new Promise();\n        $this->assertSame($p, P\\Create::promiseFor($p));\n    }\n\n    public function testReturnsPromiseForThennable(): void\n    {\n        $p = new Thennable();\n        $wrapped = P\\Create::promiseFor($p);\n        $this->assertNotSame($p, $wrapped);\n        $this->assertInstanceOf(PromiseInterface::class, $wrapped);\n        $p->resolve('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $wrapped->wait());\n    }\n\n    public function testReturnsRejection(): void\n    {\n        $p = P\\Create::rejectionFor('fail');\n        $this->assertInstanceOf(RejectedPromise::class, $p);\n        $this->assertSame('fail', PropertyHelper::get($p, 'reason'));\n    }\n\n    public function testReturnsPromisesAsIsInRejectionFor(): void\n    {\n        $a = new Promise();\n        $b = P\\Create::rejectionFor($a);\n        $this->assertSame($a, $b);\n    }\n\n    public function testIterForReturnsIterator(): void\n    {\n        $iter = new \\ArrayIterator();\n        $this->assertSame($iter, P\\Create::iterFor($iter));\n    }\n}\n"
  },
  {
    "path": "tests/EachPromiseTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\EachPromise;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse PHPUnit\\Framework\\TestCase;\n\n/**\n * @covers \\GuzzleHttp\\Promise\\EachPromise\n */\nclass EachPromiseTest extends TestCase\n{\n    public function testReturnsSameInstance(): void\n    {\n        $each = new EachPromise([], ['concurrency' => 100]);\n        $this->assertSame($each->promise(), $each->promise());\n    }\n\n    public function testResolvesInCaseOfAnEmptyList(): void\n    {\n        $promises = [];\n        $each = new EachPromise($promises);\n        $p = $each->promise();\n        $this->assertNull($p->wait());\n        $this->assertTrue(P\\Is::fulfilled($p));\n    }\n\n    public function testResolvesInCaseOfAnEmptyListAndInvokesFulfilled(): void\n    {\n        $promises = [];\n        $each = new EachPromise($promises);\n        $p = $each->promise();\n        $onFulfilledCalled = false;\n        $onRejectedCalled = false;\n        $p->then(\n            function () use (&$onFulfilledCalled): void {\n                $onFulfilledCalled = true;\n            },\n            function () use (&$onRejectedCalled): void {\n                $onRejectedCalled = true;\n            }\n        );\n        $this->assertNull($p->wait());\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertTrue($onFulfilledCalled);\n        $this->assertFalse($onRejectedCalled);\n    }\n\n    public function testInvokesAllPromises(): void\n    {\n        $promises = [new Promise(), new Promise(), new Promise()];\n        $called = [];\n        $each = new EachPromise($promises, [\n            'fulfilled' => function ($value) use (&$called): void {\n                $called[] = $value;\n            },\n        ]);\n        $p = $each->promise();\n        $promises[0]->resolve('a');\n        $promises[1]->resolve('c');\n        $promises[2]->resolve('b');\n        P\\Utils::queue()->run();\n        $this->assertSame(['a', 'c', 'b'], $called);\n        $this->assertTrue(P\\Is::fulfilled($p));\n    }\n\n    public function testIsWaitable(): void\n    {\n        $a = $this->createSelfResolvingPromise('a');\n        $b = $this->createSelfResolvingPromise('b');\n        $called = [];\n        $each = new EachPromise([$a, $b], [\n            'fulfilled' => function ($value) use (&$called): void { $called[] = $value; },\n        ]);\n        $p = $each->promise();\n        $this->assertNull($p->wait());\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertSame(['a', 'b'], $called);\n    }\n\n    public function testCanResolveBeforeConsumingAll(): void\n    {\n        $called = 0;\n        $a = $this->createSelfResolvingPromise('a');\n        $b = new Promise(function (): void { $this->fail(); });\n        $each = new EachPromise([$a, $b], [\n            'fulfilled' => function ($value, $idx, Promise $aggregate) use (&$called): void {\n                $this->assertSame($idx, 0);\n                $this->assertSame('a', $value);\n                $aggregate->resolve(null);\n                ++$called;\n            },\n            'rejected' => function (\\Exception $reason): void {\n                $this->fail($reason->getMessage());\n            },\n        ]);\n        $p = $each->promise();\n        $p->wait();\n        $this->assertNull($p->wait());\n        $this->assertSame(1, $called);\n        $this->assertTrue(P\\Is::fulfilled($a));\n        $this->assertTrue(P\\Is::pending($b));\n        // Resolving $b has no effect on the aggregate promise.\n        $b->resolve('foo');\n        $this->assertSame(1, $called);\n    }\n\n    public function testLimitsPendingPromises(): void\n    {\n        $pending = [new Promise(), new Promise(), new Promise(), new Promise()];\n        $promises = new \\ArrayIterator($pending);\n        $each = new EachPromise($promises, ['concurrency' => 2]);\n        $p = $each->promise();\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        $pending[0]->resolve('a');\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        $this->assertTrue($promises->valid());\n        $pending[1]->resolve('b');\n        P\\Utils::queue()->run();\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        $this->assertTrue($promises->valid());\n        $promises[2]->resolve('c');\n        P\\Utils::queue()->run();\n        $this->assertCount(1, PropertyHelper::get($each, 'pending'));\n        $this->assertTrue(P\\Is::pending($p));\n        $promises[3]->resolve('d');\n        P\\Utils::queue()->run();\n        $this->assertNull(PropertyHelper::get($each, 'pending'));\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertFalse($promises->valid());\n    }\n\n    public function testDynamicallyLimitsPendingPromises(): void\n    {\n        $calls = [];\n        $pendingFn = function ($count) use (&$calls) {\n            $calls[] = $count;\n\n            return 2;\n        };\n        $pending = [new Promise(), new Promise(), new Promise(), new Promise()];\n        $promises = new \\ArrayIterator($pending);\n        $each = new EachPromise($promises, ['concurrency' => $pendingFn]);\n        $p = $each->promise();\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        $pending[0]->resolve('a');\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        $this->assertTrue($promises->valid());\n        $pending[1]->resolve('b');\n        $this->assertCount(2, PropertyHelper::get($each, 'pending'));\n        P\\Utils::queue()->run();\n        $this->assertTrue($promises->valid());\n        $promises[2]->resolve('c');\n        P\\Utils::queue()->run();\n        $this->assertCount(1, PropertyHelper::get($each, 'pending'));\n        $this->assertTrue(P\\Is::pending($p));\n        $promises[3]->resolve('d');\n        P\\Utils::queue()->run();\n        $this->assertNull(PropertyHelper::get($each, 'pending'));\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertSame([0, 1, 1, 1], $calls);\n        $this->assertFalse($promises->valid());\n    }\n\n    public function testClearsReferencesWhenResolved(): void\n    {\n        $called = false;\n        $a = new Promise(function () use (&$a, &$called): void {\n            $a->resolve('a');\n            $called = true;\n        });\n        $each = new EachPromise([$a], [\n            'concurrency' => function () { return 1; },\n            'fulfilled' => function (): void {},\n            'rejected' => function (): void {},\n        ]);\n        $each->promise()->wait();\n        $this->assertNull(PropertyHelper::get($each, 'onFulfilled'));\n        $this->assertNull(PropertyHelper::get($each, 'onRejected'));\n        $this->assertNull(PropertyHelper::get($each, 'iterable'));\n        $this->assertNull(PropertyHelper::get($each, 'pending'));\n        $this->assertNull(PropertyHelper::get($each, 'concurrency'));\n        $this->assertTrue($called);\n    }\n\n    public function testCanBeCancelled(): void\n    {\n        $called = false;\n        $a = new FulfilledPromise('a');\n        $b = new Promise(function () use (&$called): void { $called = true; });\n        $each = new EachPromise([$a, $b], [\n            'fulfilled' => function ($value, $idx, Promise $aggregate): void {\n                $aggregate->cancel();\n            },\n            'rejected' => function ($reason) use (&$called): void {\n                $called = true;\n            },\n        ]);\n        $p = $each->promise();\n        $p->wait(false);\n        $this->assertTrue(P\\Is::fulfilled($a));\n        $this->assertTrue(P\\Is::pending($b));\n        $this->assertTrue(P\\Is::rejected($p));\n        $this->assertFalse($called);\n    }\n\n    public function testDoesNotBlowStackWithFulfilledPromises(): void\n    {\n        $pending = [];\n        for ($i = 0; $i < 100; ++$i) {\n            $pending[] = new FulfilledPromise($i);\n        }\n        $values = [];\n        $each = new EachPromise($pending, [\n            'fulfilled' => function ($value) use (&$values): void {\n                $values[] = $value;\n            },\n        ]);\n        $called = false;\n        $each->promise()->then(function () use (&$called): void {\n            $called = true;\n        });\n        $this->assertFalse($called);\n        P\\Utils::queue()->run();\n        $this->assertTrue($called);\n        $this->assertSame(range(0, 99), $values);\n    }\n\n    public function testDoesNotBlowStackWithRejectedPromises(): void\n    {\n        $pending = [];\n        for ($i = 0; $i < 100; ++$i) {\n            $pending[] = new RejectedPromise($i);\n        }\n        $values = [];\n        $each = new EachPromise($pending, [\n            'rejected' => function ($value) use (&$values): void {\n                $values[] = $value;\n            },\n        ]);\n        $called = false;\n        $each->promise()->then(\n            function () use (&$called): void { $called = true; },\n            function (): void { $this->fail('Should not have rejected.'); }\n        );\n        $this->assertFalse($called);\n        P\\Utils::queue()->run();\n        $this->assertTrue($called);\n        $this->assertSame(range(0, 99), $values);\n    }\n\n    public function testReturnsPromiseForWhatever(): void\n    {\n        $called = [];\n        $arr = ['a', 'b'];\n        $each = new EachPromise($arr, [\n            'fulfilled' => function ($v) use (&$called): void { $called[] = $v; },\n        ]);\n        $p = $each->promise();\n        $this->assertNull($p->wait());\n        $this->assertSame(['a', 'b'], $called);\n    }\n\n    public function testRejectsAggregateWhenNextThrows(): void\n    {\n        $iter = function () {\n            yield 'a';\n            throw new \\Exception('Failure');\n        };\n        $each = new EachPromise($iter());\n        $p = $each->promise();\n        $e = null;\n        $received = null;\n        $p->then(null, function ($reason) use (&$e): void { $e = $reason; });\n        P\\Utils::queue()->run();\n        $this->assertInstanceOf(\\Exception::class, $e);\n        $this->assertSame('Failure', $e->getMessage());\n    }\n\n    public function testDoesNotCallNextOnIteratorUntilNeededWhenWaiting(): void\n    {\n        $results = [];\n        $values = [10];\n        $remaining = 9;\n        $iter = function () use (&$values) {\n            while ($value = array_pop($values)) {\n                yield $value;\n            }\n        };\n        $each = new EachPromise($iter(), [\n            'concurrency' => 1,\n            'fulfilled' => function ($r) use (&$results, &$values, &$remaining): void {\n                $results[] = $r;\n                if ($remaining > 0) {\n                    $values[] = $remaining--;\n                }\n            },\n        ]);\n        $each->promise()->wait();\n        $this->assertSame(range(10, 1), $results);\n    }\n\n    public function testDoesNotCallNextOnIteratorUntilNeededWhenAsync(): void\n    {\n        $firstPromise = new Promise();\n        $pending = [$firstPromise];\n        $values = [$firstPromise];\n        $results = [];\n        $remaining = 9;\n        $iter = function () use (&$values) {\n            while ($value = array_pop($values)) {\n                yield $value;\n            }\n        };\n        $each = new EachPromise($iter(), [\n            'concurrency' => 1,\n            'fulfilled' => function ($r) use (&$results, &$values, &$remaining, &$pending): void {\n                $results[] = $r;\n                if ($remaining-- > 0) {\n                    $pending[] = $values[] = new Promise();\n                }\n            },\n        ]);\n        $i = 0;\n        $each->promise();\n        while ($promise = array_pop($pending)) {\n            $promise->resolve($i++);\n            P\\Utils::queue()->run();\n        }\n        $this->assertSame(range(0, 9), $results);\n    }\n\n    private function createSelfResolvingPromise($value)\n    {\n        $p = new Promise(function () use (&$p, $value): void {\n            $p->resolve($value);\n        });\n        $trickCsFixer = true;\n\n        return $p;\n    }\n\n    public function testMutexPreventsGeneratorRecursion(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $results = $promises = [];\n        for ($i = 0; $i < 20; ++$i) {\n            $p = $this->createSelfResolvingPromise($i);\n            $pending[] = $p;\n            $promises[] = $p;\n        }\n\n        $iter = function () use (&$promises, &$pending) {\n            foreach ($promises as $promise) {\n                // Resolve a promises, which will trigger the then() function,\n                // which would cause the EachPromise to try to add more\n                // promises to the queue. Without a lock, this would trigger\n                // a \"Cannot resume an already running generator\" fatal error.\n                if ($p = array_pop($pending)) {\n                    $p->wait();\n                }\n                yield $promise;\n            }\n        };\n\n        $each = new EachPromise($iter(), [\n            'concurrency' => 5,\n            'fulfilled' => function ($r) use (&$results, &$pending): void {\n                $results[] = $r;\n            },\n        ]);\n\n        $each->promise()->wait();\n        $this->assertCount(20, $results);\n    }\n\n    public function testIteratorWithSameKey(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $iter = function () {\n            yield 'foo' => $this->createSelfResolvingPromise(1);\n            yield 'foo' => $this->createSelfResolvingPromise(2);\n            yield 1 => $this->createSelfResolvingPromise(3);\n            yield 1 => $this->createSelfResolvingPromise(4);\n        };\n        $called = 0;\n        $each = new EachPromise($iter(), [\n            'fulfilled' => function ($value, $idx, Promise $aggregate) use (&$called): void {\n                ++$called;\n                if ($value < 3) {\n                    $this->assertSame('foo', $idx);\n                } else {\n                    $this->assertSame(1, $idx);\n                }\n            },\n        ]);\n        $each->promise()->wait();\n        $this->assertSame(4, $called);\n    }\n\n    public function testIsWaitableWhenLimited(): void\n    {\n        $promises = [\n            $this->createSelfResolvingPromise('a'),\n            $this->createSelfResolvingPromise('c'),\n            $this->createSelfResolvingPromise('b'),\n            $this->createSelfResolvingPromise('d'),\n        ];\n        $called = [];\n        $each = new EachPromise($promises, [\n            'concurrency' => 2,\n            'fulfilled' => function ($value) use (&$called): void {\n                $called[] = $value;\n            },\n        ]);\n        $p = $each->promise();\n        $this->assertNull($p->wait());\n        $this->assertSame(['a', 'c', 'b', 'd'], $called);\n        $this->assertTrue(P\\Is::fulfilled($p));\n    }\n}\n"
  },
  {
    "path": "tests/EachTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse PHPUnit\\Framework\\TestCase;\n\nclass EachTest extends TestCase\n{\n    public function testCallsEachLimit(): void\n    {\n        $p = new Promise();\n        $aggregate = P\\Each::ofLimit($p, 2);\n\n        $p->resolve('a');\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::fulfilled($aggregate));\n    }\n\n    public function testEachLimitAllRejectsOnFailure(): void\n    {\n        $p = [new FulfilledPromise('a'), new RejectedPromise('b')];\n        $aggregate = P\\Each::ofLimitAll($p, 2);\n\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::rejected($aggregate));\n\n        $result = P\\Utils::inspect($aggregate);\n        $this->assertSame('b', $result['reason']);\n    }\n}\n"
  },
  {
    "path": "tests/FulfilledPromiseTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse PHPUnit\\Framework\\TestCase;\n\n/**\n * @covers \\GuzzleHttp\\Promise\\FulfilledPromise\n */\nclass FulfilledPromiseTest extends TestCase\n{\n    public function testReturnsValueWhenWaitedUpon(): void\n    {\n        $p = new FulfilledPromise('foo');\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertSame('foo', $p->wait(true));\n    }\n\n    public function testCannotCancel(): void\n    {\n        $p = new FulfilledPromise('foo');\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $p->cancel();\n        $this->assertSame('foo', $p->wait());\n    }\n\n    /**\n     * @expectedExceptionMessage Cannot resolve a fulfilled promise\n     */\n    public function testCannotResolve(): void\n    {\n        $this->expectException(\\LogicException::class);\n\n        $p = new FulfilledPromise('foo');\n        $p->resolve('bar');\n    }\n\n    /**\n     * @expectedExceptionMessage Cannot reject a fulfilled promise\n     */\n    public function testCannotReject(): void\n    {\n        $this->expectException(\\LogicException::class);\n\n        $p = new FulfilledPromise('foo');\n        $p->reject('bar');\n    }\n\n    public function testCanResolveWithSameValue(): void\n    {\n        $p = new FulfilledPromise('foo');\n        $p->resolve('foo');\n        $this->assertSame('foo', $p->wait());\n    }\n\n    public function testCannotResolveWithPromise(): void\n    {\n        $this->expectException(\\InvalidArgumentException::class);\n\n        new FulfilledPromise(new Promise());\n    }\n\n    public function testReturnsSelfWhenNoOnFulfilled(): void\n    {\n        $p = new FulfilledPromise('a');\n        $this->assertSame($p, $p->then());\n    }\n\n    public function testAsynchronouslyInvokesOnFulfilled(): void\n    {\n        $p = new FulfilledPromise('a');\n        $r = null;\n        $f = function ($d) use (&$r): void { $r = $d; };\n        $p2 = $p->then($f);\n        $this->assertNotSame($p, $p2);\n        $this->assertNull($r);\n        P\\Utils::queue()->run();\n        $this->assertSame('a', $r);\n    }\n\n    public function testReturnsNewRejectedWhenOnFulfilledFails(): void\n    {\n        $p = new FulfilledPromise('a');\n        $f = function (): void { throw new \\Exception('b'); };\n        $p2 = $p->then($f);\n        $this->assertNotSame($p, $p2);\n        try {\n            $p2->wait();\n            $this->fail();\n        } catch (\\Exception $e) {\n            $this->assertSame('b', $e->getMessage());\n        }\n    }\n\n    public function testOtherwiseIsSugarForRejections(): void\n    {\n        $c = null;\n        $p = new FulfilledPromise('foo');\n        $p->otherwise(function ($v) use (&$c): void { $c = $v; });\n        $this->assertNull($c);\n    }\n\n    public function testDoesNotTryToFulfillTwiceDuringTrampoline(): void\n    {\n        $fp = new FulfilledPromise('a');\n        $t1 = $fp->then(function ($v) { return $v.' b'; });\n        $t1->resolve('why!');\n        $this->assertSame('why!', $t1->wait());\n    }\n}\n"
  },
  {
    "path": "tests/IsTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse PHPUnit\\Framework\\TestCase;\n\nclass IsTest extends TestCase\n{\n    public function testKnowsIfFulfilled(): void\n    {\n        $p = new FulfilledPromise(null);\n        $this->assertTrue(P\\Is::fulfilled($p));\n        $this->assertFalse(P\\Is::rejected($p));\n    }\n\n    public function testKnowsIfRejected(): void\n    {\n        $p = new RejectedPromise(null);\n        $this->assertTrue(P\\Is::rejected($p));\n        $this->assertFalse(P\\Is::fulfilled($p));\n    }\n\n    public function testKnowsIfSettled(): void\n    {\n        $p = new RejectedPromise(null);\n        $this->assertTrue(P\\Is::settled($p));\n        $this->assertFalse(P\\Is::pending($p));\n    }\n\n    public function testKnowsIfPending(): void\n    {\n        $p = new Promise();\n        $this->assertFalse(P\\Is::settled($p));\n        $this->assertTrue(P\\Is::pending($p));\n    }\n}\n"
  },
  {
    "path": "tests/NotPromiseInstance.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\PromiseInterface;\n\nclass NotPromiseInstance extends Thennable implements PromiseInterface\n{\n    private $nextPromise;\n\n    public function __construct()\n    {\n        $this->nextPromise = new Promise();\n    }\n\n    public function then(?callable $res = null, ?callable $rej = null): PromiseInterface\n    {\n        return $this->nextPromise->then($res, $rej);\n    }\n\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->then($onRejected);\n    }\n\n    public function resolve($value): void\n    {\n        $this->nextPromise->resolve($value);\n    }\n\n    public function reject($reason): void\n    {\n        $this->nextPromise->reject($reason);\n    }\n\n    public function wait(bool $unwrap = true, ?bool $defaultResolution = null): void\n    {\n    }\n\n    public function cancel(): void\n    {\n    }\n\n    public function getState(): string\n    {\n        return $this->nextPromise->getState();\n    }\n}\n"
  },
  {
    "path": "tests/PromiseTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\CancellationException;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse GuzzleHttp\\Promise\\RejectionException;\nuse PHPUnit\\Framework\\TestCase;\n\n/**\n * @covers \\GuzzleHttp\\Promise\\Promise\n */\nclass PromiseTest extends TestCase\n{\n    public function testCannotResolveNonPendingPromise(): void\n    {\n        $this->expectException(\\LogicException::class);\n        $this->expectExceptionMessage('The promise is already fulfilled');\n\n        $p = new Promise();\n        $p->resolve('foo');\n        $p->resolve('bar');\n        $this->assertSame('foo', $p->wait());\n    }\n\n    public function testCanResolveWithSameValue(): void\n    {\n        $p = new Promise();\n        $p->resolve('foo');\n        $p->resolve('foo');\n        $this->assertSame('foo', $p->wait());\n    }\n\n    public function testCannotRejectNonPendingPromise(): void\n    {\n        $this->expectException(\\LogicException::class);\n        $this->expectExceptionMessage('Cannot change a fulfilled promise to rejected');\n\n        $p = new Promise();\n        $p->resolve('foo');\n        $p->reject('bar');\n        $this->assertSame('foo', $p->wait());\n    }\n\n    public function testCanRejectWithSameValue(): void\n    {\n        $p = new Promise();\n        $p->reject('foo');\n        $p->reject('foo');\n        $this->assertTrue(P\\Is::rejected($p));\n    }\n\n    public function testCannotRejectResolveWithSameValue(): void\n    {\n        $this->expectException(\\LogicException::class);\n        $this->expectExceptionMessage('Cannot change a fulfilled promise to rejected');\n\n        $p = new Promise();\n        $p->resolve('foo');\n        $p->reject('foo');\n    }\n\n    public function testInvokesWaitFunction(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->resolve('10');\n        });\n        $this->assertSame('10', $p->wait());\n    }\n\n    public function testRejectsAndThrowsWhenWaitFailsToResolve(): void\n    {\n        $this->expectException(RejectionException::class);\n        $this->expectExceptionMessage('The promise was rejected with reason: Invoking the wait callback did not resolve the promise');\n\n        $p = new Promise(function (): void {});\n        $p->wait();\n    }\n\n    public function testThrowsWhenUnwrapIsRejectedWithNonException(): void\n    {\n        $this->expectException(RejectionException::class);\n        $this->expectExceptionMessage('The promise was rejected with reason: foo');\n\n        $p = new Promise(function () use (&$p): void {\n            $p->reject('foo');\n        });\n        $p->wait();\n    }\n\n    public function testThrowsWhenUnwrapIsRejectedWithException(): void\n    {\n        $this->expectException(\\UnexpectedValueException::class);\n        $this->expectExceptionMessage('foo');\n\n        $e = new \\UnexpectedValueException('foo');\n        $p = new Promise(function () use (&$p, $e): void {\n            $p->reject($e);\n        });\n        $p->wait();\n    }\n\n    public function testDoesNotUnwrapExceptionsWhenDisabled(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->reject('foo');\n        });\n        $this->assertTrue(P\\Is::pending($p));\n        $p->wait(false);\n        $this->assertTrue(P\\Is::rejected($p));\n    }\n\n    public function testRejectsSelfWhenWaitThrows(): void\n    {\n        $e = new \\UnexpectedValueException('foo');\n        $p = new Promise(function () use ($e): void {\n            throw $e;\n        });\n        try {\n            $p->wait();\n            $this->fail();\n        } catch (\\UnexpectedValueException $e) {\n            $this->assertTrue(P\\Is::rejected($p));\n        }\n    }\n\n    public function testWaitsOnNestedPromises(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->resolve('_');\n        });\n        $p2 = new Promise(function () use (&$p2): void {\n            $p2->resolve('foo');\n        });\n        $p3 = $p->then(function () use ($p2) {\n            return $p2;\n        });\n        $this->assertSame('foo', $p3->wait());\n    }\n\n    public function testThrowsWhenWaitingOnPromiseWithNoWaitFunction(): void\n    {\n        $this->expectException(RejectionException::class);\n\n        $p = new Promise();\n        $p->wait();\n    }\n\n    public function testThrowsWaitExceptionAfterPromiseIsResolved(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->reject('Foo!');\n            throw new \\Exception('Bar?');\n        });\n\n        try {\n            $p->wait();\n            $this->fail();\n        } catch (\\Exception $e) {\n            $this->assertSame('Bar?', $e->getMessage());\n        }\n    }\n\n    public function testGetsActualWaitValueFromThen(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->reject('Foo!');\n        });\n        $p2 = $p->then(null, function ($reason) {\n            return new RejectedPromise([$reason]);\n        });\n\n        try {\n            $p2->wait();\n            $this->fail('Should have thrown');\n        } catch (RejectionException $e) {\n            $this->assertSame(['Foo!'], $e->getReason());\n        }\n    }\n\n    public function testWaitBehaviorIsBasedOnLastPromiseInChain(): void\n    {\n        $p3 = new Promise(function () use (&$p3): void {\n            $p3->resolve('Whoop');\n        });\n        $p2 = new Promise(function () use (&$p2, $p3): void {\n            $p2->reject($p3);\n        });\n        $p = new Promise(function () use (&$p, $p2): void {\n            $p->reject($p2);\n        });\n        $this->assertSame('Whoop', $p->wait());\n    }\n\n    public function testWaitsOnAPromiseChainEvenWhenNotUnwrapped(): void\n    {\n        $p2 = new Promise(function () use (&$p2): void {\n            $p2->reject('Fail');\n        });\n        $p = new Promise(function () use ($p2, &$p): void {\n            $p->resolve($p2);\n        });\n        $p->wait(false);\n        $this->assertTrue(P\\Is::rejected($p2));\n    }\n\n    public function testCannotCancelNonPending(): void\n    {\n        $p = new Promise();\n        $p->resolve('foo');\n        $p->cancel();\n        $this->assertTrue(P\\Is::fulfilled($p));\n    }\n\n    public function testCancelsPromiseWhenNoCancelFunction(): void\n    {\n        $this->expectException(CancellationException::class);\n\n        $p = new Promise();\n        $p->cancel();\n        $this->assertTrue(P\\Is::rejected($p));\n        $p->wait();\n    }\n\n    public function testCancelsPromiseWithCancelFunction(): void\n    {\n        $called = false;\n        $p = new Promise(null, function () use (&$called): void {\n            $called = true;\n        });\n        $p->cancel();\n        $this->assertTrue(P\\Is::rejected($p));\n        $this->assertTrue($called);\n    }\n\n    public function testCancelsUppermostPendingPromise(): void\n    {\n        $called = false;\n        $p1 = new Promise(null, function () use (&$called): void {\n            $called = true;\n        });\n        $p2 = $p1->then(function (): void {});\n        $p3 = $p2->then(function (): void {});\n        $p4 = $p3->then(function (): void {});\n        $p3->cancel();\n        $this->assertTrue(P\\Is::rejected($p1));\n        $this->assertTrue(P\\Is::rejected($p2));\n        $this->assertTrue(P\\Is::rejected($p3));\n        $this->assertTrue(P\\Is::pending($p4));\n        $this->assertTrue($called);\n\n        try {\n            $p3->wait();\n            $this->fail();\n        } catch (CancellationException $e) {\n            $this->assertStringContainsString('cancelled', $e->getMessage());\n        }\n\n        try {\n            $p4->wait();\n            $this->fail();\n        } catch (CancellationException $e) {\n            $this->assertStringContainsString('cancelled', $e->getMessage());\n        }\n\n        $this->assertTrue(P\\Is::rejected($p4));\n    }\n\n    public function testCancelsChildPromises(): void\n    {\n        $called1 = $called2 = $called3 = false;\n        $p1 = new Promise(null, function () use (&$called1): void {\n            $called1 = true;\n        });\n        $p2 = new Promise(null, function () use (&$called2): void {\n            $called2 = true;\n        });\n        $p3 = new Promise(null, function () use (&$called3): void {\n            $called3 = true;\n        });\n        $p4 = $p2->then(function () use ($p3) {\n            return $p3;\n        });\n        $p5 = $p4->then(function (): void {\n            $this->fail();\n        });\n        $p4->cancel();\n        $this->assertTrue(P\\Is::pending($p1));\n        $this->assertTrue(P\\Is::rejected($p2));\n        $this->assertTrue(P\\Is::pending($p3));\n        $this->assertTrue(P\\Is::rejected($p4));\n        $this->assertTrue(P\\Is::pending($p5));\n        $this->assertFalse($called1);\n        $this->assertTrue($called2);\n        $this->assertFalse($called3);\n    }\n\n    public function testRejectsPromiseWhenCancelFails(): void\n    {\n        $called = false;\n        $p = new Promise(null, function () use (&$called): void {\n            $called = true;\n            throw new \\Exception('e');\n        });\n        $p->cancel();\n        $this->assertTrue(P\\Is::rejected($p));\n        $this->assertTrue($called);\n        try {\n            $p->wait();\n            $this->fail();\n        } catch (\\Exception $e) {\n            $this->assertSame('e', $e->getMessage());\n        }\n    }\n\n    public function testCreatesPromiseWhenFulfilledAfterThen(): void\n    {\n        $p = new Promise();\n        $carry = null;\n        $p2 = $p->then(function ($v) use (&$carry): void {\n            $carry = $v;\n        });\n        $this->assertNotSame($p, $p2);\n        $p->resolve('foo');\n        P\\Utils::queue()->run();\n\n        $this->assertSame('foo', $carry);\n    }\n\n    public function testCreatesPromiseWhenFulfilledBeforeThen(): void\n    {\n        $p = new Promise();\n        $p->resolve('foo');\n        $carry = null;\n        $p2 = $p->then(function ($v) use (&$carry): void {\n            $carry = $v;\n        });\n        $this->assertNotSame($p, $p2);\n        $this->assertNull($carry);\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $carry);\n    }\n\n    public function testCreatesPromiseWhenFulfilledWithNoCallback(): void\n    {\n        $p = new Promise();\n        $p->resolve('foo');\n        $p2 = $p->then();\n        $this->assertNotSame($p, $p2);\n        $this->assertInstanceOf(FulfilledPromise::class, $p2);\n    }\n\n    public function testCreatesPromiseWhenRejectedAfterThen(): void\n    {\n        $p = new Promise();\n        $carry = null;\n        $p2 = $p->then(null, function ($v) use (&$carry): void {\n            $carry = $v;\n        });\n        $this->assertNotSame($p, $p2);\n        $p->reject('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $carry);\n    }\n\n    public function testCreatesPromiseWhenRejectedBeforeThen(): void\n    {\n        $p = new Promise();\n        $p->reject('foo');\n        $carry = null;\n        $p2 = $p->then(null, function ($v) use (&$carry): void {\n            $carry = $v;\n        });\n        $this->assertNotSame($p, $p2);\n        $this->assertNull($carry);\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $carry);\n    }\n\n    public function testCreatesPromiseWhenRejectedWithNoCallback(): void\n    {\n        $p = new Promise();\n        $p->reject('foo');\n        $p2 = $p->then();\n        $this->assertNotSame($p, $p2);\n        $this->assertInstanceOf(RejectedPromise::class, $p2);\n    }\n\n    public function testInvokesWaitFnsForThens(): void\n    {\n        $p = new Promise(function () use (&$p): void {\n            $p->resolve('a');\n        });\n        $p2 = $p\n            ->then(function ($v) {\n                return $v.'-1-';\n            })\n            ->then(function ($v) {\n                return $v.'2';\n            });\n        $this->assertSame('a-1-2', $p2->wait());\n    }\n\n    public function testStacksThenWaitFunctions(): void\n    {\n        $p1 = new Promise(function () use (&$p1): void {\n            $p1->resolve('a');\n        });\n        $p2 = new Promise(function () use (&$p2): void {\n            $p2->resolve('b');\n        });\n        $p3 = new Promise(function () use (&$p3): void {\n            $p3->resolve('c');\n        });\n        $p4 = $p1\n            ->then(function () use ($p2) {\n                return $p2;\n            })\n            ->then(function () use ($p3) {\n                return $p3;\n            });\n        $this->assertSame('c', $p4->wait());\n    }\n\n    public function testForwardsFulfilledDownChainBetweenGaps(): void\n    {\n        $p = new Promise();\n        $r = $r2 = null;\n        $p->then(null, null)\n            ->then(function ($v) use (&$r) {\n                $r = $v;\n\n                return $v.'2';\n            })\n            ->then(function ($v) use (&$r2): void {\n                $r2 = $v;\n            });\n        $p->resolve('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $r);\n        $this->assertSame('foo2', $r2);\n    }\n\n    public function testForwardsRejectedPromisesDownChainBetweenGaps(): void\n    {\n        $p = new Promise();\n        $r = $r2 = null;\n        $p->then(null, null)\n            ->then(null, function ($v) use (&$r) {\n                $r = $v;\n\n                return $v.'2';\n            })\n            ->then(function ($v) use (&$r2): void {\n                $r2 = $v;\n            });\n        $p->reject('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $r);\n        $this->assertSame('foo2', $r2);\n    }\n\n    public function testForwardsThrownPromisesDownChainBetweenGaps(): void\n    {\n        $e = new \\Exception();\n        $p = new Promise();\n        $r = $r2 = null;\n        $p->then(null, null)\n            ->then(null, function ($v) use (&$r, $e): void {\n                $r = $v;\n                throw $e;\n            })\n            ->then(\n                null,\n                function ($v) use (&$r2): void {\n                    $r2 = $v;\n                }\n            );\n        $p->reject('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $r);\n        $this->assertSame($e, $r2);\n    }\n\n    public function testForwardsReturnedRejectedPromisesDownChainBetweenGaps(): void\n    {\n        $p = new Promise();\n        $rejected = new RejectedPromise('bar');\n        $r = $r2 = null;\n        $p->then(null, null)\n            ->then(null, function ($v) use (&$r, $rejected) {\n                $r = $v;\n\n                return $rejected;\n            })\n            ->then(\n                null,\n                function ($v) use (&$r2): void {\n                    $r2 = $v;\n                }\n            );\n        $p->reject('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $r);\n        $this->assertSame('bar', $r2);\n        try {\n            $p->wait();\n        } catch (RejectionException $e) {\n            $this->assertSame('foo', $e->getReason());\n        }\n    }\n\n    public function testForwardsHandlersToNextPromise(): void\n    {\n        $p = new Promise();\n        $p2 = new Promise();\n        $resolved = null;\n        $p\n            ->then(function ($v) use ($p2) {\n                return $p2;\n            })\n            ->then(function ($value) use (&$resolved): void {\n                $resolved = $value;\n            });\n        $p->resolve('a');\n        $p2->resolve('b');\n        P\\Utils::queue()->run();\n        $this->assertSame('b', $resolved);\n    }\n\n    public function testRemovesReferenceFromChildWhenParentWaitedUpon(): void\n    {\n        $r = null;\n        $p = new Promise(function () use (&$p): void {\n            $p->resolve('a');\n        });\n        $p2 = new Promise(function () use (&$p2): void {\n            $p2->resolve('b');\n        });\n        $pb = $p->then(\n            function ($v) use ($p2, &$r) {\n                $r = $v;\n\n                return $p2;\n            }\n        )\n            ->then(function ($v) {\n                return $v.'.';\n            });\n        $this->assertSame('a', $p->wait());\n        $this->assertSame('b', $p2->wait());\n        $this->assertSame('b.', $pb->wait());\n        $this->assertSame('a', $r);\n    }\n\n    public function testForwardsHandlersWhenFulfilledPromiseIsReturned(): void\n    {\n        $res = [];\n        $p = new Promise();\n        $p2 = new Promise();\n        $p2->resolve('foo');\n        $p2->then(function ($v) use (&$res): void {\n            $res[] = 'A:'.$v;\n        });\n        // $res is A:foo\n        $p\n            ->then(function () use ($p2, &$res) {\n                $res[] = 'B';\n\n                return $p2;\n            })\n            ->then(function ($v) use (&$res): void {\n                $res[] = 'C:'.$v;\n            });\n        $p->resolve('a');\n        $p->then(function ($v) use (&$res): void {\n            $res[] = 'D:'.$v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);\n    }\n\n    public function testForwardsHandlersWhenRejectedPromiseIsReturned(): void\n    {\n        $res = [];\n        $p = new Promise();\n        $p2 = new Promise();\n        $p2->reject('foo');\n        $p2->then(null, function ($v) use (&$res): void {\n            $res[] = 'A:'.$v;\n        });\n        $p->then(null, function () use ($p2, &$res) {\n            $res[] = 'B';\n\n            return $p2;\n        })\n            ->then(null, function ($v) use (&$res): void {\n                $res[] = 'C:'.$v;\n            });\n        $p->reject('a');\n        $p->then(null, function ($v) use (&$res): void {\n            $res[] = 'D:'.$v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);\n    }\n\n    public function testDoesNotForwardRejectedPromise(): void\n    {\n        $res = [];\n        $p = new Promise();\n        $p2 = new Promise();\n        $p2->cancel();\n        $p2->then(function ($v) use (&$res) {\n            $res[] = \"B:$v\";\n\n            return $v;\n        });\n        $p->then(function ($v) use ($p2, &$res) {\n            $res[] = \"B:$v\";\n\n            return $p2;\n        })\n            ->then(function ($v) use (&$res): void {\n                $res[] = 'C:'.$v;\n            });\n        $p->resolve('a');\n        $p->then(function ($v) use (&$res): void {\n            $res[] = 'D:'.$v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame(['B:a', 'D:a'], $res);\n    }\n\n    public function testRecursivelyForwardsWhenOnlyThennable(): void\n    {\n        $res = [];\n        $p = new Promise();\n        $p2 = new Thennable();\n        $p2->resolve('foo');\n        $p2->then(function ($v) use (&$res): void {\n            $res[] = 'A:'.$v;\n        });\n        $p->then(function () use ($p2, &$res) {\n            $res[] = 'B';\n\n            return $p2;\n        })\n            ->then(function ($v) use (&$res): void {\n                $res[] = 'C:'.$v;\n            });\n        $p->resolve('a');\n        $p->then(function ($v) use (&$res): void {\n            $res[] = 'D:'.$v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);\n    }\n\n    public function testRecursivelyForwardsWhenNotInstanceOfPromise(): void\n    {\n        $res = [];\n        $p = new Promise();\n        $p2 = new NotPromiseInstance();\n        $p2->then(function ($v) use (&$res): void {\n            $res[] = 'A:'.$v;\n        });\n        $p->then(function () use ($p2, &$res) {\n            $res[] = 'B';\n\n            return $p2;\n        })\n            ->then(function ($v) use (&$res): void {\n                $res[] = 'C:'.$v;\n            });\n        $p->resolve('a');\n        $p->then(function ($v) use (&$res): void {\n            $res[] = 'D:'.$v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame(['B', 'D:a'], $res);\n        $p2->resolve('foo');\n        P\\Utils::queue()->run();\n        $this->assertSame(['B', 'D:a', 'A:foo', 'C:foo'], $res);\n    }\n\n    public function testCannotResolveWithSelf(): void\n    {\n        $this->expectException(\\LogicException::class);\n        $this->expectExceptionMessage('Cannot fulfill or reject a promise with itself');\n\n        $p = new Promise();\n        $p->resolve($p);\n    }\n\n    public function testCannotRejectWithSelf(): void\n    {\n        $this->expectException(\\LogicException::class);\n        $this->expectExceptionMessage('Cannot fulfill or reject a promise with itself');\n\n        $p = new Promise();\n        $p->reject($p);\n    }\n\n    public function testDoesNotBlowStackWhenWaitingOnNestedThens(): void\n    {\n        $inner = new Promise(function () use (&$inner): void {\n            $inner->resolve(0);\n        });\n        $prev = $inner;\n        for ($i = 1; $i < 100; ++$i) {\n            $prev = $prev->then(function ($i) {\n                return $i + 1;\n            });\n        }\n\n        $parent = new Promise(function () use (&$parent, $prev): void {\n            $parent->resolve($prev);\n        });\n\n        $this->assertSame(99, $parent->wait());\n    }\n\n    public function testOtherwiseIsSugarForRejections(): void\n    {\n        $p = new Promise();\n        $p->reject('foo');\n        $p->otherwise(function ($v) use (&$c): void {\n            $c = $v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame($c, 'foo');\n    }\n\n    public function testRepeatedWaitFulfilled(): void\n    {\n        $promise = new Promise(function () use (&$promise): void {\n            $promise->resolve('foo');\n        });\n\n        $this->assertSame('foo', $promise->wait());\n        $this->assertSame('foo', $promise->wait());\n    }\n\n    public function testRepeatedWaitRejected(): void\n    {\n        $promise = new Promise(function () use (&$promise): void {\n            $promise->reject(new \\RuntimeException('foo'));\n        });\n\n        $exceptionCount = 0;\n        try {\n            $promise->wait();\n        } catch (\\Exception $e) {\n            $this->assertSame('foo', $e->getMessage());\n            ++$exceptionCount;\n        }\n\n        try {\n            $promise->wait();\n        } catch (\\Exception $e) {\n            $this->assertSame('foo', $e->getMessage());\n            ++$exceptionCount;\n        }\n\n        $this->assertSame(2, $exceptionCount);\n    }\n}\n"
  },
  {
    "path": "tests/PropertyHelper.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\n/**\n * A class to help get properties of an object.\n *\n * @internal\n *\n * @author Tobias Nyholm <tobias.nyholm@gmail.com>\n */\nclass PropertyHelper\n{\n    /**\n     * @param object $object\n     * @param string $property\n     *\n     * @throws \\ReflectionException\n     */\n    public static function get($object, $property)\n    {\n        $property = (new \\ReflectionObject($object))->getProperty($property);\n\n        if (PHP_VERSION_ID < 80100) {\n            $property->setAccessible(true);\n        }\n\n        return $property->getValue($object);\n    }\n}\n"
  },
  {
    "path": "tests/RejectedPromiseTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse PHPUnit\\Framework\\TestCase;\n\n/**\n * @covers \\GuzzleHttp\\Promise\\RejectedPromise\n */\nclass RejectedPromiseTest extends TestCase\n{\n    public function testThrowsReasonWhenWaitedUpon(): void\n    {\n        $p = new RejectedPromise('foo');\n        $this->assertTrue(P\\Is::rejected($p));\n        try {\n            $p->wait(true);\n            $this->fail();\n        } catch (\\Exception $e) {\n            $this->assertTrue(P\\Is::rejected($p));\n            $this->assertStringContainsString('foo', $e->getMessage());\n        }\n    }\n\n    public function testCannotCancel(): void\n    {\n        $p = new RejectedPromise('foo');\n        $p->cancel();\n        $this->assertTrue(P\\Is::rejected($p));\n    }\n\n    /**\n     * @exepctedExceptionMessage Cannot resolve a rejected promise\n     */\n    public function testCannotResolve(): void\n    {\n        $this->expectException(\\LogicException::class);\n\n        $p = new RejectedPromise('foo');\n        $p->resolve('bar');\n    }\n\n    /**\n     * @expectedExceptionMessage Cannot reject a rejected promise\n     */\n    public function testCannotReject(): void\n    {\n        $this->expectException(\\LogicException::class);\n\n        $p = new RejectedPromise('foo');\n        $p->reject('bar');\n    }\n\n    public function testCanRejectWithSameValue(): void\n    {\n        $p = new RejectedPromise('foo');\n        $p->reject('foo');\n        $this->assertTrue(P\\Is::rejected($p));\n    }\n\n    public function testThrowsSpecificException(): void\n    {\n        $e = new \\Exception();\n        $p = new RejectedPromise($e);\n        try {\n            $p->wait(true);\n            $this->fail();\n        } catch (\\Exception $e2) {\n            $this->assertSame($e, $e2);\n        }\n    }\n\n    public function testCannotResolveWithPromise(): void\n    {\n        $this->expectException(\\InvalidArgumentException::class);\n\n        new RejectedPromise(new Promise());\n    }\n\n    public function testReturnsSelfWhenNoOnReject(): void\n    {\n        $p = new RejectedPromise('a');\n        $this->assertSame($p, $p->then());\n    }\n\n    public function testInvokesOnRejectedAsynchronously(): void\n    {\n        $p = new RejectedPromise('a');\n        $r = null;\n        $f = function ($reason) use (&$r): void { $r = $reason; };\n        $p->then(null, $f);\n        $this->assertNull($r);\n        P\\Utils::queue()->run();\n        $this->assertSame('a', $r);\n    }\n\n    public function testReturnsNewRejectedWhenOnRejectedFails(): void\n    {\n        $p = new RejectedPromise('a');\n        $f = function (): void { throw new \\Exception('b'); };\n        $p2 = $p->then(null, $f);\n        $this->assertNotSame($p, $p2);\n        try {\n            $p2->wait();\n            $this->fail();\n        } catch (\\Exception $e) {\n            $this->assertSame('b', $e->getMessage());\n        }\n    }\n\n    public function testWaitingIsNoOp(): void\n    {\n        $p = new RejectedPromise('a');\n        $p->wait(false);\n        $this->assertTrue(P\\Is::rejected($p));\n    }\n\n    public function testOtherwiseIsSugarForRejections(): void\n    {\n        $p = new RejectedPromise('foo');\n        $p->otherwise(function ($v) use (&$c): void { $c = $v; });\n        P\\Utils::queue()->run();\n        $this->assertSame('foo', $c);\n    }\n\n    public function testCanResolveThenWithSuccess(): void\n    {\n        $actual = null;\n        $p = new RejectedPromise('foo');\n        $p->otherwise(function ($v) {\n            return $v.' bar';\n        })->then(function ($v) use (&$actual): void {\n            $actual = $v;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame('foo bar', $actual);\n    }\n\n    public function testDoesNotTryToRejectTwiceDuringTrampoline(): void\n    {\n        $fp = new RejectedPromise('a');\n        $t1 = $fp->then(null, function ($v) { return $v.' b'; });\n        $t1->resolve('why!');\n        $this->assertSame('why!', $t1->wait());\n    }\n}\n"
  },
  {
    "path": "tests/RejectionExceptionTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\RejectionException;\nuse PHPUnit\\Framework\\TestCase;\n\n/**\n * @covers \\GuzzleHttp\\Promise\\RejectionException\n */\nclass RejectionExceptionTest extends TestCase\n{\n    public function testCanGetReasonFromException(): void\n    {\n        $thing = new Thing1('foo');\n        $e = new RejectionException($thing);\n\n        $this->assertSame($thing, $e->getReason());\n        $this->assertSame('The promise was rejected with reason: foo', $e->getMessage());\n    }\n\n    public function testCanGetReasonMessageFromJson(): void\n    {\n        $reason = new Thing2();\n        $e = new RejectionException($reason);\n        $this->assertStringContainsString('{}', $e->getMessage());\n    }\n}\n"
  },
  {
    "path": "tests/TaskQueueTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\TaskQueue;\nuse PHPUnit\\Framework\\TestCase;\n\nclass TaskQueueTest extends TestCase\n{\n    public function testKnowsIfEmpty(): void\n    {\n        $tq = new TaskQueue(false);\n        $this->assertTrue($tq->isEmpty());\n    }\n\n    public function testKnowsIfFull(): void\n    {\n        $tq = new TaskQueue(false);\n        $tq->add(function (): void {});\n        $this->assertFalse($tq->isEmpty());\n    }\n\n    public function testExecutesTasksInOrder(): void\n    {\n        $tq = new TaskQueue(false);\n        $called = [];\n        $tq->add(function () use (&$called): void { $called[] = 'a'; });\n        $tq->add(function () use (&$called): void { $called[] = 'b'; });\n        $tq->add(function () use (&$called): void { $called[] = 'c'; });\n        $tq->run();\n        $this->assertSame(['a', 'b', 'c'], $called);\n    }\n}\n"
  },
  {
    "path": "tests/Thennable.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\Promise;\n\nclass Thennable\n{\n    private $nextPromise;\n\n    public function __construct()\n    {\n        $this->nextPromise = new Promise();\n    }\n\n    public function then(?callable $res = null, ?callable $rej = null)\n    {\n        return $this->nextPromise->then($res, $rej);\n    }\n\n    public function resolve($value): void\n    {\n        $this->nextPromise->resolve($value);\n    }\n}\n"
  },
  {
    "path": "tests/Thing1.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nclass Thing1\n{\n    private $message;\n\n    public function __construct($message)\n    {\n        $this->message = $message;\n    }\n\n    public function __toString()\n    {\n        return $this->message;\n    }\n}\n"
  },
  {
    "path": "tests/Thing2.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nclass Thing2 implements \\JsonSerializable\n{\n    #[\\ReturnTypeWillChange]\n    public function jsonSerialize()\n    {\n        return '{}';\n    }\n}\n"
  },
  {
    "path": "tests/UtilsTest.php",
    "content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace GuzzleHttp\\Promise\\Tests;\n\nuse GuzzleHttp\\Promise\\AggregateException;\nuse GuzzleHttp\\Promise as P;\nuse GuzzleHttp\\Promise\\FulfilledPromise;\nuse GuzzleHttp\\Promise\\Promise;\nuse GuzzleHttp\\Promise\\PromiseInterface;\nuse GuzzleHttp\\Promise\\RejectedPromise;\nuse GuzzleHttp\\Promise\\RejectionException;\nuse GuzzleHttp\\Promise\\TaskQueue;\nuse PHPUnit\\Framework\\TestCase;\n\nclass UtilsTest extends TestCase\n{\n    public function testWaitsOnAllPromisesIntoArray(): void\n    {\n        $e = new \\Exception();\n        $a = new Promise(function () use (&$a): void { $a->resolve('a'); });\n        $b = new Promise(function () use (&$b): void { $b->reject('b'); });\n        $c = new Promise(function () use (&$c, $e): void { $c->reject($e); });\n        $results = P\\Utils::inspectAll([$a, $b, $c]);\n        $this->assertSame([\n            ['state' => 'fulfilled', 'value' => 'a'],\n            ['state' => 'rejected', 'reason' => 'b'],\n            ['state' => 'rejected', 'reason' => $e],\n        ], $results);\n    }\n\n    public function testUnwrapsPromisesWithNoDefaultAndFailure(): void\n    {\n        $this->expectException(RejectionException::class);\n\n        $promises = [new FulfilledPromise('a'), new Promise()];\n        P\\Utils::unwrap($promises);\n    }\n\n    public function testUnwrapsPromisesWithNoDefault(): void\n    {\n        $promises = [new FulfilledPromise('a')];\n        $this->assertSame(['a'], P\\Utils::unwrap($promises));\n    }\n\n    public function testUnwrapsPromisesWithKeys(): void\n    {\n        $promises = [\n            'foo' => new FulfilledPromise('a'),\n            'bar' => new FulfilledPromise('b'),\n        ];\n        $this->assertSame([\n            'foo' => 'a',\n            'bar' => 'b',\n        ], P\\Utils::unwrap($promises));\n    }\n\n    public function testAllAggregatesSortedArray(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $c = new Promise();\n        $d = P\\Utils::all([$a, $b, $c]);\n        $b->resolve('b');\n        $a->resolve('a');\n        $c->resolve('c');\n        $d->then(\n            function ($value) use (&$result): void { $result = $value; },\n            function ($reason) use (&$result): void { $result = $reason; }\n        );\n        P\\Utils::queue()->run();\n        $this->assertSame(['a', 'b', 'c'], $result);\n    }\n\n    public function testPromisesDynamicallyAddedToStack(): void\n    {\n        $promises = new \\ArrayIterator();\n        $counter = 0;\n        $promises['a'] = new FulfilledPromise('a');\n        $promises['b'] = $promise = new Promise(function () use (&$promise, &$promises, &$counter): void {\n            ++$counter; // Make sure the wait function is called only once\n            $promise->resolve('b');\n            $promises['c'] = $subPromise = new Promise(function () use (&$subPromise): void {\n                $subPromise->resolve('c');\n            });\n        });\n        $result = P\\Utils::all($promises, true)->wait();\n        $this->assertCount(3, $promises);\n        $this->assertCount(3, $result);\n        $this->assertSame($result['c'], 'c');\n        $this->assertSame(1, $counter);\n    }\n\n    public function testAllThrowsWhenAnyRejected(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $c = new Promise();\n        $d = P\\Utils::all([$a, $b, $c]);\n        $b->resolve('b');\n        $a->reject('fail');\n        $c->resolve('c');\n        $d->then(\n            function ($value) use (&$result): void { $result = $value; },\n            function ($reason) use (&$result): void { $result = $reason; }\n        );\n        P\\Utils::queue()->run();\n        $this->assertSame('fail', $result);\n    }\n\n    public function testSomeAggregatesSortedArrayWithMax(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $c = new Promise();\n        $d = P\\Utils::some(2, [$a, $b, $c]);\n        $b->resolve('b');\n        $c->resolve('c');\n        $a->resolve('a');\n        $d->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame(['b', 'c'], $result);\n    }\n\n    public function testSomeRejectsWhenTooManyRejections(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $d = P\\Utils::some(2, [$a, $b]);\n        $a->reject('bad');\n        $b->resolve('good');\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::rejected($d));\n        $d->then(null, function ($reason) use (&$called): void {\n            $called = $reason;\n        });\n        P\\Utils::queue()->run();\n        $this->assertInstanceOf(AggregateException::class, $called);\n        $this->assertContains('bad', $called->getReason());\n    }\n\n    public function testCanWaitUntilSomeCountIsSatisfied(): void\n    {\n        $a = new Promise(function () use (&$a): void { $a->resolve('a'); });\n        $b = new Promise(function () use (&$b): void { $b->resolve('b'); });\n        $c = new Promise(function () use (&$c): void { $c->resolve('c'); });\n        $d = P\\Utils::some(2, [$a, $b, $c]);\n        $this->assertSame(['a', 'b'], $d->wait());\n    }\n\n    public function testThrowsIfImpossibleToWaitForSomeCount(): void\n    {\n        $this->expectException(AggregateException::class);\n        $this->expectExceptionMessage('Not enough promises to fulfill count');\n\n        $a = new Promise(function () use (&$a): void { $a->resolve('a'); });\n        $d = P\\Utils::some(2, [$a]);\n        $d->wait();\n    }\n\n    public function testThrowsIfResolvedWithoutCountTotalResults(): void\n    {\n        $this->expectException(AggregateException::class);\n        $this->expectExceptionMessage('Not enough promises to fulfill count');\n\n        $a = new Promise();\n        $b = new Promise();\n        $d = P\\Utils::some(3, [$a, $b]);\n        $a->resolve('a');\n        $b->resolve('b');\n        $d->wait();\n    }\n\n    public function testAnyReturnsFirstMatch(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $c = P\\Utils::any([$a, $b]);\n        $b->resolve('b');\n        $a->resolve('a');\n        $c->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame('b', $result);\n    }\n\n    public function testSettleFulfillsWithFulfilledAndRejected(): void\n    {\n        $a = new Promise();\n        $b = new Promise();\n        $c = new Promise();\n        $d = P\\Utils::settle([$a, $b, $c]);\n        $b->resolve('b');\n        $c->resolve('c');\n        $a->reject('a');\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::fulfilled($d));\n        $d->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame([\n            ['state' => 'rejected', 'reason' => 'a'],\n            ['state' => 'fulfilled', 'value' => 'b'],\n            ['state' => 'fulfilled', 'value' => 'c'],\n        ], $result);\n    }\n\n    public function testCanInspectFulfilledPromise(): void\n    {\n        $p = new FulfilledPromise('foo');\n        $this->assertSame([\n            'state' => 'fulfilled',\n            'value' => 'foo',\n        ], P\\Utils::inspect($p));\n    }\n\n    public function testCanInspectRejectedPromise(): void\n    {\n        $p = new RejectedPromise('foo');\n        $this->assertSame([\n            'state' => 'rejected',\n            'reason' => 'foo',\n        ], P\\Utils::inspect($p));\n    }\n\n    public function testCanInspectRejectedPromiseWithNormalException(): void\n    {\n        $e = new \\Exception('foo');\n        $p = new RejectedPromise($e);\n        $this->assertSame([\n            'state' => 'rejected',\n            'reason' => $e,\n        ], P\\Utils::inspect($p));\n    }\n\n    public function testReturnsTrampoline(): void\n    {\n        $this->assertInstanceOf(TaskQueue::class, P\\Utils::queue());\n        $this->assertSame(P\\Utils::queue(), P\\Utils::queue());\n    }\n\n    public function testCanScheduleThunk(): void\n    {\n        $tramp = P\\Utils::queue();\n        $promise = P\\Utils::task(function () { return 'Hi!'; });\n        $c = null;\n        $promise->then(function ($v) use (&$c): void { $c = $v; });\n        $this->assertNull($c);\n        $tramp->run();\n        $this->assertSame('Hi!', $c);\n    }\n\n    public function testCanScheduleThunkWithRejection(): void\n    {\n        $tramp = P\\Utils::queue();\n        $promise = P\\Utils::task(function (): void { throw new \\Exception('Hi!'); });\n        $c = null;\n        $promise->otherwise(function ($v) use (&$c): void { $c = $v; });\n        $this->assertNull($c);\n        $tramp->run();\n        $this->assertSame('Hi!', $c->getMessage());\n    }\n\n    public function testCanScheduleThunkWithWait(): void\n    {\n        $tramp = P\\Utils::queue();\n        $promise = P\\Utils::task(function () { return 'a'; });\n        $this->assertSame('a', $promise->wait());\n        $tramp->run();\n    }\n\n    public function testYieldsFromCoroutine(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = P\\Coroutine::of(function () {\n            $value = (yield new FulfilledPromise('a'));\n            yield $value.'b';\n        });\n        $promise->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame('ab', $result);\n    }\n\n    public function testCanCatchExceptionsInCoroutine(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = P\\Coroutine::of(function () {\n            try {\n                yield new RejectedPromise('a');\n                $this->fail('Should have thrown into the coroutine!');\n            } catch (RejectionException $e) {\n                $value = (yield new FulfilledPromise($e->getReason()));\n                yield $value.'b';\n            }\n        });\n        $promise->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::fulfilled($promise));\n        $this->assertSame('ab', $result);\n    }\n\n    /**\n     * @dataProvider rejectsParentExceptionProvider\n     */\n    public function testRejectsParentExceptionWhenException(PromiseInterface $promise): void\n    {\n        $promise->then(\n            function (): void { $this->fail(); },\n            function ($reason) use (&$result): void { $result = $reason; }\n        );\n        P\\Utils::queue()->run();\n        $this->assertInstanceOf(\\Exception::class, $result);\n        $this->assertSame('a', $result->getMessage());\n    }\n\n    public static function rejectsParentExceptionProvider()\n    {\n        return [\n            [P\\Coroutine::of(function () {\n                yield new FulfilledPromise(0);\n                throw new \\Exception('a');\n            })],\n            [P\\Coroutine::of(function () {\n                throw new \\Exception('a');\n                yield new FulfilledPromise(0);\n            })],\n        ];\n    }\n\n    public function testCanRejectFromRejectionCallback(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = P\\Coroutine::of(function () {\n            yield new FulfilledPromise(0);\n            yield new RejectedPromise('no!');\n        });\n        $promise->then(\n            function (): void { $this->fail(); },\n            function ($reason) use (&$result): void { $result = $reason; }\n        );\n        P\\Utils::queue()->run();\n        $this->assertInstanceOf(RejectionException::class, $result);\n        $this->assertSame('no!', $result->getReason());\n    }\n\n    public function testCanAsyncReject(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $rej = new Promise();\n        $promise = P\\Coroutine::of(function () use ($rej) {\n            yield new FulfilledPromise(0);\n            yield $rej;\n        });\n        $promise->then(\n            function (): void { $this->fail(); },\n            function ($reason) use (&$result): void { $result = $reason; }\n        );\n        $rej->reject('no!');\n        P\\Utils::queue()->run();\n        $this->assertInstanceOf(RejectionException::class, $result);\n        $this->assertSame('no!', $result->getReason());\n    }\n\n    public function testCanCatchAndThrowOtherException(): void\n    {\n        $promise = P\\Coroutine::of(function () {\n            try {\n                yield new RejectedPromise('a');\n                $this->fail('Should have thrown into the coroutine!');\n            } catch (RejectionException $e) {\n                throw new \\Exception('foo');\n            }\n        });\n        $promise->otherwise(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::rejected($promise));\n        $this->assertStringContainsString('foo', $result->getMessage());\n    }\n\n    public function testCanCatchAndYieldOtherException(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = P\\Coroutine::of(function () {\n            try {\n                yield new RejectedPromise('a');\n                $this->fail('Should have thrown into the coroutine!');\n            } catch (RejectionException $e) {\n                yield new RejectedPromise('foo');\n            }\n        });\n        $promise->otherwise(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertTrue(P\\Is::rejected($promise));\n        $this->assertStringContainsString('foo', $result->getMessage());\n    }\n\n    public function createLotsOfSynchronousPromise()\n    {\n        return P\\Coroutine::of(function () {\n            $value = 0;\n            for ($i = 0; $i < 1000; ++$i) {\n                $value = (yield new FulfilledPromise($i));\n            }\n            yield $value;\n        });\n    }\n\n    public function testLotsOfSynchronousDoesNotBlowStack(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = $this->createLotsOfSynchronousPromise();\n        $promise->then(function ($v) use (&$r): void { $r = $v; });\n        P\\Utils::queue()->run();\n        $this->assertSame(999, $r);\n    }\n\n    public function testLotsOfSynchronousWaitDoesNotBlowStack(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = $this->createLotsOfSynchronousPromise();\n        $promise->then(function ($v) use (&$r): void { $r = $v; });\n        $this->assertSame(999, $promise->wait());\n        $this->assertSame(999, $r);\n    }\n\n    private function createLotsOfFlappingPromise()\n    {\n        return P\\Coroutine::of(function () {\n            $value = 0;\n            for ($i = 0; $i < 1000; ++$i) {\n                try {\n                    if ($i % 2) {\n                        $value = (yield new FulfilledPromise($i));\n                    } else {\n                        $value = (yield new RejectedPromise($i));\n                    }\n                } catch (\\Exception $e) {\n                    $value = (yield new FulfilledPromise($i));\n                }\n            }\n            yield $value;\n        });\n    }\n\n    public function testLotsOfTryCatchingDoesNotBlowStack(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = $this->createLotsOfFlappingPromise();\n        $promise->then(function ($v) use (&$r): void { $r = $v; });\n        P\\Utils::queue()->run();\n        $this->assertSame(999, $r);\n    }\n\n    public function testLotsOfTryCatchingWaitingDoesNotBlowStack(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promise = $this->createLotsOfFlappingPromise();\n        $promise->then(function ($v) use (&$r): void { $r = $v; });\n        $this->assertSame(999, $promise->wait());\n        $this->assertSame(999, $r);\n    }\n\n    public function testAsyncPromisesWithCorrectlyYieldedValues(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promises = [\n            new Promise(),\n            new Promise(),\n            new Promise(),\n        ];\n\n        eval('\n        $promise = \\GuzzleHttp\\Promise\\Coroutine::of(function () use ($promises) {\n            $value = null;\n            $this->assertSame(\\'skip\\', (yield new \\GuzzleHttp\\Promise\\FulfilledPromise(\\'skip\\')));\n            foreach ($promises as $idx => $p) {\n                $value = (yield $p);\n                $this->assertSame($idx, $value);\n                $this->assertSame(\\'skip\\', (yield new \\GuzzleHttp\\Promise\\FulfilledPromise(\\'skip\\')));\n            }\n            $this->assertSame(\\'skip\\', (yield new \\GuzzleHttp\\Promise\\FulfilledPromise(\\'skip\\')));\n            yield $value;\n        });\n');\n\n        $promises[0]->resolve(0);\n        $promises[1]->resolve(1);\n        $promises[2]->resolve(2);\n\n        $promise->then(function ($v) use (&$r): void { $r = $v; });\n        P\\Utils::queue()->run();\n        $this->assertSame(2, $r);\n    }\n\n    public function testYieldFinalWaitablePromise(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $p1 = new Promise(function () use (&$p1): void {\n            $p1->resolve('skip me');\n        });\n        $p2 = new Promise(function () use (&$p2): void {\n            $p2->resolve('hello!');\n        });\n        $co = P\\Coroutine::of(function () use ($p1, $p2) {\n            yield $p1;\n            yield $p2;\n        });\n        P\\Utils::queue()->run();\n        $this->assertSame('hello!', $co->wait());\n    }\n\n    public function testCanYieldFinalPendingPromise(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $p1 = new Promise();\n        $p2 = new Promise();\n        $co = P\\Coroutine::of(function () use ($p1, $p2) {\n            yield $p1;\n            yield $p2;\n        });\n        $p1->resolve('a');\n        $p2->resolve('b');\n        $co->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame('b', $result);\n    }\n\n    public function testCanNestYieldsAndFailures(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $p1 = new Promise();\n        $p2 = new Promise();\n        $p3 = new Promise();\n        $p4 = new Promise();\n        $p5 = new Promise();\n        $co = P\\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5) {\n            try {\n                yield $p1;\n            } catch (\\Exception $e) {\n                yield $p2;\n                try {\n                    yield $p3;\n                    yield $p4;\n                } catch (\\Exception $e) {\n                    yield $p5;\n                }\n            }\n        });\n        $p1->reject('a');\n        $p2->resolve('b');\n        $p3->resolve('c');\n        $p4->reject('d');\n        $p5->resolve('e');\n        $co->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame('e', $result);\n    }\n\n    public function testCanYieldErrorsAndSuccessesWithoutRecursion(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $promises = [];\n        for ($i = 0; $i < 20; ++$i) {\n            $promises[] = new Promise();\n        }\n\n        $co = P\\Coroutine::of(function () use ($promises) {\n            for ($i = 0; $i < 20; $i += 4) {\n                try {\n                    yield $promises[$i];\n                    yield $promises[$i + 1];\n                } catch (\\Exception $e) {\n                    yield $promises[$i + 2];\n                    yield $promises[$i + 3];\n                }\n            }\n        });\n\n        for ($i = 0; $i < 20; $i += 4) {\n            $promises[$i]->resolve($i);\n            $promises[$i + 1]->reject($i + 1);\n            $promises[$i + 2]->resolve($i + 2);\n            $promises[$i + 3]->resolve($i + 3);\n        }\n\n        $co->then(function ($value) use (&$result): void { $result = $value; });\n        P\\Utils::queue()->run();\n        $this->assertSame(19, $result);\n    }\n\n    public function testCanWaitOnPromiseAfterFulfilled(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $f = function () {\n            static $i = 0;\n            ++$i;\n\n            return $p = new Promise(function () use (&$p, $i): void {\n                $p->resolve($i.'-bar');\n            });\n        };\n\n        $promises = [];\n        for ($i = 0; $i < 20; ++$i) {\n            $promises[] = $f();\n        }\n\n        $p = P\\Coroutine::of(function () use ($promises) {\n            yield new FulfilledPromise('foo!');\n            foreach ($promises as $promise) {\n                yield $promise;\n            }\n        });\n\n        $this->assertSame('20-bar', $p->wait());\n    }\n\n    public function testCanWaitOnErroredPromises(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $p1 = new Promise(function () use (&$p1): void { $p1->reject('a'); });\n        $p2 = new Promise(function () use (&$p2): void { $p2->resolve('b'); });\n        $p3 = new Promise(function () use (&$p3): void { $p3->resolve('c'); });\n        $p4 = new Promise(function () use (&$p4): void { $p4->reject('d'); });\n        $p5 = new Promise(function () use (&$p5): void { $p5->resolve('e'); });\n        $p6 = new Promise(function () use (&$p6): void { $p6->reject('f'); });\n\n        $co = P\\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5, $p6) {\n            try {\n                yield $p1;\n            } catch (\\Exception $e) {\n                yield $p2;\n                try {\n                    yield $p3;\n                    yield $p4;\n                } catch (\\Exception $e) {\n                    yield $p5;\n                    yield $p6;\n                }\n            }\n        });\n\n        $res = P\\Utils::inspect($co);\n        $this->assertSame('f', $res['reason']);\n    }\n\n    public function testCoroutineOtherwiseIntegrationTest(): void\n    {\n        if (defined('HHVM_VERSION')) {\n            $this->markTestIncomplete('Broken on HHVM.');\n        }\n\n        $a = new Promise();\n        $b = new Promise();\n        $promise = P\\Coroutine::of(function () use ($a, $b) {\n            // Execute the pool of commands concurrently, and process errors.\n            yield $a;\n            yield $b;\n        })->otherwise(function (\\Exception $e): void {\n            // Throw errors from the operations as a specific Multipart error.\n            throw new \\OutOfBoundsException('a', 0, $e);\n        });\n        $a->resolve('a');\n        $b->reject('b');\n        $reason = P\\Utils::inspect($promise)['reason'];\n        $this->assertInstanceOf(\\OutOfBoundsException::class, $reason);\n        $this->assertInstanceOf(RejectionException::class, $reason->getPrevious());\n    }\n\n    public function testCanManuallySettleTaskQueueGeneratedPromises(): void\n    {\n        $p1 = P\\Utils::task(function () { return 'a'; });\n        $p2 = P\\Utils::task(function () { return 'b'; });\n        $p3 = P\\Utils::task(function () { return 'c'; });\n\n        $p1->cancel();\n        $p2->resolve('b2');\n\n        $results = P\\Utils::inspectAll([$p1, $p2, $p3]);\n\n        $this->assertSame([\n            ['state' => 'rejected', 'reason' => 'Promise has been cancelled'],\n            ['state' => 'fulfilled', 'value' => 'b2'],\n            ['state' => 'fulfilled', 'value' => 'c'],\n        ], $results);\n    }\n}\n"
  },
  {
    "path": "vendor-bin/php-cs-fixer/composer.json",
    "content": "{\n    \"require\": {\n        \"php\": \"^7.4\",\n        \"friendsofphp/php-cs-fixer\": \"3.94.2\"\n    },\n    \"config\": {\n        \"preferred-install\": \"dist\"\n    }\n}\n"
  },
  {
    "path": "vendor-bin/phpstan/composer.json",
    "content": "{\n    \"require\": {\n        \"php\": \"^8.2\",\n        \"phpstan/phpstan\": \"2.1.40\",\n        \"phpstan/phpstan-deprecation-rules\": \"2.0.3\"\n    },\n    \"config\": {\n        \"preferred-install\": \"dist\"\n    }\n}\n"
  }
]