PyStringYamlParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 13 4
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