Php::run()   B
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 58
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 44
nc 4
nop 0
dl 0
loc 58
rs 7.9715
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\qa\Views;
4
5
use Drupal\qa\Pass;
6
7
/**
8
 * Find views containing PHP code
9
 */
10
class Php extends Views {
11
12
  /**
13
   * {@inheritdoc]
14
   */
15
  public function init() {
16
    $this->package_name = __NAMESPACE__;
17
    $this->title = t('PHP code within views');
18
    $this->description = t('Is there any embedded PHP within views and display definitions ? This is both a security risk and a performance issue.');
19
  }
20
21
  /**
22
   * @param string $area
23
   *   The area (header, footer, empty) being examined.
24
   * @param array $php
25
   *   The array of input formats containing PHP.
26
   * @param \stdClass $display
27
   *   The display being examined.
28
   * @param string $area_name
29
   *   The name of the area
30
   *
31
   * @return array
32
   *   The array of PHP fragments found in the area.
33
   */
34
  protected function checkViews2Php($area, array $php, $display, $area_name) {
35
    $ret = [];
36
    $area_format = $display->display_options[$area_name .'_format']; // Always set
37
    if (in_array($area_format, $php)) {
38
      $ret['text'] = $area;
39
    }
40
    return $ret;
41
  }
42
43
  /**
44
   * @param array $area
45
   *   The area (header, footer, empty) being examined.
46
   * @param array $php
47
   *   The array of input formats containing PHP.
48
   *
49
   * @return array
50
   *   The array of PHP fragments found in the area.
51
   */
52
  protected function checkViews3Php(array $area, array $php) {
53
    $ret = [];
54
    foreach ($area as $field => $field_options) {
55
      if ($field_options['field'] == 'area' && isset($field_options['format']) && in_array($field_options['format'], $php)) {
56
        $ret[$field] = $field_options['content'];
57
      }
58
    }
59
    return $ret;
60
  }
61
62
  /**
63
   * Views 2 had a single string for areas whereas Views 3 has an array for them.
64
   */
65
  public function checkViewPhp($view) {
66
    $php = $this->getPhpFormats();
67
    $areas = ['header', 'footer', 'empty'];
68
    $result = [];
69
70
    foreach ($view->display as $display_name => $display) {
71
      foreach ($areas as $area_name) {
72
        if (!isset($display->display_options[$area_name])) {
73
          continue;
74
        }
75
76
        $area = $display->display_options[$area_name];
77
        $fragments = is_array($area)
78
          ? $this->checkViews3Php($area, $php)
79
          : $this->checkViews2Php($area, $php, $display, $area_name);
80
81
        if (!empty($fragments))  {
82
          $result[$display_name][$area_name] = $fragments;
83
        }
84
      } // foreach header, footer, empty...
85
    } // foreach display
86
87
    $ret = [
88
      'name' => $view->name,
89
      'status' => empty($result),
90
      'result' => $result,
91
    ];
92
    return $ret;
93
  }
94
95
  /**
96
   * Get a list of the ids of input formats containing the PHP eval filter.
97
   *
98
   * @return array
99
   */
100
  protected function getPhpFormats($reset = FALSE) {
101
    static $php = NULL;
102
103
    if (!isset($php) || $reset) {
104
      $formats = filter_formats();
105
      $php = [];
106
      foreach ($formats as $format) {
107
        $filters = filter_list_format($format->id());
0 ignored issues
show
Bug introduced by
The function filter_list_format was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $filters = /** @scrutinizer ignore-call */ filter_list_format($format->id());
Loading history...
108
        foreach ($filters as $filter) {
109
          if ($filter->module == 'php') {
110
            $php[] = $format->id();
111
            break;
112
          }
113
        }
114
      }
115
    }
116
    return $php;
117
  }
118
119
  public function run(): Pass {
120
    $pass = parent::run();
121
    $views = views_get_all_views(TRUE);
0 ignored issues
show
Bug introduced by
The function views_get_all_views was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
    $views = /** @scrutinizer ignore-call */ views_get_all_views(TRUE);
Loading history...
122
    foreach ($views as $view) {
123
      $pass->record($this->checkViewPhp($view));
0 ignored issues
show
Bug introduced by
$this->checkViewPhp($view) of type array<string,array|boolean|mixed> is incompatible with the type Drupal\qa\Result|null expected by parameter $checkResult of Drupal\qa\Pass::record(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
      $pass->record(/** @scrutinizer ignore-type */ $this->checkViewPhp($view));
Loading history...
124
    }
125
    $pass->life->end();
126
127
    if ($pass->status) {
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Drupal\qa\Pass.
Loading history...
128
      $result = format_plural(count($views), '1 view checked, none containing PHP',
0 ignored issues
show
Bug introduced by
The function format_plural was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
      $result = /** @scrutinizer ignore-call */ format_plural(count($views), '1 view checked, none containing PHP',
Loading history...
129
        '@count views checked, none containing PHP', []);
130
    }
131
    else {
132
      $result = format_plural(count($views), '1 view checked and containing PHP',
133
        '@count views checked, @php containing PHP', [
134
          '@php' => count($pass->result),
135
        ]);
136
      $header = [
137
        t('View'),
138
        t('Display'),
139
        t('Area'),
140
        t('Field'),
141
        t('Content'),
142
      ];
143
      $data = [];
144
      foreach ($pass->result as $view_name => $displays) {
145
        $row = [];
146
        $link_title = empty($views[$view_name]->human_name)
147
          ? $view_name
148
          : $views[$view_name]->human_name;
149
        $view_link = l($link_title, "admin/structure/views/view/$view_name/edit");
0 ignored issues
show
Bug introduced by
The function l was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $view_link = /** @scrutinizer ignore-call */ l($link_title, "admin/structure/views/view/$view_name/edit");
Loading history...
150
        $row['view'] = ['data' => $view_link];
151
        foreach ($displays as $display_id => $areas) {
152
          $row['display'] = l($display_id, "admin/structure/views/view/$view_name/edit/$display_id");
153
          foreach ($areas as $area_name => $fields) {
154
            $row['area'] = l($area_name, "admin/structure/views/nojs/rearrange/$view_name/$display_id/$area_name");
155
            foreach ($fields as $field => $content) {
156
              $row['field'] = l($field,
157
                'admin/structure/views/nojs/config-item/'. $view_name .'/'. $display_id .'/'. $area_name .'/'. $field,
158
                ['query' => ['destination' => 'admin/reports/qa/results']]
159
              );
160
              $row['content'] = [
161
                'data'  => '<pre>'. check_plain($content) .'</pre>',
0 ignored issues
show
Bug introduced by
The function check_plain was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
                'data'  => '<pre>'. /** @scrutinizer ignore-call */ check_plain($content) .'</pre>',
Loading history...
162
                'class' => 'pre',
163
              ];
164
              $data[$view_name .'/'. $display_id .'/'. $area_name .'/'. $field] = $row;
165
            }
166
          }
167
        }
168
      }
169
      ksort($data);
170
      $result .= theme('table', [
0 ignored issues
show
Bug introduced by
The function theme was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
      $result .= /** @scrutinizer ignore-call */ theme('table', [
Loading history...
171
        'header' => $header,
172
        'rows' => $data,
173
      ]);
174
    }
175
    $pass->result = $result;
176
    return $pass;
177
  }
178
}
179
180