<commit_before><?php

namespace Drubo\Config;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * Schema class for drubo configuration files.
 */
class ConfigSchema implements ConfigurationInterface {

  /**
   * Create and return configuration schema node.
   *
   * @param $name
   *   The name of the node.
   * @param string $type
   *   The type of the node.
   * @param NodeBuilder|null $builder
   *   A custom node builder instance.
   *
   * @return ArrayNodeDefinition|NodeDefinition
   *   The node (as an ArrayNodeDefinition when the type is 'array').
   */
  protected function createNode($name, $type = 'array', NodeBuilder $builder = null) {
    $treebuilder = new TreeBuilder();

    return $treebuilder->root($name, $type, $builder);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigTreeBuilder() {
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('config');

    $rootNode
      ->children()
        ->append($this->nodeDocroot())
        ->append($this->nodeDrubo())
        ->append($this->nodeDrupal())
        ->append($this->nodeDrupalConsole())
        ->append($this->nodeDrush())
      ->end();

    return $treeBuilder;
  }

  /**
   * Create 'docroot' node.
   *
   * @return ArrayNodeDefinition
   *   The 'docroot' node.
   */
  protected function nodeDocroot() {
    return $this->createNode('docroot')
      ->addDefaultsIfNotSet()
      ->children()
      ->scalarNode('path')->defaultValue('docroot')->end()
      ->end();
  }

  /**
   * Create 'drupal' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drupal' node.
   */
  protected function nodeDrupal() {
    return $this->createNode('drupal')
      ->addDefaultsIfNotSet()
      ->children()
        ->arrayNode('account')
          ->addDefaultsIfNotSet()
          ->children()
            ->scalarNode('mail')->defaultValue('<EMAIL>')->end()
            ->scalarNode('name')->defaultValue('admin')->end()
            ->scalarNode('pass')->defaultValue(NULL)->end()
          ->end()
        ->end()
        ->arrayNode('site')
          ->addDefaultsIfNotSet()
          ->children()
            ->scalarNode('language')->defaultValue('en')->end()
            ->scalarNode('mail')->defaultValue('<EMAIL>')->end()
            ->scalarNode('name')->defaultValue('Drupal')->end()
            ->scalarNode('profile')->defaultValue('standard')->end()
          ->end()
        ->end()
      ->end();
  }

  /**
   * Create 'drubo' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drubo' node.
   */
  protected function nodeDrubo() {
    $commandDefaults = [
      'site:reinstall' => ['disabled' => TRUE],
    ];

    return $this->createNode('drubo')
      ->addDefaultsIfNotSet()
      ->children()
        ->arrayNode('commands')
          ->useAttributeAsKey('name')
          ->defaultValue($commandDefaults)
          ->requiresAtLeastOneElement()
          ->validate()
            ->ifArray()
            ->then(function($v) use ($commandDefaults) {
              // Ensure defaults.
              foreach ($commandDefaults as $commandName => $defaultValue) {
                if (!isset($v[$commandName])) {
                  $v[$commandName] = $defaultValue;
                }
              }

              return $v;
            })
          ->end()
          ->prototype('array')
            ->children()
              ->booleanNode('disabled')->defaultFalse()->end()
            ->end()
          ->end()
        ->end()
      ->end();
  }

  /**
   * Create 'drupalConsole' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drupalConsole' node.
   */
  protected function nodeDrupalConsole() {
    return $this->createNode('drupalconsole')
      ->addDefaultsIfNotSet()
      ->children()
        ->booleanNode('ansi')->defaultTrue()->end()
        ->scalarNode('path')->defaultValue('bin/drupal')->end()
      ->end();
  }

  /**
   * Create 'drush' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drush' node.
   */
  protected function nodeDrush() {
    return $this->createNode('drush')
      ->addDefaultsIfNotSet()
      ->children()
        ->scalarNode('path')->defaultValue('bin/drush')->end()
      ->end();
  }

}<commit_msg>Move validation of „drubo.commads“ config schema key to own method<commit_after><?php

namespace Drubo\Config;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * Schema class for drubo configuration files.
 */
class ConfigSchema implements ConfigurationInterface {

  /**
   * Create and return configuration schema node.
   *
   * @param $name
   *   The name of the node.
   * @param string $type
   *   The type of the node.
   * @param NodeBuilder|null $builder
   *   A custom node builder instance.
   *
   * @return ArrayNodeDefinition|NodeDefinition
   *   The node (as an ArrayNodeDefinition when the type is 'array').
   */
  protected function createNode($name, $type = 'array', NodeBuilder $builder = null) {
    $treebuilder = new TreeBuilder();

    return $treebuilder->root($name, $type, $builder);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigTreeBuilder() {
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('config');

    $rootNode
      ->children()
        ->append($this->nodeDocroot())
        ->append($this->nodeDrubo())
        ->append($this->nodeDrupal())
        ->append($this->nodeDrupalConsole())
        ->append($this->nodeDrush())
      ->end();

    return $treeBuilder;
  }

  /**
   * Create 'docroot' node.
   *
   * @return ArrayNodeDefinition
   *   The 'docroot' node.
   */
  protected function nodeDocroot() {
    return $this->createNode('docroot')
      ->addDefaultsIfNotSet()
      ->children()
      ->scalarNode('path')->defaultValue('docroot')->end()
      ->end();
  }

  /**
   * Create 'drupal' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drupal' node.
   */
  protected function nodeDrupal() {
    return $this->createNode('drupal')
      ->addDefaultsIfNotSet()
      ->children()
        ->arrayNode('account')
          ->addDefaultsIfNotSet()
          ->children()
            ->scalarNode('mail')->defaultValue('<EMAIL>')->end()
            ->scalarNode('name')->defaultValue('admin')->end()
            ->scalarNode('pass')->defaultValue(NULL)->end()
          ->end()
        ->end()
        ->arrayNode('site')
          ->addDefaultsIfNotSet()
          ->children()
            ->scalarNode('language')->defaultValue('en')->end()
            ->scalarNode('mail')->defaultValue('<EMAIL>')->end()
            ->scalarNode('name')->defaultValue('Drupal')->end()
            ->scalarNode('profile')->defaultValue('standard')->end()
          ->end()
        ->end()
      ->end();
  }

  /**
   * Create 'drubo' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drubo' node.
   */
  protected function nodeDrubo() {
    $commandDefaults = [
      'site:reinstall' => ['disabled' => TRUE],
    ];

    return $this->createNode('drubo')
      ->addDefaultsIfNotSet()
      ->children()
        ->arrayNode('commands')
          ->useAttributeAsKey('name')
          ->defaultValue($commandDefaults)
          ->requiresAtLeastOneElement()
          ->validate()
            ->ifArray()
            ->then($this->validateDruboCommands($commandDefaults))
          ->end()
          ->prototype('array')
            ->children()
              ->booleanNode('disabled')->defaultFalse()->end()
            ->end()
          ->end()
        ->end()
      ->end();
  }

  /**
   * Create 'drupalConsole' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drupalConsole' node.
   */
  protected function nodeDrupalConsole() {
    return $this->createNode('drupalconsole')
      ->addDefaultsIfNotSet()
      ->children()
        ->booleanNode('ansi')->defaultTrue()->end()
        ->scalarNode('path')->defaultValue('bin/drupal')->end()
      ->end();
  }

  /**
   * Create 'drush' node.
   *
   * @return ArrayNodeDefinition
   *   The 'drush' node.
   */
  protected function nodeDrush() {
    return $this->createNode('drush')
      ->addDefaultsIfNotSet()
      ->children()
        ->scalarNode('path')->defaultValue('bin/drush')->end()
      ->end();
  }

  /**
   * Validate 'drubo.commands' node.
   *
   * @param array $defaults
   *   A keyed array of default values.
   *
   * @return \Closure
   *   The validation closure.
   */
  protected function validateDruboCommands(array $defaults) {
    return function($v) use ($defaults) {
      // Ensure defaults.
      foreach ($defaults as $commandName => $defaultValue) {
        if (!isset($v[$commandName])) {
          $v[$commandName] = $defaultValue;
        }
      }

      return $v;
    };
  }

}
{'fraction_non_alphanumeric': 0.16520610191496268, 'fraction_numerical': 0.0, 'mean_word_length': 2.4211695040710586, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 200, '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': '2459988', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2639, 'n_words': 496}