Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DisplayBuilderHelpers
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
380
0.00% covered (danger)
0.00%
0 / 1
 findArrayReplaceSource
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 getFixtureData
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 getFixtureDataFromExtension
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 formatLog
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatTime
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder;
6
7use Drupal\Component\Serialization\Yaml;
8use Drupal\Core\Datetime\DateFormatterInterface;
9use Drupal\Core\Render\Markup;
10use Drupal\Core\StringTranslation\TranslatableMarkup;
11
12/**
13 * Helpers related class for Display builder.
14 */
15class DisplayBuilderHelpers {
16
17  /**
18   * Multi-array search and replace parent.
19   *
20   * @param array $array
21   *   The array to search in.
22   * @param array $search
23   *   The key value to replace.
24   * @param mixed $new_value
25   *   The new value to set.
26   */
27  public static function findArrayReplaceSource(array &$array, array $search, mixed $new_value): void {
28    foreach ($array as $key => $value) {
29      if (\is_array($value) && \is_array($array[$key])) {
30        self::findArrayReplaceSource($array[$key], $search, $new_value);
31      }
32      elseif ([$key => $value] === $search) {
33        $array['source'] = $new_value;
34      }
35    }
36  }
37
38  /**
39   * Load YAML data if found in fixtures folder.
40   *
41   * @param array $filepaths
42   *   The fixture file paths.
43   * @param bool $extension
44   *   (Optional) The filepath include extension. Default FALSE.
45   *
46   * @return array
47   *   The file content.
48   */
49  public static function getFixtureData(array $filepaths, bool $extension = FALSE): array {
50    foreach ($filepaths as $filepath) {
51      if (!$extension) {
52        $filepath = $filepath . '.yml';
53      }
54
55      if (!\file_exists($filepath)) {
56        continue;
57      }
58
59      $content = \file_get_contents($filepath);
60
61      if (!$content) {
62        continue;
63      }
64
65      return Yaml::decode($content);
66    }
67
68    return [];
69  }
70
71  /**
72   * Load YAML data from fixtures folder for current theme.
73   *
74   * @param string $name
75   *   The extension name.
76   * @param string|null $fixture_id
77   *   (Optional) The fixture file name.
78   *
79   * @return array
80   *   The file content.
81   */
82  public static function getFixtureDataFromExtension(string $name, ?string $fixture_id = NULL): array {
83    $path = NULL;
84
85    try {
86      $path = \Drupal::moduleHandler()->getModule($name)->getPath();
87    }
88    // @phpcs:ignore SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired
89    catch (\Throwable $th) {
90    }
91
92    if (!$path) {
93      try {
94        $path = \Drupal::service('theme_handler')->getTheme($name)->getPath();
95      }
96      // @phpcs:ignore SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired
97      catch (\Throwable $th) {
98      }
99    }
100
101    if (!$path) {
102      return [];
103    }
104
105    $filepath = \sprintf('%s/%s/fixtures/%s.yml', DRUPAL_ROOT, $path, $fixture_id);
106
107    if (!\file_exists($filepath)) {
108      return [];
109    }
110
111    return self::getFixtureData([$filepath], TRUE);
112  }
113
114  /**
115   * Format the log.
116   *
117   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $log
118   *   The log to format.
119   *
120   * @return array
121   *   The formatted log.
122   */
123  public static function formatLog(TranslatableMarkup $log): array {
124    return ['#markup' => Markup::create($log->render())];
125  }
126
127  /**
128   * Print the date for humans.
129   *
130   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
131   *   The date formatter service.
132   * @param int $timestamp
133   *   The timestamp integer.
134   *
135   * @return string
136   *   The formatted date.
137   */
138  public static function formatTime(DateFormatterInterface $dateFormatter, int $timestamp): string {
139    $delta = \time() - $timestamp;
140
141    if ($delta < 86400) {
142      return $dateFormatter->format($timestamp, 'custom', 'G:i');
143    }
144
145    return $dateFormatter->format($timestamp, 'short');
146  }
147
148}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

DisplayBuilderHelpers->findArrayReplaceSource
27  public static function findArrayReplaceSource(array &$array, array $search, mixed $new_value): void {
28    foreach ($array as $key => $value) {
28    foreach ($array as $key => $value) {
28    foreach ($array as $key => $value) {
29      if (\is_array($value) && \is_array($array[$key])) {
29      if (\is_array($value) && \is_array($array[$key])) {
29      if (\is_array($value) && \is_array($array[$key])) {
30        self::findArrayReplaceSource($array[$key], $search, $new_value);
32      elseif ([$key => $value] === $search) {
28    foreach ($array as $key => $value) {
29      if (\is_array($value) && \is_array($array[$key])) {
30        self::findArrayReplaceSource($array[$key], $search, $new_value);
31      }
32      elseif ([$key => $value] === $search) {
33        $array['source'] = $new_value;
28    foreach ($array as $key => $value) {
29      if (\is_array($value) && \is_array($array[$key])) {
30        self::findArrayReplaceSource($array[$key], $search, $new_value);
31      }
32      elseif ([$key => $value] === $search) {
33        $array['source'] = $new_value;
34      }
35    }
36  }
DisplayBuilderHelpers->formatLog
123  public static function formatLog(TranslatableMarkup $log): array {
124    return ['#markup' => Markup::create($log->render())];
DisplayBuilderHelpers->formatTime
138  public static function formatTime(DateFormatterInterface $dateFormatter, int $timestamp): string {
139    $delta = \time() - $timestamp;
140
141    if ($delta < 86400) {
142      return $dateFormatter->format($timestamp, 'custom', 'G:i');
145    return $dateFormatter->format($timestamp, 'short');
DisplayBuilderHelpers->getFixtureData
49  public static function getFixtureData(array $filepaths, bool $extension = FALSE): array {
50    foreach ($filepaths as $filepath) {
50    foreach ($filepaths as $filepath) {
51      if (!$extension) {
52        $filepath = $filepath . '.yml';
53      }
54
55      if (!\file_exists($filepath)) {
55      if (!\file_exists($filepath)) {
56        continue;
59      $content = \file_get_contents($filepath);
60
61      if (!$content) {
62        continue;
65      return Yaml::decode($content);
50    foreach ($filepaths as $filepath) {
51      if (!$extension) {
52        $filepath = $filepath . '.yml';
53      }
54
55      if (!\file_exists($filepath)) {
56        continue;
57      }
58
59      $content = \file_get_contents($filepath);
60
61      if (!$content) {
62        continue;
63      }
64
65      return Yaml::decode($content);
66    }
67
68    return [];
DisplayBuilderHelpers->getFixtureDataFromExtension
82  public static function getFixtureDataFromExtension(string $name, ?string $fixture_id = NULL): array {
83    $path = NULL;
84
85    try {
86      $path = \Drupal::moduleHandler()->getModule($name)->getPath();
89    catch (\Throwable $th) {
92    if (!$path) {
94        $path = \Drupal::service('theme_handler')->getTheme($name)->getPath();
97      catch (\Throwable $th) {
101    if (!$path) {
102      return [];
105    $filepath = \sprintf('%s/%s/fixtures/%s.yml', DRUPAL_ROOT, $path, $fixture_id);
106
107    if (!\file_exists($filepath)) {
108      return [];
111    return self::getFixtureData([$filepath], TRUE);