Completed
Push — 1.0.x ( 7f19ee...54cff8 )
by Antonio
02:18
created

PyStringYamlParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
   * Parse YAML contained in a PyString node.
17
   *
18
   * @param \Behat\Gherkin\Node\PyStringNode $node
19
   *    PyString containing text in YAML format.
20
   *
21
   * @return array
22
   *    Parsed YAML.
23
   */
24
  public function parse(PyStringNode $node) {
25
    // Sanitize PyString test by removing initial indentation spaces.
26
    $strings = $node->getStrings();
27
    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...
28
      preg_match('/^(\s+)/', $strings[0], $matches);
29
      $indentation_size = isset($matches[1]) ? strlen($matches[1]) : 0;
30
      foreach ($strings as $key => $string) {
31
        $strings[$key] = substr($string, $indentation_size);
32
      }
33
    }
34
    $raw = implode("\n", $strings);
35
    return Yaml::parse($raw);
36
  }
37
38
}
39