opcache.module::opcache_menu()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 54
nc 1
nop 0
dl 0
loc 65
rs 9.3571
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
/**
4
 * @file
5
 * Contains functions for OPcache module.
6
 */
7
8
use OPCache\OPCache;
9
use OPCache\OPCacheConfiguration;
10
use OPCache\OPCacheStatus;
11
12
/**
13
 * Implements hook_flush_caches().
14
 */
15
function opcache_flush_caches() {
16
  if (!drupal_is_cli()) {
17
    composer_manager_register_autoloader();
18
    if (class_exists('\OPCache\OPCache')) {
19
      $opcache = new OPCache();
20
      $opcache->cacheClear();
21
    }
22
  }
23
}
24
25
/**
26
 * Implements hook_menu().
27
 */
28
function opcache_menu() {
29
  $items = array(
30
    'opcache/%/%/reset/%' => array(
31
      'type' => MENU_CALLBACK,
32
      'access callback' => 'opcache_router_access',
33
      'access arguments' => array(1, 2),
34
      'page callback' => 'opcache_router',
35
      'page arguments' => array(3, 4),
36
    ),
37
    'opcache/%/%/invalidate/%' => array(
38
      'type' => MENU_CALLBACK,
39
      'access callback' => 'opcache_router_access',
40
      'access arguments' => array(1 ,2),
41
      'page callback' => 'opcache_router',
42
      'page arguments' => array(3, 4),
43
    ),
44
    'opcache/%/%/status' => array(
45
      'type' => MENU_CALLBACK,
46
      'access callback' => 'opcache_router_access',
47
      'access arguments' => array(1, 2),
48
      'page callback' => 'opcache_router',
49
      'page arguments' => array(3),
50
    ),
51
    'admin/config/development/performance/opcache' => array(
52
      'type' => MENU_LOCAL_TASK,
53
      'title' => 'OPcache',
54
      'page callback' => 'drupal_get_form',
55
      'page arguments' => array('opcache_admin_form'),
56
      'access arguments' => array('administer site configuration'),
57
      'file' => 'opcache.admin.inc',
58
      'weight' => 10,
59
    ),
60
    'admin/config/development/performance/opcache/backends' => array(
61
      'title' => 'Backends',
62
      'type' => MENU_DEFAULT_LOCAL_TASK,
63
      'weight' => -10,
64
    ),
65
    'admin/config/development/performance/opcache/status' => array(
66
      'title' => 'Status',
67
      'type' => MENU_LOCAL_TASK,
68
      'page callback' => 'opcache_admin_status',
69
      'access arguments' => array('administer site configuration'),
70
      'file' => 'opcache.admin.inc',
71
      'weight' => 0,
72
    ),
73
    'admin/config/development/performance/opcache/configuration' => array(
74
      'title' => 'Configuration',
75
      'type' => MENU_LOCAL_TASK,
76
      'page callback' => 'opcache_admin_config',
77
      'access arguments' => array('administer site configuration'),
78
      'file' => 'opcache.admin.inc',
79
      'weight' => 1,
80
    ),
81
    'admin/config/development/performance/opcache/scripts' => array(
82
      'title' => 'Scripts',
83
      'type' => MENU_LOCAL_TASK,
84
      'page callback' => 'opcache_admin_scripts',
85
      'access arguments' => array('administer site configuration'),
86
      'file' => 'opcache.admin.inc',
87
      'weight' => 0,
88
    ),
89
 );
90
91
  return $items;
92
}
93
94
/**
95
 * Access callback for OPcache router.
96
 */
97
function opcache_router_access($request_time, $token) {
98
  composer_manager_register_autoloader();
99
  $opcache = new OPCache();
100
  if (!$opcache->isEnabled() || REQUEST_TIME - $request_time > 30) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $opcache->isEnabled() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
101
    return FALSE;
102
  }
103
104
  return $opcache->verifyToken($request_time, $token);
105
}
106
107
/**
108
 * Page callback for OPcache router.
109
 */
110
function opcache_router($operation = 'reset', $arg = FALSE) {
111
  composer_manager_register_autoloader();
112
  $opcache = new OPCache();
113
  if ($operation == 'reset') {
114
    $opcache->reset($arg);
115
  }
116
  elseif ($operation == 'invalidate') {
117
    $opcache->invalidate($arg);
118
  }
119
  elseif ($operation == 'status') {
120
    print json_encode($opcache->status());
121
    exit();
122
  }
123
124
  return '';
125
}
126