<commit_before><?php
/**
 * Go! OOP&AOP PHP framework
 *
 * @copyright     Copyright 2012, <NAME> <<EMAIL>>
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

namespace Go\Instrument\Transformer;

/**
 * Replace magic directory and file constants in the source code
 *
 * @package go
 * @subpackage instrument
 */
class MagicConstantTransformer implements SourceTransformer
{

    /**
     * Root path of application
     *
     * @var string
     */
    protected static $rootPath = '';

    /**
     * Path to rewrite to (cache directory)
     *
     * @var string
     */
    protected static $rewriteToPath = '';

    /**
     * Class constructor
     *
     * @param array $options Configuration options from kernel
     */
    public function __construct(array $options)
    {
        self::$rootPath = realpath($options['appDir']);
        $rewriteToPath  = $options['cacheDir'];
        if ($rewriteToPath) {
            self::$rewriteToPath = realpath($rewriteToPath);
        }
    }

    /**
     * This method may transform the supplied source and return a new replacement for it
     *
     * @param StreamMetaData $metadata Metadata for source
     * @return void
     */
    public function transform(StreamMetaData $metadata)
    {
        // Make the job only when we use cache directory
        if (!self::$rewriteToPath) {
            return;
        }

        $hasReflectionFilename = strpos($metadata->source, 'getFileName') !== false;
        $notHasMagicConsts     = (strpos($metadata->source, '__DIR__') === false) && (strpos($metadata->source, '__FILE__') === false);

        if ($notHasMagicConsts && !$hasReflectionFilename) {
            return;
        }

        // Resolve magic constants
        if (!$notHasMagicConsts) {
            $originalUri = $metadata->getResourceUri();

            $metadata->source = strtr(
                $metadata->source,
                array(
                    '__DIR__'  => "'" . dirname($originalUri) . "'",
                    '__FILE__' => "'" . $originalUri . "'",
                )
            );
        }

        if ($hasReflectionFilename) {
            // TODO: need to make more reliable solution
            $metadata->source = preg_replace(
                '/\$([\w\$\-\>\:\(\)]*?getFileName\(\))/S',
                '\\' . __CLASS__ . '::resolveFileName(\$\1)',
                $metadata->source
            );
        }
    }

    /**
     * Resolves file name from the cache directory to the real application root dir
     *
     * @param string $fileName Absolute file name
     *
     * @return string Resolved file name
     */
    public static function resolveFileName($fileName)
    {
        return str_replace(self::$rewriteToPath, self::$rootPath, $fileName);
    }
}<commit_msg>Use tokenizer to replace only magic constants in the source code #34<commit_after><?php
/**
 * Go! OOP&AOP PHP framework
 *
 * @copyright     Copyright 2012, <NAME> <<EMAIL>>
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

namespace Go\Instrument\Transformer;

/**
 * Replace magic directory and file constants in the source code
 *
 * @package go
 * @subpackage instrument
 */
class MagicConstantTransformer implements SourceTransformer
{

    /**
     * Root path of application
     *
     * @var string
     */
    protected static $rootPath = '';

    /**
     * Path to rewrite to (cache directory)
     *
     * @var string
     */
    protected static $rewriteToPath = '';

    /**
     * Class constructor
     *
     * @param array $options Configuration options from kernel
     */
    public function __construct(array $options)
    {
        self::$rootPath = realpath($options['appDir']);
        $rewriteToPath  = $options['cacheDir'];
        if ($rewriteToPath) {
            self::$rewriteToPath = realpath($rewriteToPath);
        }
    }

    /**
     * This method may transform the supplied source and return a new replacement for it
     *
     * @param StreamMetaData $metadata Metadata for source
     * @return void
     */
    public function transform(StreamMetaData $metadata)
    {
        // Make the job only when we use cache directory
        if (!self::$rewriteToPath) {
            return;
        }

        $hasReflectionFilename = strpos($metadata->source, 'getFileName') !== false;
        $hasMagicConstants     = (strpos($metadata->source, '__DIR__') !== false) ||
            (strpos($metadata->source, '__FILE__') !== false);

        if (!$hasMagicConstants && !$hasReflectionFilename) {
            return;
        }

        // Resolve magic constants
        if ($hasMagicConstants) {
            $this->replaceMagicConstants($metadata);
        }

        if ($hasReflectionFilename) {
            // TODO: need to make more reliable solution
            $metadata->source = preg_replace(
                '/\$([\w\$\-\>\:\(\)]*?getFileName\(\))/S',
                '\\' . __CLASS__ . '::resolveFileName(\$\1)',
                $metadata->source
            );
        }
    }

    /**
     * Resolves file name from the cache directory to the real application root dir
     *
     * @param string $fileName Absolute file name
     *
     * @return string Resolved file name
     */
    public static function resolveFileName($fileName)
    {
        return str_replace(self::$rewriteToPath, self::$rootPath, $fileName);
    }

    /**
     * Replace only magic constants in the code
     *
     * @param StreamMetaData $metadata
     */
    private function replaceMagicConstants(StreamMetaData $metadata)
    {
        $originalUri = $metadata->getResourceUri();
        $replacement = array(
            T_FILE => $originalUri,
            T_DIR  => dirname($originalUri)
        );
        $tokenStream = token_get_all($metadata->source);

        $transformedSource = '';
        foreach ($tokenStream as $token) {
            list ($token, $value) = (array) $token + array(1 => $token);
            if (isset($replacement[$token])) {
                $value = "'" . $replacement[$token] . "'";
            }
            $transformedSource .= $value;
        }
        $metadata->source = $transformedSource;
    }
}
{'fraction_non_alphanumeric': 0.12741002570694088, 'fraction_numerical': 0.002088688946015424, 'mean_word_length': 2.055964653902798, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3433199', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1659, 'n_words': 428}