Repository: symfony/symfony
File: src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php (PHP)

Code before:
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Config\Definition\Builder;

use Symfony\Component\Config\Definition\NodeInterface;

/**
 * This is the entry class for building a config tree.
 *
 * @template T of 'variable'|'scalar'|'boolean'|'integer'|'float'|'enum'|'array'
 *
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 */
class TreeBuilder implements NodeParentInterface
{
    protected ?NodeInterface $tree = null;
    protected ?NodeDefinition $root = null;

    /**
     * @param T $type
     */
    public function __construct(string $name, string $type = 'array', ?NodeBuilder $builder = null)
    {
        $builder ??= new NodeBuilder();
        $this->root = $builder->node($name, $type)->setParent($this);
    }

    /**
     * @return NodeDefinition|ArrayNodeDefinition The root node (as an ArrayNodeDefinition when the type is 'array')
     *
     * @return (
     *    T is 'variable' ? VariableNodeDefinition
     *    : T is 'scalar' ? ScalarNodeDefinition
     *    : T is 'boolean' ? BooleanNodeDefinition
     *    : T is 'integer' ? IntegerNodeDefinition
     *    : T is 'float' ? FloatNodeDefinition
     *    : T is 'enum' ? EnumNodeDefinition
     *    : ArrayNodeDefinition
     * )
     */
    public function getRootNode(): NodeDefinition|ArrayNodeDefinition
    {
        return $this->root;
    }

    /**
     * Builds the tree.

Review comment (bug):
Not so happy with this, because technically, it is incorrect, `new TreeBuilder('x')` would result in `TreeBuilder<'variable'|'scalar'|'boolean'|'integer'|'float'|'enum'|'array'>`, so `getRootNode` would return `VariableNodeDefinition|ScalarNodeDefinition|....`, but can't really fix it without a BC break.

However, those specifying the second argument will get better return type, everyone else will still get basically `NodeDefinition|ArrayNodeDefinition` which is same as before.

Code after:
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Config\Definition\Builder;

use Symfony\Component\Config\Definition\NodeInterface;

/**
 * This is the entry class for building a config tree.
 *
 * @template T of 'variable'|'scalar'|'boolean'|'integer'|'float'|'enum'|'array'
 *
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 */
class TreeBuilder implements NodeParentInterface
{
    protected ?NodeInterface $tree = null;
    protected ?NodeDefinition $root = null;

    /**
     * @param T $type
     */
    public function __construct(string $name, string $type = 'array', ?NodeBuilder $builder = null)
    {
        $builder ??= new NodeBuilder();
        $this->root = $builder->node($name, $type)->setParent($this);
    }

    /**
     * @return (
     *    T is 'variable' ? VariableNodeDefinition
     *    : T is 'scalar' ? ScalarNodeDefinition
     *    : T is 'boolean' ? BooleanNodeDefinition
     *    : T is 'integer' ? IntegerNodeDefinition
     *    : T is 'float' ? FloatNodeDefinition
     *    : T is 'enum' ? EnumNodeDefinition
     *    : ArrayNodeDefinition
     * ) The root node (as an ArrayNodeDefinition when the type is 'array')
     */
    public function getRootNode(): NodeDefinition|ArrayNodeDefinition
    {
        return $this->root;
    }

    /**
     * Builds the tree.
     *
     * @throws \RuntimeException