Completed
Push — 1.0.x ( 235ffe...0a17d9 )
by Antonio
08:29
created

PyStringYamlParser::parse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Component;
4
5
use Behat\Gherkin\Node\PyStringNode;
6
use Symfony\Component\Yaml\Yaml;
7
8
/**
9
 * Class PyStringYamlParser.
10
 *
11
 * @package NuvoleWeb\Drupal\DrupalExtension\Component
12
 */
13
class PyStringYamlParser {
14
15
  /**
16
   * PyStringNode object.
17
   *
18
   * @var PyStringNode;
19
   */
20
  protected $node;
21
22
  /**
23
   * PyStringYamlParser constructor.
24
   *
25
   * @param \Behat\Gherkin\Node\PyStringNode $node
26
   *    PyString containing text in YAML format.
27
   */
28
  public function __construct(PyStringNode $node) {
29
    $this->node = $node;
30
  }
31
32
  /**
33
   * Parse YAML contained in a PyString node.
34
   *
35
   * @return array
36
   *    Parsed YAML.
37
   */
38
  public function parse() {
39
    // Sanitize PyString test by removing initial indentation spaces.
40
    $strings = $this->node->getStrings();
41
    if ($strings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $strings 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...
42
      preg_match('/^(\s+)/', $strings[0], $matches);
43
      $indentation_size = isset($matches[1]) ? strlen($matches[1]) : 0;
44
      foreach ($strings as $key => $string) {
45
        $strings[$key] = substr($string, $indentation_size);
46
      }
47
    }
48
    $raw = implode("\n", $strings);
49
    return Yaml::parse($raw);
50
  }
51
52
}
53