Completed
Push — 7.x-1.x ( 2f9e3c...1070be )
by Frédéric G.
01:36
created

Php::run()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 45
nc 4
nop 0
dl 0
loc 59
rs 7.132
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\Plugin\Qa\Control\Views;
4
5
/**
6
 * Find views containing PHP code
7
 */
8
class Php extends Views {
9
10
  /**
11
   * {@inheritdoc]
12
   */
13
  public function init() {
14
    $this->package_name = __NAMESPACE__;
15
    $this->title = t('PHP code within views');
16
    $this->description = t('Is there any embedded PHP within views and display definitions ? This is both a security risk and a performance issue.');
17
  }
18
19
  /**
20
   * @param string $area
21
   *   The area (header, footer, empty) being examined.
22
   * @param array $php
23
   *   The array of input formats containing PHP.
24
   * @param \stdClass $display
25
   *   The display being examined.
26
   * @param string $area_name
27
   *   The name of the area
28
   *
29
   * @return array
30
   *   The array of PHP fragments found in the area.
31
   */
32
  protected function checkViews2Php($area, array $php, $display, $area_name) {
33
    $ret = array();
34
    $area_format = $display->display_options[$area_name .'_format']; // Always set
35
    if (in_array($area_format, $php)) {
36
      $ret['text'] = $area;
37
    }
38
    return $ret;
39
  }
40
41
  /**
42
   * @param array $area
43
   *   The area (header, footer, empty) being examined.
44
   * @param array $php
45
   *   The array of input formats containing PHP.
46
   *
47
   * @return array
48
   *   The array of PHP fragments found in the area.
49
   */
50
  protected function checkViews3Php(array $area, array $php) {
51
    $ret = array();
52
    foreach ($area as $field => $field_options) {
53
      if ($field_options['field'] == 'area' && isset($field_options['format']) && in_array($field_options['format'], $php)) {
54
        $ret[$field] = $field_options['content'];
55
      }
56
    }
57
    return $ret;
58
  }
59
60
  /**
61
   * Views 2 had a single string for areas whereas Views 3 has an array for them.
62
   */
63
  public function checkViewPhp($view) {
64
    $php = $this->getPhpFormats();
65
    $areas = array('header', 'footer', 'empty');
66
    $result = array();
67
68
    foreach ($view->display as $display_name => $display) {
69
      foreach ($areas as $area_name) {
70
        if (!isset($display->display_options[$area_name])) {
71
          continue;
72
        }
73
74
        $area = $display->display_options[$area_name];
75
        $fragments = is_array($area)
76
          ? $this->checkViews3Php($area, $php)
77
          : $this->checkViews2Php($area, $php, $display, $area_name);
78
79
        if (!empty($fragments))  {
80
          $result[$display_name][$area_name] = $fragments;
81
        }
82
      } // foreach header, footer, empty...
83
    } // foreach display
84
85
    $ret = array(
86
      'name' => $view->name,
87
      'status' => empty($result),
88
      'result' => $result,
89
    );
90
    return $ret;
91
  }
92
93
  /**
94
   * Get a list of the ids of input formats containing the PHP eval filter.
95
   *
96
   * @return array
97
   */
98
  protected function getPhpFormats($reset = FALSE) {
99
    static $php = NULL;
100
101
    if (!isset($php) || $reset) {
102
      $formats = filter_formats();
103
      $php = array();
104
      foreach ($formats as $format) {
105
        $filters = filter_list_format($format->format);
106
        foreach ($filters as $filter) {
107
          // If filter->status is not set (early 7.x), the presence of the PHP
108
          // module indicates PHP. Otherwise, if filter->status is set,
109
          // activation of the module is controlled by this flag.
110
          if ($filter->module == 'php' && (!isset($filter->status) || !empty($filter->status))) {
111
            $php[] = $format->format;
112
            break;
113
          }
114
        }
115
      }
116
    }
117
118
    return $php;
119
  }
120
121
  function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
    $pass = parent::run();
123
    $views = views_get_all_views(TRUE);
124
    foreach ($views as $view) {
125
      $pass->record($this->checkViewPhp($view));
126
    }
127
    $pass->life->end();
128
129
    if ($pass->status) {
130
      $result = format_plural(count($views), '1 view checked, none containing PHP',
131
        '@count views checked, none containing PHP', array());
132
    }
133
    else {
134
      $result = format_plural(count($views), '1 view checked and containing PHP',
135
        '@count views checked, @php containing PHP', array(
136
          '@php' => count($pass->result),
137
      ));
138
      $header = array(
139
        t('View'),
140
        t('Display'),
141
        t('Area'),
142
        t('Field'),
143
        t('Content'),
144
      );
145
      $data = array();
146
      foreach ($pass->result as $view_name => $displays) {
147
        $row = array();
148
        $link_title = empty($views[$view_name]->human_name)
149
          ? $view_name
150
          : $views[$view_name]->human_name;
151
        $view_link = l($link_title, "admin/structure/views/view/$view_name/edit");
152
        $row['view'] = array('data' => $view_link);
153
        foreach ($displays as $display_id => $areas) {
154
          $row['display'] = l($display_id, "admin/structure/views/view/$view_name/edit/$display_id");
155
          foreach ($areas as $area_name => $fields) {
156
            $row['area'] = l($area_name, "admin/structure/views/nojs/rearrange/$view_name/$display_id/$area_name");
157
            foreach ($fields as $field => $content) {
158
              $row['field'] = l($field,
159
                'admin/structure/views/nojs/config-item/'. $view_name .'/'. $display_id .'/'. $area_name .'/'. $field,
160
                array('query' => array('destination' => 'admin/reports/qa/results'))
161
              );
162
              $row['content'] = array(
163
                'data'  => '<pre>'. check_plain($content) .'</pre>',
164
                'class' => 'pre',
165
              );
166
              $data[$view_name .'/'. $display_id .'/'. $area_name .'/'. $field] = $row;
167
            }
168
          }
169
        }
170
      }
171
      ksort($data);
172
      $result .= theme('table', array(
173
        'header' => $header,
174
        'rows' => $data,
175
      ));
176
    }
177
    $pass->result = $result;
178
    return $pass;
179
  }
180
}
181
182