<commit_before><?php
namespace Thunder\Shortcode;

/**
 * @author <NAME> <<EMAIL>>
 */
final class Parser implements ParserInterface
    {
    /** @var Syntax */
    private $syntax;

    public function __construct(Syntax $syntax = null)
        {
        $this->syntax = $syntax ?: new Syntax();
        }

    public function parse($text)
        {
        $count = preg_match($this->syntax->getSingleShortcodeRegex(), $text, $matches);

        if(!$count)
            {
            $msg = 'Failed to match single shortcode in text "%s"!';
            throw new \RuntimeException(sprintf($msg, $text));
            }

        return new Shortcode(
            trim(isset($matches[4]) ? $matches[4] : $matches[2]),
            isset($matches[5])
                ? $this->parseParameters($matches[5])
                : (isset($matches[3]) ? $this->parseParameters($matches[3]) : array()),
            isset($matches[6]) ? $matches[6] : null
            );
        }

    private function parseParameters($text)
        {
        preg_match_all($this->syntax->getArgumentsRegex(), $text, $argsMatches);

        $return = array();
        foreach($argsMatches[1] as $item)
            {
            $parts = explode($this->syntax->getParameterValueSeparator(), $item, 2);
            $return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null);
            }

        return $return;
        }

    private function parseValue($value)
        {
        return $this->isStringValue(trim($value))
            ? $this->extractStringValue(trim($value))
            : (null === $value ? null : trim($value));
        }

    private function extractStringValue($value)
        {
        $length = strlen($this->syntax->getParameterValueDelimiter());

        return substr($value, $length, -1 * $length);
        }

    private function isStringValue($value)
        {
        return preg_match('/^'.$this->syntax->getParameterValueDelimiter().'/us', $value)
            && preg_match('/'.$this->syntax->getParameterValueDelimiter().'$/us', $value);
        }
    }<commit_msg>refactored extracting parameter values<commit_after><?php
namespace Thunder\Shortcode;

/**
 * @author <NAME> <<EMAIL>>
 */
final class Parser implements ParserInterface
    {
    /** @var Syntax */
    private $syntax;

    public function __construct(Syntax $syntax = null)
        {
        $this->syntax = $syntax ?: new Syntax();
        }

    public function parse($text)
        {
        $count = preg_match($this->syntax->getSingleShortcodeRegex(), $text, $matches);

        if(!$count)
            {
            $msg = 'Failed to match single shortcode in text "%s"!';
            throw new \RuntimeException(sprintf($msg, $text));
            }

        return new Shortcode(
            trim(isset($matches[4]) ? $matches[4] : $matches[2]),
            isset($matches[5])
                ? $this->parseParameters($matches[5])
                : (isset($matches[3]) ? $this->parseParameters($matches[3]) : array()),
            isset($matches[6]) ? $matches[6] : null
            );
        }

    private function parseParameters($text)
        {
        preg_match_all($this->syntax->getArgumentsRegex(), $text, $argsMatches);

        $return = array();
        foreach($argsMatches[1] as $item)
            {
            $parts = explode($this->syntax->getParameterValueSeparator(), $item, 2);
            $return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null);
            }

        return $return;
        }

    private function parseValue($value)
        {
        return null === $value ? null : $this->extractValue(trim($value));
        }

    private function extractValue($value)
        {
        $length = strlen($this->syntax->getParameterValueDelimiter());

        return $this->isDelimitedValue($value) ? substr($value, $length, -1 * $length) : $value;
        }

    private function isDelimitedValue($value)
        {
        return preg_match('/^'.$this->syntax->getParameterValueDelimiter().'/us', $value)
            && preg_match('/'.$this->syntax->getParameterValueDelimiter().'$/us', $value);
        }
    }
{'fraction_non_alphanumeric': 0.1611897337491005, 'fraction_numerical': 0.007195970256656272, 'mean_word_length': 2.268025078369906, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '1249922', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1164, 'n_words': 219}