<commit_before><?php
/**
 * FrontMatterParser.php
 *
 * Created By: jonathan
 * Date: 23/09/2017
 * Time: 00:34
 */

namespace Stati\Parser;

use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Yaml\Yaml;

class FrontMatterParser
{
    public static function parse($text)
    {
        $split = preg_split("/[\n]*[-]{3}/", $text, 2, PREG_SPLIT_NO_EMPTY);

        if (count($split) > 1) {
            try {
                // Using symfony's YAML parser
                // we can use trim here because we only remove white space
                // at the beginning (first should not have any) and at the end (insignificant)
                return Yaml::parse(trim($split[0]));
            } catch (InvalidArgumentException $e) {
                // This is not YAML
                return [];
            }
        }
        return [];
    }
}<commit_msg>Return empty array on ParseException<commit_after><?php
/**
 * FrontMatterParser.php
 *
 * Created By: jonathan
 * Date: 23/09/2017
 * Time: 00:34
 */

namespace Stati\Parser;

use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

class FrontMatterParser
{
    public static function parse($text)
    {
        $split = preg_split("/[\n]*[-]{3}/", $text, 2, PREG_SPLIT_NO_EMPTY);

        if (count($split) > 1) {
            try {
                // Using symfony's YAML parser
                // we can use trim here because we only remove white space
                // at the beginning (first should not have any) and at the end (insignificant)
                $val = trim($split[0]);
                if (strlen($val) === 0) {
                    return [];
                }
                return Yaml::parse($val);
            } catch (InvalidArgumentException $e) {
                // This is not YAML
                return [];
            } catch (ParseException $err) {
                return [];
            }
        }
        return [];
    }
}
{'fraction_non_alphanumeric': 0.11731581417175035, 'fraction_numerical': 0.015485687470671047, 'mean_word_length': 1.8694481830417227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1342883', 'n_tokens_mistral': 646, 'n_tokens_neox': 595, 'n_words': 158}