<commit_before><?php
namespace AspectMock\Intercept;
use Go\Instrument\Transformer\StreamMetaData;
use Go\Instrument\Transformer\WeavingTransformer;
use TokenReflection\Exception\FileProcessingException;
use TokenReflection\Broker;
use TokenReflection\Php\ReflectionMethod;
use TokenReflection\Php\ReflectionParameter;
use TokenReflection\ReflectionClass as ParsedClass;
use TokenReflection\ReflectionFileNamespace as ParsedFileNamespace;

class ClosureTransformer extends WeavingTransformer {

    protected $before = " return __amock_around(\$this, __CLASS__, __FUNCTION__, array(%s), false, function(%s)";
    protected $beforeStatic = " return __amock_around(get_called_class(), __CLASS__, __FUNCTION__, array(%s), true, function(%s)";
    protected $after = ");}";

    public function transform(StreamMetaData $metadata)
    {
        $fileName = $metadata->uri;
        if ($this->includePaths) {
            $found = false;
            foreach ($this->includePaths as $includePath) {
                if (strpos($fileName, $includePath) === 0) {
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                return;
            }
        }

        foreach ($this->excludePaths as $excludePath) {
            if (strpos($fileName, $excludePath) === 0) {
                return;
            }
        }

        try {
            $parsedSource = $this->broker->processString($metadata->source, $fileName, true);
        } catch (FileProcessingException $e) {
            throw new \RuntimeException("AspectMock couldn't parse some of files.\n Try to exclude them from parsing list.\n" . $e->getDetail());
        }

        /** @var $namespaces ParsedFileNamespace[] */
        $namespaces = $parsedSource->getNamespaces();
        $dataArray = explode("\n", $metadata->source);

        foreach ($namespaces as $namespace) {

            /** @var $classes ParsedClass[] */
            $classes = $namespace->getClasses();
            foreach ($classes as $class) {

                // Skip interfaces
                if ($class->isInterface()) {
                    continue;
                }

                // Look for aspects
                if (in_array('Go\Aop\Aspect', $class->getInterfaceNames())) {
                    continue;
                }

                $methods = $class->getMethods();
                foreach ($methods as $method) {
                    /** @var $method ReflectionMethod`  **/
                    if ($method->getDeclaringClassName() != $class->getName()) continue;
                    if ($method->isAbstract()) continue;
                     $aroundDefinition = $method->isStatic()
                        ? $this->beforeStatic
                        : $this->before;
                    $reflectedParams = $method->getParameters();

                    $params = [];

                    foreach ($reflectedParams as $reflectedParam) {
                        /** @var $reflectedParam ReflectionParameter  **/
                        $params[] = '$'.$reflectedParam->getName();
                    }
                    $params = implode(", ", $params);
                    $inject = sprintf($aroundDefinition, $params, $params);
                    if (strpos($dataArray[$method->getStartLine()-1],'{')) {
                        $dataArray[$method->getStartLine()-1] .= $inject.' { ';
                    } else {
                        $dataArray[$method->getStartLine()-1] .= '{ '.$inject;
                    }

                    try {

                        $dataArray[$method->getEndLine()-1] .= $this->after;
                    } catch (\Exception $e) {
                        $e;
                    }<commit_msg>if method started on same line<commit_after><?php
namespace AspectMock\Intercept;
use Go\Instrument\Transformer\StreamMetaData;
use Go\Instrument\Transformer\WeavingTransformer;
use TokenReflection\Exception\FileProcessingException;
use TokenReflection\Broker;
use TokenReflection\Php\ReflectionMethod;
use TokenReflection\Php\ReflectionParameter;
use TokenReflection\ReflectionClass as ParsedClass;
use TokenReflection\ReflectionFileNamespace as ParsedFileNamespace;

class ClosureTransformer extends WeavingTransformer {

    protected $before = " return __amock_around(\$this, __CLASS__, __FUNCTION__, array(%s), false, function(%s)";
    protected $beforeStatic = " return __amock_around(get_called_class(), __CLASS__, __FUNCTION__, array(%s), true, function(%s)";
    protected $after = ");}";

    public function transform(StreamMetaData $metadata)
    {
        $fileName = $metadata->uri;
        if ($this->includePaths) {
            $found = false;
            foreach ($this->includePaths as $includePath) {
                if (strpos($fileName, $includePath) === 0) {
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                return;
            }
        }

        foreach ($this->excludePaths as $excludePath) {
            if (strpos($fileName, $excludePath) === 0) {
                return;
            }
        }

        try {
            $parsedSource = $this->broker->processString($metadata->source, $fileName, true);
        } catch (FileProcessingException $e) {
            throw new \RuntimeException("AspectMock couldn't parse some of files.\n Try to exclude them from parsing list.\n" . $e->getDetail());
        }

        /** @var $namespaces ParsedFileNamespace[] */
        $namespaces = $parsedSource->getNamespaces();
        $dataArray = explode("\n", $metadata->source);

        foreach ($namespaces as $namespace) {

            /** @var $classes ParsedClass[] */
            $classes = $namespace->getClasses();
            foreach ($classes as $class) {

                // Skip interfaces
                if ($class->isInterface()) {
                    continue;
                }

                // Look for aspects
                if (in_array('Go\Aop\Aspect', $class->getInterfaceNames())) {
                    continue;
                }

                $methods = $class->getMethods();
                foreach ($methods as $method) {
                    /** @var $method ReflectionMethod`  **/
                    if ($method->getDeclaringClassName() != $class->getName()) continue;
                    if ($method->isAbstract()) continue;
                     $aroundDefinition = $method->isStatic()
                        ? $this->beforeStatic
                        : $this->before;
                    $reflectedParams = $method->getParameters();

                    $params = [];

                    foreach ($reflectedParams as $reflectedParam) {
                        /** @var $reflectedParam ReflectionParameter  **/
                        $params[] = '$'.$reflectedParam->getName();
                    }
                    $params = implode(", ", $params);
                    $inject = sprintf($aroundDefinition, $params, $params);
                    for ($i = $method->getStartLine()-1; $i < $method->getEndLine()-1; $i++) {
                        $pos = strpos($dataArray[$i],'{');
                        if ($pos === null) continue;
                        $dataArray[$i] = substr($dataArray[$i], 0, $pos).$inject.' { '.substr($dataArray[$i], $pos);
                        break;
                    }

                    $dataArray[$method->getEndLine()-1] .= $this->after;
{'fraction_non_alphanumeric': 0.12917445899011487, 'fraction_numerical': 0.0016029922522041143, 'mean_word_length': 1.7176043557168783, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 56, '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': '7142678', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1799, 'n_words': 392}