You are here

protected function DuplicateEntrySniff::findDuplicateInfoFileEntries in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/InfoFiles/DuplicateEntrySniff.php \Drupal\Sniffs\InfoFiles\DuplicateEntrySniff::findDuplicateInfoFileEntries()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/InfoFiles/DuplicateEntrySniff.php \Drupal\Sniffs\InfoFiles\DuplicateEntrySniff::findDuplicateInfoFileEntries()

Parses a Drupal info file and checsk if a key apperas more than once.

Parameters

string $data The contents of the info file to parse:

Return value

array A list of configuration keys that appear more than once.

1 call to DuplicateEntrySniff::findDuplicateInfoFileEntries()
DuplicateEntrySniff::process in coder_sniffer/Drupal/Sniffs/InfoFiles/DuplicateEntrySniff.php
Processes this test, when one of its tokens is encountered.

File

coder_sniffer/Drupal/Sniffs/InfoFiles/DuplicateEntrySniff.php, line 76

Class

DuplicateEntrySniff
Make sure that entries in info files are specified only once.

Namespace

Drupal\Sniffs\InfoFiles

Code

protected function findDuplicateInfoFileEntries($data) {
  $info = array();
  $duplicates = array();
  $constants = get_defined_constants();
  if (preg_match_all('
          @^\\s*                           # Start at the beginning of a line, ignoring leading whitespace
          ((?:
            [^=;\\[\\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
            \\[[^\\[\\]]*\\]                  # unless they are balanced and not nested
          )+?)
          \\s*=\\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
          (?:
            ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
            (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
            ([^\\r\\n]*?)                   # Non-quoted string
          )\\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
          @msx', $data, $matches, PREG_SET_ORDER) !== false) {
    foreach ($matches as $match) {

      // Fetch the key and value string.
      $i = 0;
      foreach (array(
        'key',
        'value1',
        'value2',
        'value3',
      ) as $var) {
        if (isset($match[++$i]) === true) {
          ${$var} = $match[$i];
        }
        else {
          ${$var} = '';
        }
      }
      $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;

      // Parse array syntax.
      $keys = preg_split('/\\]?\\[/', rtrim($key, ']'));
      $last = array_pop($keys);
      $parent =& $info;

      // Create nested arrays.
      foreach ($keys as $key) {
        if ($key === '') {
          $key = count($parent);
        }
        if (isset($parent[$key]) === false || is_array($parent[$key]) === false) {
          $parent[$key] = array();
        }
        $parent =& $parent[$key];
      }

      // Handle PHP constants.
      if (isset($constants[$value]) === true) {
        $value = $constants[$value];
      }

      // Insert actual value.
      if ($last === '') {
        $last = count($parent);
      }
      if (array_key_exists($last, $parent) === true) {
        $duplicates[] = $last;
      }
      $parent[$last] = $value;
    }

    //end foreach
  }

  //end if
  return $duplicates;
}