<commit_before><?php

/**
 * This file is part of the PHP ST utility.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Zikula\Tools\Converter;

use Zikula\Tools\ConverterAbstract;

/**
 * @author sankar <<EMAIL>>
 */
class IfConverter extends ConverterAbstract
{
    private $alt = array(
        'gt' => '>',
        'lt' => '<',
        'eq' => '==',
        'neq' => '!=',
        'ne' => '!=',
        'not' => '!',
        'mod' => '%',
    );

    public function convert(\SplFileInfo $file, $content)
    {
        // Replace {if }
        $content = $this->replaceIf($content);
        // Replace {elseif }
        $content = $this->replaceElseIf($content);
        // Replace {/if}
        $content = preg_replace('#\{/if\s*\}#', "{% endif %}", $content);
        // Replace {else}
        $content = preg_replace('#\{else\s*\}#', "{% else %}", $content);

        return $content;
    }

    private function replaceIf($content)
    {

        $pattern = "#\{if\b\s*([^{}]+)?\}#i";
        $string = '{%% if %s %%}';

        return $this->replace($pattern, $content, $string);
    }

    private function replace($pattern, $content, $string)
    {
        return preg_replace_callback($pattern, function ($matches) use ($string) {

            $match = $matches[1];
            $search = $matches[0];

            foreach ($this->alt as $key => $value) {
                $match = str_replace(" $key ", " $value ", $match);
            }

            // take care of `!foo`
            $match = preg_replace_callback("/(!)([^=\s]+)/i", function ($m) {
                return "not $m[2]";
            }, $match);

            // take care of bar:foo['hello']
            $match = preg_replace_callback("/(:)([^}\s]+)/i", function ($m) {
                return "($m[2])";
            }, $match);

            // Replace $vars
            $match = $this->replaceVariable($match);

            $string = sprintf($string, $match);

            return str_replace($search, $string, $search);

        }, $content);
    }

    private function replaceVariable($string)
    {
        $pattern = '/\$([\w\.\-\>\[\]]+)/';
        return preg_replace_callback($pattern, function ($matches) {
            // Convert Object to dot
            $matches[1] = str_replace('->', '.', $matches[1]);

            return str_replace($matches[0], $matches[1], $matches[0]);
<commit_msg>correct issue with converting !empty<commit_after><?php

/**
 * This file is part of the PHP ST utility.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Zikula\Tools\Converter;

use Zikula\Tools\ConverterAbstract;

/**
 * @author sankar <<EMAIL>>
 */
class IfConverter extends ConverterAbstract
{
    private $alt = array(
        'gt' => '>',
        'lt' => '<',
        'eq' => '==',
        'neq' => '!=',
        'ne' => '!=',
        'not' => '!',
        'mod' => '%',
    );

    public function convert(\SplFileInfo $file, $content)
    {
        // Replace {if }
        $content = $this->replaceIf($content);
        // Replace {elseif }
        $content = $this->replaceElseIf($content);
        // Replace {/if}
        $content = preg_replace('#\{/if\s*\}#', "{% endif %}", $content);
        // Replace {else}
        $content = preg_replace('#\{else\s*\}#', "{% else %}", $content);

        return $content;
    }

    private function replaceIf($content)
    {

        $pattern = "#\{if\b\s*([^{}]+)?\}#i";
        $string = '{%% if %s %%}';

        return $this->replace($pattern, $content, $string);
    }

    private function replace($pattern, $content, $string)
    {
        return preg_replace_callback($pattern, function ($matches) use ($string) {

            $match = $matches[1];
            $search = $matches[0];

            foreach ($this->alt as $key => $value) {
                $match = str_replace(" $key ", " $value ", $match);
            }

            // take care of `!foo`
            $match = preg_replace_callback("/(!)([^=\s]+)/i", function ($m) {
                return (false === strpos($m[2], 'empty')) ? "not $m[2]" : "!$m[2]";
            }, $match);

            // take care of bar:foo['hello']
            $match = preg_replace_callback("/(:)([^}\s]+)/i", function ($m) {
                return "($m[2])";
            }, $match);

            // Replace $vars
            $match = $this->replaceVariable($match);

            $string = sprintf($string, $match);

            return str_replace($search, $string, $search);

        }, $content);
    }

    private function replaceVariable($string)
    {
        $pattern = '/\$([\w\.\-\>\[\]]+)/';
        return preg_replace_callback($pattern, function ($matches) {
            // Convert Object to dot
            $matches[1] = str_replace('->', '.', $matches[1]);

            return str_replace($matches[0], $matches[1], $matches[0]);

{'fraction_non_alphanumeric': 0.2009132420091324, 'fraction_numerical': 0.0039706174310105225, 'mean_word_length': 2.0023837902264603, 'pattern_counts': {'":': 0, '<': 17, '<?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': '2962445', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1513, 'n_words': 354}