<commit_before><?php

namespace Amp\Internal;

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

class LazyAwaitable implements Awaitable {
    /**
     * @var callable|null
     */
    private $provider;

    /**
     * @var \Interop\Async\Awaitable
     */
    private $awaitable;

    /**
     * @param callable $provider
     */
    public function __construct(callable $provider) {
        $this->provider = $provider;
    }

    /**
     * @return \Interop\Async\Awaitable
     */
    protected function getAwaitable() {
        if (null === $this->awaitable) {
            $provider = $this->provider;
            $this->provider = null;

            try {
                $this->awaitable = $provider();

                if ($this->awaitable instanceof Awaitable) {
                    $this->awaitable = new Success($this->awaitable);
                }
            } catch (\Throwable $exception) {
                $this->awaitable = new Failure($exception);
            } catch (\Exception $exception) {
                $this->awaitable = new Failure($exception);
            }
        }

        return $this->awaitable;
    }

    /**
     * {@inheritdoc}
     */
    public function when(callable $onResolved) {
        $this->getAwaitable()->when($onResolved);
    }
}<commit_msg>Build awaitable in when()<commit_after><?php

namespace Amp\Internal;

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

class LazyAwaitable implements Awaitable {
    /**
     * @var callable|null
     */
    private $provider;

    /**
     * @var \Interop\Async\Awaitable|null
     */
    private $awaitable;

    /**
     * @param callable $provider
     */
    public function __construct(callable $provider) {
        $this->provider = $provider;
    }

    /**
     * {@inheritdoc}
     */
    public function when(callable $onResolved) {
        if (null === $this->awaitable) {
            $provider = $this->provider;
            $this->provider = null;

            try {
                $this->awaitable = $provider();

                if ($this->awaitable instanceof Awaitable) {
                    $this->awaitable = new Success($this->awaitable);
                }
            } catch (\Throwable $exception) {
                $this->awaitable = new Failure($exception);
            } catch (\Exception $exception) {
                $this->awaitable = new Failure($exception);
            }
        }

        $this->awaitable->when($onResolved);
    }
}
{'fraction_non_alphanumeric': 0.13443017656500802, 'fraction_numerical': 0.0, 'mean_word_length': 1.7730812013348165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3842565', 'n_tokens_mistral': 729, 'n_tokens_neox': 718, 'n_words': 140}