<commit_before><?php

/*
 * This file is part of the PhpTabs package.
 *
 * Copyright (c) landrok at github.com/landrok
 *
 * For the full copyright and license information, please see
 * <https://github.com/stdtabs/phptabs/blob/master/LICENSE>.
 */

namespace PhpTabs\Component\Importer;

use PhpTabs\Music\EffectBend;
use PhpTabs\Music\EffectTremoloBar;
use PhpTabs\Music\NoteEffect;

class NoteEffectParser extends ParserBase
{
  protected $required = [
        'bend',
        'tremoloBar',
        'harmonic',
        'grace',
        'trill',
        'tremoloPicking',
        'vibrato',
        'deadNote',
        'slide',
        'hammer',
        'ghostNote',
        'accentuatedNote',
        'heavyAccentuatedNote',
        'palmMute',
        'staccato',
        'tapping',
        'slapping',
        'popping',
        'fadeIn',
        'letRing'
  ];

  private $parsers = [
    'harmonic'        => 'Harmonic',
    'grace'           => 'Grace',
    'trill'           => 'Trill',
    'tremoloPicking'  => 'TremoloPicking'
  ];

  private $autoset = [
        'vibrato',
        'deadNote',
        'slide',
        'hammer',
        'ghostNote',
        'accentuatedNote',
        'heavyAccentuatedNote',
        'palmMute',
        'staccato',
        'tapping',
        'slapping',
        'popping',
        'fadeIn',
        'letRing'
  ];

  /**
   * Parse a note effect array
   * 
   * @param  array $data
   */
  public function __construct(array $data)
  {
    $this->checkKeys($data, $this->required);

    $effect = new NoteEffect();

    if ($data['bend'] !== null) {
      $effect->setBend(
        $this->parseEffectPoints($data['bend'], new EffectBend())
      );
    }

    if ($data['tremoloBar'] !== null) {
      $effect->setTremoloBar(
        $this->parseEffectPoints($data['tremoloBar'], new EffectTremoloBar())
      );
    }

    foreach ($this->parsers as $name => $parser) {
      if ($data[$name] !== null) {
        $setter = 'set' . $parser;
        $getter = 'parse' . $parser;
        
        $effect->$setter(
          $this->$getter($data[$name])
        );
      }
    }

    foreach ($this->autoset as $key) {
      if ($data[$key] !== null) {
        $method = 'set' . ucfirst($key);
        $effect->$method($data[$key]);
      }
    }

    $this->item = $effect;
  }
}<commit_msg>[QA] Reduce complexity for NoteEffectParser methods<commit_after><?php

/*
 * This file is part of the PhpTabs package.
 *
 * Copyright (c) landrok at github.com/landrok
 *
 * For the full copyright and license information, please see
 * <https://github.com/stdtabs/phptabs/blob/master/LICENSE>.
 */

namespace PhpTabs\Component\Importer;

use PhpTabs\Music\EffectBend;
use PhpTabs\Music\EffectTremoloBar;
use PhpTabs\Music\NoteEffect;

class NoteEffectParser extends ParserBase
{
  protected $required = [
        'bend',
        'tremoloBar',
        'harmonic',
        'grace',
        'trill',
        'tremoloPicking',
        'vibrato',
        'deadNote',
        'slide',
        'hammer',
        'ghostNote',
        'accentuatedNote',
        'heavyAccentuatedNote',
        'palmMute',
        'staccato',
        'tapping',
        'slapping',
        'popping',
        'fadeIn',
        'letRing'
  ];

  private $parsers = [
    'harmonic'        => 'Harmonic',
    'grace'           => 'Grace',
    'trill'           => 'Trill',
    'tremoloPicking'  => 'TremoloPicking'
  ];

  private $autoset = [
        'vibrato',
        'deadNote',
        'slide',
        'hammer',
        'ghostNote',
        'accentuatedNote',
        'heavyAccentuatedNote',
        'palmMute',
        'staccato',
        'tapping',
        'slapping',
        'popping',
        'fadeIn',
        'letRing'
  ];

  /**
   * Parse a note effect array
   * 
   * @param  array $data
   */
  public function __construct(array $data)
  {
    $this->checkKeys($data, $this->required);

    $this->item = new NoteEffect();

    $this->createBendAndTremolo($data);

    $this->createAttributes($data);
  }

  /**
   * Create bend and tremolo bar values
   *
   * @param array $data
   */
  private function createBendAndTremolo(array $data)
  {
    if ($data['bend'] !== null) {
      $this->item->setBend(
        $this->parseEffectPoints($data['bend'], new EffectBend())
      );
    }

    if ($data['tremoloBar'] !== null) {
      $this->item->setTremoloBar(
        $this->parseEffectPoints($data['tremoloBar'], new EffectTremoloBar())
      );
    }
  }

  /**
   * Create attributes
   *
   * @param array $data
   */
  private function createAttributes(array $data)
  {
    foreach ($this->parsers as $name => $parser) {
      if ($data[$name] !== null) {
        $setter = 'set' . $parser;
        $getter = 'parse' . $parser;
        
        $this->item->$setter(
          $this->$getter($data[$name])
        );
      }
    }

    foreach ($this->autoset as $key) {
      if ($data[$key] !== null) {
        $method = 'set' . ucfirst($key);
        $this->item->$method($data[$key]);
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.1605550049554014, 'fraction_numerical': 0.0, 'mean_word_length': 2.028811524609844, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 2, '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': '6015490', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1683, 'n_words': 315}