GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 8.x-2.x ( 80514e...5e8aa5 )
by Devin
04:03
created

IniEncoder::doEncode()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Acquia\DF;
4
5
/**
6
 * Contains methods for parsing and dumping data in the legacy info file format.
7
 */
8
class IniEncoder {
9
10
  /**
11
   * Serializes an array to legacy make format.
12
   *
13
   * @param array $input
14
   *   The data to serialize.
15
   *
16
   * @return string
17
   *   The serialized data.
18
   */
19
  public function encode(array $input) {
20
    return implode("\n", $this->doEncode($input));
21
  }
22
23
  /**
24
   * Recursively serializes data to legacy make format.
25
   *
26
   * @param array $input
27
   *   The data to serialize.
28
   * @param array $keys
29
   *   The current key path.
30
   *
31
   * @return string[]
32
   *   The serialized data as a flat array of lines.
33
   */
34
  protected function doEncode(array $input, array $keys = []) {
35
    $output = [];
36
37
    foreach ($input as $key => $value) {
38
      $keys[] = $key;
39
40
      if (is_array($value)) {
41
        if ($this->isAssociative($value)) {
42
          $output = array_merge($output, $this->doEncode($value, $keys));
43
        }
44
        else {
45
          foreach ($value as $j) {
46
            $output[] = $this->keysToString($keys) . '[] = ' . $j;
47
          }
48
        }
49
      }
50
      else {
51
        $output[] = $this->keysToString($keys) . ' = ' . $value;
52
      }
53
54
      array_pop($keys);
55
    }
56
57
    return $output;
58
  }
59
60
  /**
61
   * Transforms an key path to a string.
62
   *
63
   * @param string[] $keys
64
   *   The key path.
65
   *
66
   * @return string
67
   *   The flattened key path.
68
   */
69
  protected function keysToString(array $keys) {
70
    $head = array_shift($keys);
71
    if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
72
      return $head . '[' . implode('][', $keys) . ']';
73
    }
74
    else {
75
      return $head;
76
    }
77
  }
78
79
  /**
80
   * Tests if an array is associative.
81
   *
82
   * @param array $input
83
   *   The array to test.
84
   *
85
   * @return bool
86
   *   Whether or not the array has non-numeric keys.
87
   */
88
  protected function isAssociative(array $input) {
89
    $keys = implode('', array_keys($input));
90
    return !is_numeric($keys);
91
  }
92
93
  /**
94
   * Parses data in Drupal's .info format.
95
   *
96
   * @see https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_parse_info_format/7.x
97
   *
98
   * @param string $data
99
   *   A string to parse.
100
   *
101
   * @return array
102
   *   The parsed data.
103
   */
104
  public function parse($data) {
105
    $info = array();
106
107
    if (preg_match_all('
108
      @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
109
      ((?:
110
        [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
111
        \[[^\[\]]*\]                  # unless they are balanced and not nested
112
      )+?)
113
      \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
114
      (?:
115
        ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
116
        (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
117
        ([^\r\n]*?)                   # Non-quoted string
118
      )\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
119
      @msx', $data, $matches, PREG_SET_ORDER)) {
120
      foreach ($matches as $match) {
121
        // Fetch the key and value string.
122
        $i = 0;
123
        foreach (['key', 'value1', 'value2', 'value3'] as $var) {
124
          $$var = isset($match[++$i]) ? $match[$i] : '';
125
        }
126
        $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
0 ignored issues
show
Bug introduced by
The variable $value1 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value2 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value3 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
127
128
        // Parse array syntax.
129
        $keys = preg_split('/\]?\[/', rtrim($key, ']'));
0 ignored issues
show
Bug introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
130
        $last = array_pop($keys);
131
        $parent = &$info;
132
133
        // Create nested arrays.
134
        foreach ($keys as $key) {
135
          if ($key == '') {
136
            $key = count($parent);
137
          }
138
          if (!isset($parent[$key]) || !is_array($parent[$key])) {
139
            $parent[$key] = [];
140
          }
141
          $parent = &$parent[$key];
142
        }
143
144
        // Handle PHP constants.
145
        if (preg_match('/^\w+$/i', $value) && defined($value)) {
146
          $value = constant($value);
147
        }
148
149
        // Insert actual value.
150
        if ($last == '') {
151
          $last = count($parent);
152
        }
153
        $parent[$last] = $value;
154
      }
155
    }
156
157
    return $info;
158
  }
159
160
}