<commit_before><?php

namespace WyriHaximus\React\Cake\Orm;

use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
use Doctrine\Common\Annotations\AnnotationReader;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory;
use WyriHaximus\React\Cake\Orm\Annotations\Async;
use WyriHaximus\React\Cake\Orm\Annotations\Sync;

class AsyncTable
{
    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var string
     */
    protected $tableName;

    /**
     * @var AnnotationReader
     */
    protected $annotationReader;

    /**
     * @var \ReflectionClass
     */
    protected $reflectionClass;

    /**
     * @param Pool $pool
     * @param string $tableName
     */
    public function __construct(Pool $pool, $tableName, $tableClass)
    {
        $this->pool = $pool;
        $this->tableName = $tableName;
        $this->annotationReader = new AnnotationReader();
        $this->reflectionClass = new \ReflectionClass($tableClass);
    }

    public function __call($function, array $arguments = [])
    {
        if (
            $this->returnsQuery($function) ||
            $this->hasMethodAnnotation($function, Async::class) ||
            (
                $this->hasClassAnnotation(Async::class) &&
                $this->hasNoMethodAnnotation($function)
            ) ||
            strpos(strtolower($function), 'find') === 0 ||
            strpos(strtolower($function), 'fetch') === 0 ||
            strpos(strtolower($function), 'retrieve') === 0
        ) {
            return $this->callAsync($function, $arguments);
        }

        return $this->callSync($function, $arguments);
    }

    protected function callSync($function, array $arguments = [])
    {
        return \React\Promise\resolve(
            call_user_func_array(
                [
                    TableRegistry::get($this->tableName),
                    $function
                ],
                $arguments
            )
        );
    }

    protected function callAsync($function, array $arguments = [])
    {
        $unSerialize = function ($input) {
            if (is_string($input)) {<commit_msg>Save is also async by default<commit_after><?php

namespace WyriHaximus\React\Cake\Orm;

use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
use Doctrine\Common\Annotations\AnnotationReader;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory;
use WyriHaximus\React\Cake\Orm\Annotations\Async;
use WyriHaximus\React\Cake\Orm\Annotations\Sync;

class AsyncTable
{
    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var string
     */
    protected $tableName;

    /**
     * @var AnnotationReader
     */
    protected $annotationReader;

    /**
     * @var \ReflectionClass
     */
    protected $reflectionClass;

    /**
     * @param Pool $pool
     * @param string $tableName
     */
    public function __construct(Pool $pool, $tableName, $tableClass)
    {
        $this->pool = $pool;
        $this->tableName = $tableName;
        $this->annotationReader = new AnnotationReader();
        $this->reflectionClass = new \ReflectionClass($tableClass);
    }

    public function __call($function, array $arguments = [])
    {
        if (
            $this->returnsQuery($function) ||
            $this->hasMethodAnnotation($function, Async::class) ||
            (
                $this->hasClassAnnotation(Async::class) &&
                $this->hasNoMethodAnnotation($function)
            ) ||
            strpos(strtolower($function), 'save') === 0 ||
            strpos(strtolower($function), 'find') === 0 ||
            strpos(strtolower($function), 'fetch') === 0 ||
            strpos(strtolower($function), 'retrieve') === 0
        ) {
            return $this->callAsync($function, $arguments);
        }

        return $this->callSync($function, $arguments);
    }

    protected function callSync($function, array $arguments = [])
    {
        return \React\Promise\resolve(
            call_user_func_array(
                [
                    TableRegistry::get($this->tableName),
                    $function
                ],
                $arguments
            )
        );
    }

    protected function callAsync($function, array $arguments = [])
    {
        $unSerialize = function ($input) {
            if (is_string($input)) {
{'fraction_non_alphanumeric': 0.13236980958935535, 'fraction_numerical': 0.001605872906629961, 'mean_word_length': 2.0856334041047417, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2052131', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1220, 'n_words': 216}