<commit_before><?php
namespace Rs\Json;

use Rs\Json\Patch\Document;
use Rs\Json\Patch\Operations\Test;
use Rs\Json\Patch\InvalidJsonException;
use Rs\Json\Patch\FailedTestException;
use Seld\JsonLint\JsonParser;
use Seld\JsonLint\ParsingException;

class Patch
{
    const MEDIA_TYPE = "application/json-patch+json";

    /**
     * @var string
     */
    private $targetDocument;

    /**
     * @var Rs\Json\Patch\Document
     */
    private $patchDocument;

    /**
     * @param string $targetDocument
     * @param string $patchDocument
     * @throws Rs\Json\Patch\InvalidJsonException
     */
    public function __construct($targetDocument, $patchDocument)
    {
      if ($this->lintJson($targetDocument)) {
          $this->targetDocument = $targetDocument;
      }

      if ($this->lintJson($patchDocument)) {
          $this->jsonPatchDocument = new Document($patchDocument);
      }
    }

    /**
     * @return string
     */
    public function apply()
    {
        $patchOperations = $this->jsonPatchDocument->getPatchOperations();
        $patchedDocument = $this->targetDocument;
        foreach ($patchOperations as $index => $patchOperation) {
            $targetDocument = $patchOperation->perform($patchedDocument);
            if ($patchOperation instanceof Test && $targetDocument === false) {
                $exceptionMessage = 'Failed on Test PatchOperation at Index : '. $index;
                throw new FailedTestException($exceptionMessage,$index);
            } elseif (!$patchOperation instanceof Test) {
                $patchedDocument = $targetDocument;
            } 
        }
        return $patchedDocument;
    }

    /**
     * @param  mixed $json The Json structure to lint.
     * @return boolean
     * @throws RuntimeException
     * @throws Rs\Json\Patch\InvalidJsonException
     */
    private function lintJson($json)
    {
        if (!class_exists('Seld\\JsonLint\\JsonParser')) {
            throw new \RuntimeException('Unable to lint Json as JsonLint is not installed');
        }

        $parser = new JsonParser;
        $lintResult = $parser->lint($json);

        if ($lintResult instanceof ParsingException) {
            $exceptionMessage = 'Cannot operate on invalid Json. Message: '
                . $lintResult->getMessage();
            throw new InvalidJsonException($exceptionMessage);
        }
        return true;
    }
}
<commit_msg>Coding Standard Fixes, $index != exception code<commit_after><?php
namespace Rs\Json;

use Rs\Json\Patch\Document;
use Rs\Json\Patch\Operations\Test;
use Rs\Json\Patch\InvalidJsonException;
use Rs\Json\Patch\FailedTestException;
use Seld\JsonLint\JsonParser;
use Seld\JsonLint\ParsingException;

class Patch
{
    const MEDIA_TYPE = "application/json-patch+json";

    /**
     * @var string
     */
    private $targetDocument;

    /**
     * @var Rs\Json\Patch\Document
     */
    private $patchDocument;

    /**
     * @param string $targetDocument
     * @param string $patchDocument
     * @throws Rs\Json\Patch\InvalidJsonException
     */
    public function __construct($targetDocument, $patchDocument)
    {
      if ($this->lintJson($targetDocument)) {
          $this->targetDocument = $targetDocument;
      }

      if ($this->lintJson($patchDocument)) {
          $this->jsonPatchDocument = new Document($patchDocument);
      }
    }

    /**
     * @return string
     * @throws Rs\Json\Patch\FailedTestException
     */
    public function apply()
    {
        $patchOperations = $this->jsonPatchDocument->getPatchOperations();
        $patchedDocument = $this->targetDocument;
        foreach ($patchOperations as $index => $patchOperation) {
            $targetDocument = $patchOperation->perform($patchedDocument);
            if ($patchOperation instanceof Test && $targetDocument === false) {
                $exceptionMessage = 'Failed on Test PatchOperation at index: ' . $index;
                throw new FailedTestException($exceptionMessage);
            } elseif (!$patchOperation instanceof Test) {
                $patchedDocument = $targetDocument;
            } 
        }
        return $patchedDocument;
    }

    /**
     * @param  mixed $json The Json structure to lint.
     * @return boolean
     * @throws RuntimeException
     * @throws Rs\Json\Patch\InvalidJsonException
     */
    private function lintJson($json)
    {
        if (!class_exists('Seld\\JsonLint\\JsonParser')) {
            throw new \RuntimeException('Unable to lint Json as JsonLint is not installed');
        }

        $parser = new JsonParser;
        $lintResult = $parser->lint($json);

        if ($lintResult instanceof ParsingException) {
            $exceptionMessage = 'Cannot operate on invalid Json. Message: '
                . $lintResult->getMessage();
            throw new InvalidJsonException($exceptionMessage);
        }
        return true;
    }
}

{'fraction_non_alphanumeric': 0.11408163265306122, 'fraction_numerical': 0.0, 'mean_word_length': 2.4417134831460676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '6305594', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1297, 'n_words': 293}