<commit_before><?php

namespace Kibo\Phast\Services\Scripts;

use Kibo\Phast\Exceptions\RuntimeException;
use Kibo\Phast\Services\ProxyBaseService;
use Kibo\Phast\ValueObjects\Resource;

class Service extends ProxyBaseService {

    private $cleanupFunction = <<<EOF
(function (param) {
    var script = document.querySelector('[data-phast-proxied-script="' + param.i + '"]');
    if (script) {
        script.removeAttribute('data-phast-proxied-script');
        script.setAttribute('src', param.s);
    }
})
EOF;

    protected function makeResponse(Resource $resource, array $request) {
        $resource = $this->addCleanupScript($resource, $request);
        $response = parent::makeResponse($resource, $request);
        $response->setHeader('Content-Type', 'application/javascript');
        return $response;
    }

    private function addCleanupScript(Resource $resource, $request) {
        if (empty($request['src'])
            || empty($request['id'])
            || !preg_match('/^[a-z]?[0-9]+$/', $request['id'])
        ) {
            return $resource;
        }

        $param = [
            'i' => $request['id'],
            's' => $request['src']
        ];
        $script = $this->cleanupFunction . '(' . json_encode($param) . ');';

        return $resource->withContent($script . $resource->getContent());
    }

}
<commit_msg>Remove unused use statement<commit_after><?php

namespace Kibo\Phast\Services\Scripts;

use Kibo\Phast\Services\ProxyBaseService;
use Kibo\Phast\ValueObjects\Resource;

class Service extends ProxyBaseService {

    private $cleanupFunction = <<<EOF
(function (param) {
    var script = document.querySelector('[data-phast-proxied-script="' + param.i + '"]');
    if (script) {
        script.removeAttribute('data-phast-proxied-script');
        script.setAttribute('src', param.s);
    }
})
EOF;

    protected function makeResponse(Resource $resource, array $request) {
        $resource = $this->addCleanupScript($resource, $request);
        $response = parent::makeResponse($resource, $request);
        $response->setHeader('Content-Type', 'application/javascript');
        return $response;
    }

    private function addCleanupScript(Resource $resource, $request) {
        if (empty($request['src'])
            || empty($request['id'])
            || !preg_match('/^[a-z]?[0-9]+$/', $request['id'])
        ) {
            return $resource;
        }

        $param = [
            'i' => $request['id'],
            's' => $request['src']
        ];
        $script = $this->cleanupFunction . '(' . json_encode($param) . ');';

        return $resource->withContent($script . $resource->getContent());
    }

}

{'fraction_non_alphanumeric': 0.16940298507462687, 'fraction_numerical': 0.0014925373134328358, 'mean_word_length': 2.9024745269286756, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '5915636', 'n_tokens_mistral': 805, 'n_tokens_neox': 785, 'n_words': 135}