<commit_before><?php
/**
 * Extension to Flourish fJSON class to provide methods for JSONP support.
 *
 * @copyright Copyright (c) 2011 Poluza.
 * @author <NAME> [au] <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php
 *
 * @package Sutra
 * @link http://www.example.com/
 *
 * @version 1.0
 */
class sJSONP extends fJSON {
  /**
   * Overrides encode method to wrap around the callback received.
   *
   * @throws fValidationException If the callback is invalid.
   *
   * @param mixed $data Data to encode.
   * @param string $callback Optional. If not passed, will be retrieved from GET
   *   paramater 'callback'. If no such parameter is found, 'fn' will be used.
   * @return string Encoded JSONP data.
   */
  public static function encode($data, $callback = NULL) {
    if (is_null($callback)) {
      $callback = fRequest::get('callback', 'string', 'fn');
    }

    if (!self::isValidCallback($callback)) {
      throw new fValidationException('Invalid callback "%s" passed.', $callback);
    }

    return $callback.'('.parent::encode($data).');';
  }

  /**
   * Validate a callback name is not a reserved word in JavaScript and does not have
   *   invalid characters.
   *
   * @credit http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
   *
   * @param string $subject The callback name.
   * @return boolean TRUE if the callback can be used, FALSE otherwise.
   */
  private static function isValidCallback($subject) {
    $identifier_syntax = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';

    $reserved_words = array(
      'break',
      'do',
      'instanceof',
      'typeof',
      'case',
      'else',
      'new',
      'var',
      'catch',
      'finally',
      'return',
      'void',
      'continue',
      'for',
      'switch',
      'while',
      'debugger',
      'function',
      'this',
      'with',
      'default',
      'if',
      'throw',
      'delete',
      'in',
      'try',
      'class',
      'enum',
      'extends',
      'super',
      'const',
      'export',
      'import',
      'implements',
      'let',
      'private',
      'public',
      'yield',
      'interface',
      'package',
      'protected',
      'static',
      'null',
      'true',
      'false',
    );

    return preg_match($identifier_syntax, $subject) && !in_array(fUTF8::lower($subject), $reserved_words);
  }

  /**
   * Overrides sendHeader to send a text/javascript response instead.
   *
   * @return void
   */
  public static function sendHeader() {
    header('Content-Type: text/javascript; charset=utf-8');
  }

  /**
   * Force use as a static class.
   *
   * @return sJSONP
   */
  private function __construct() {}
}<commit_msg>Add code coverage ignore annotations<commit_after><?php
/**
 * Extension to Flourish fJSON class to provide methods for JSONP support.
 *
 * @copyright Copyright (c) 2011 Poluza.
 * @author <NAME> [au] <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php
 *
 * @package Sutra
 * @link http://www.example.com/
 *
 * @version 1.0
 */
class sJSONP extends fJSON {
  /**
   * Overrides encode method to wrap around the callback received.
   *
   * @throws fValidationException If the callback is invalid.
   *
   * @param mixed $data Data to encode.
   * @param string $callback Optional. If not passed, will be retrieved from GET
   *   paramater 'callback'. If no such parameter is found, 'fn' will be used.
   * @return string Encoded JSONP data.
   */
  public static function encode($data, $callback = NULL) {
    if (is_null($callback)) {
      $callback = fRequest::get('callback', 'string', 'fn');
    }

    if (!self::isValidCallback($callback)) {
      throw new fValidationException('Invalid callback "%s" passed.', $callback);
    }

    return $callback.'('.parent::encode($data).');';
  }

  /**
   * Validate a callback name is not a reserved word in JavaScript and does not have
   *   invalid characters.
   *
   * @credit http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
   *
   * @param string $subject The callback name.
   * @return boolean TRUE if the callback can be used, FALSE otherwise.
   */
  private static function isValidCallback($subject) {
    $identifier_syntax = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';

    $reserved_words = array(
      'break',
      'do',
      'instanceof',
      'typeof',
      'case',
      'else',
      'new',
      'var',
      'catch',
      'finally',
      'return',
      'void',
      'continue',
      'for',
      'switch',
      'while',
      'debugger',
      'function',
      'this',
      'with',
      'default',
      'if',
      'throw',
      'delete',
      'in',
      'try',
      'class',
      'enum',
      'extends',
      'super',
      'const',
      'export',
      'import',
      'implements',
      'let',
      'private',
      'public',
      'yield',
      'interface',
      'package',
      'protected',
      'static',
      'null',
      'true',
      'false',
    );

    return preg_match($identifier_syntax, $subject) && !in_array(fUTF8::lower($subject), $reserved_words);
  }

  // @codeCoverageIgnoreStart
  /**
   * Overrides sendHeader to send a text/javascript response instead.
   *
   * @return void
   */
  public static function sendHeader() {
    header('Content-Type: text/javascript; charset=utf-8');
  }
  // @codeCoverageIgnoreEnd

  // @codeCoverageIgnoreStart
  /**
   * Force use as a static class.
   *
   * @return sJSONP
   */
  private function __construct() {}
  // @codeCoverageIgnoreEnd
}
{'fraction_non_alphanumeric': 0.15706435029250132, 'fraction_numerical': 0.007800035454706612, 'mean_word_length': 2.459227467811159, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2178969', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1803, 'n_words': 466}