<commit_before><?php
/**
 * This file is part of Railt package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace Railt\Reflection\Builder;

use Hoa\Compiler\Llk\TreeNode;
use Railt\Reflection\Base\BaseSchema;
use Railt\Reflection\Builder\Support\Builder;
use Railt\Reflection\Builder\Support\Compilable;
use Railt\Reflection\Contracts\Types\ObjectType;
use Railt\Reflection\Contracts\Types\TypeInterface;

/**
 * Class SchemaBuilder
 */
class SchemaBuilder extends BaseSchema implements Compilable
{
    use Builder;

    /**
     * SchemaBuilder constructor.
     * @param TreeNode $ast
     * @param DocumentBuilder $document
     */
    public function __construct(TreeNode $ast, DocumentBuilder $document)
    {
        $this->bootBuilder($ast, $document);
    }

    /**
     * @param TreeNode $ast
     * @return bool
     * @throws \Railt\Reflection\Exceptions\BuildingException
     */
    public function compile(TreeNode $ast): bool
    {
        $type = null;

        switch ($ast->getId()) {
            case '#Query':
                $this->query = $type = $this->fetchType($ast);
                break;

            case '#Mutation':
                $this->mutation = $type = $this->fetchType($ast);
                break;

            case '#Subscription':
                $this->subscription = $type = $this->fetchType($ast);
                break;
        }

        if ($type === null) {
            $this->throwInvalidAstNodeError($ast);
        }

        return true;
    }

    /**
     * @param TreeNode $ast
     * @return ObjectType|TypeInterface
     * @throws \Railt\Reflection\Exceptions\BuildingException
     */
    private function fetchType(TreeNode $ast): ObjectType
    {
        $field = $ast->getChild(0);

        if ($field->getId() !== '#Type') {
            $this->throwInvalidAstNodeError($field);
        }

        $name = $field->getChild(0)->getValueValue();

        if (! \is_string($name)) {
            $this->throwInvalidAstNodeError($field);
        }

        return $this->getCompiler()->get($name);
    }
}<commit_msg>Improve schema building kernel<commit_after><?php
/**
 * This file is part of Railt package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace Railt\Reflection\Builder;

use Hoa\Compiler\Llk\TreeNode;
use Railt\Reflection\Base\BaseSchema;
use Railt\Reflection\Builder\Support\Builder;
use Railt\Reflection\Builder\Support\Compilable;
use Railt\Reflection\Contracts\Types\ObjectType;
use Railt\Reflection\Contracts\Types\TypeInterface;

/**
 * Class SchemaBuilder
 */
class SchemaBuilder extends BaseSchema implements Compilable
{
    use Builder;

    /**
     * SchemaBuilder constructor.
     * @param TreeNode $ast
     * @param DocumentBuilder $document
     * @throws \Railt\Reflection\Exceptions\TypeConflictException
     */
    public function __construct(TreeNode $ast, DocumentBuilder $document)
    {
        $this->bootBuilder($ast, $document);
    }

    /**
     * @param TreeNode $ast
     * @return bool
     * @throws \Railt\Reflection\Exceptions\BuildingException
     */
    public function compile(TreeNode $ast): bool
    {
        switch ($ast->getId()) {
            case '#Query':
                $this->query = $this->fetchType($ast);
                return true;

            case '#Mutation':
                $this->mutation = $this->fetchType($ast);
                return true;

            case '#Subscription':
                $this->subscription = $this->fetchType($ast);
                return true;
        }

        return false;
    }

    /**
     * @param TreeNode $ast
     * @return ObjectType|TypeInterface
     * @throws \Railt\Reflection\Exceptions\BuildingException
     */
    private function fetchType(TreeNode $ast): ObjectType
    {
        /**
         * <code>
         * #Query|#Mutation|#Subscription   *->getChild(0)
         *     #Type                        *->getChild(0)
         *         token(T_NAME, TypeName)  *->getValueValue()
         * </code>
         */
        $name = $ast->getChild(0)->getChild(0)->getValueValue();

        return $this->getCompiler()->get($name);
    }
}
{'fraction_non_alphanumeric': 0.12634716808071544, 'fraction_numerical': 0.0018344416418252694, 'mean_word_length': 2.3020439061317184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, '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': '4731539', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1200, 'n_words': 265}