<commit_before><?php

namespace Technodelight;

use UnexpectedValueException;

class Simplate
{
    protected $content;

    public function __construct($content)
    {
        $this->content = $content;
    }

    public static function fromFile($path)
    {
        if (!is_readable($path)) {
            throw new UnexpectedValueException(sprintf('File %s could not be opened', $path));
        }
        return new self(file_get_contents($path));
    }

    /**
     * @param array $variables key => value pairs
     *
     * @return string
     */
    public function render(array $variables = [])
    {
        return $this->replaceContent($variables, $this->hideDepends($variables, $this->content));
    }

    private function replaceContent(array $variables, $content)
    {
        $keys = array_map(
            function($key) {
                return sprintf('{{ %s }}', $key);
            },
            array_keys($variables)
        );

        return strtr($content, array_combine($keys, array_values($variables)));
    }

    private function hideDepends(array $variables, $content)
    {
        $idx = 0;
        $endIdx = 0;
        $pos = 0;
        $endPos = 0;

        do {
            $idx = strpos($content, '{{ depends');
            if ($idx !== false) {
                $endIdx = strpos($content, '}}', $idx);
                $endIdx-= $idx - 10;

                if ($endIdx < 0) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "A 'depends' tag misses the double curly braces closing at %d",
                            $idx
                        )
                    );
                }

                // get expression contents
                $expression = trim(substr($content, $idx + 10, $endIdx));

                // get end_depends position
                $pos = strpos($content, '{{ end_depends', $endIdx + 2);
                var_dump($pos);
                if ($pos < 0) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "No 'end_depends' found for 'depends' tag started from char %d",<commit_msg>Fix expression extraction<commit_after><?php

namespace Technodelight;

use UnexpectedValueException;

class Simplate
{
    protected $content;

    public function __construct($content)
    {
        $this->content = $content;
    }

    public static function fromFile($path)
    {
        if (!is_readable($path)) {
            throw new UnexpectedValueException(sprintf('File %s could not be opened', $path));
        }
        return new self(file_get_contents($path));
    }

    /**
     * @param array $variables key => value pairs
     *
     * @return string
     */
    public function render(array $variables = [])
    {
        return $this->replaceContent($variables, $this->hideDepends($variables, $this->content));
    }

    private function replaceContent(array $variables, $content)
    {
        $keys = array_map(
            function($key) {
                return sprintf('{{ %s }}', $key);
            },
            array_keys($variables)
        );

        return strtr($content, array_combine($keys, array_values($variables)));
    }

    private function hideDepends(array $variables, $content)
    {
        $idx = 0;
        $endIdx = 0;
        $pos = 0;
        $endPos = 0;

        do {
            $idx = strpos($content, '{{ depends');
            if ($idx !== false) {
                $endIdx = strpos($content, '}}', $idx);

                if ($endIdx < 0) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "A 'depends' tag misses the double curly braces closing at %d",
                            $idx
                        )
                    );
                }

                // get expression contents
                $expression = trim(substr($content, $idx + 10, $endIdx - $idx - 10));

                // get end_depends position
                // buggy
                $pos = strpos($content, '{{ end_depends', $endIdx + 2);
                if ($pos < 0) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "No 'end_depends' found for 'depends' tag started from char %d",
{'fraction_non_alphanumeric': 0.11747126436781609, 'fraction_numerical': 0.005057471264367816, 'mean_word_length': 1.5092272202998847, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5425324', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1114, 'n_words': 288}