OPCacheConfiguration   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 114
Duplicated Lines 23.68 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 27
loc 114
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getDirectives() 0 3 1
A getDirective() 0 7 2
B getDirectiveKeys() 0 29 1
A getBlacklist() 0 3 1
A getVersion() 0 3 1
B checkDirectives() 0 26 6
A recommendedDirectiveSetting() 13 13 3
A requiredDirectiveSetting() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace OPCache;
4
5
class OPCacheConfiguration {
6
7
  private $configurationData;
8
9
  public function __construct() {
10
    $this->configurationData = opcache_get_configuration();
11
  }
12
13
  public function getDirectives() {
14
    return $this->configurationData['directives'];
15
  }
16
17
  public function getDirective($directive) {
18
    if (strpos($directive, 'opcache.') === FALSE) {
19
      $directive = 'opcache.' . $directive;
20
    }
21
22
    return ini_get($directive);
23
  }
24
25
  public function getDirectiveKeys() {
26
    return array(
27
      'opcache.enable',
28
      'opcache.enable_cli',
29
      'opcache.memory_consumption',
30
      'opcache.interned_strings_buffer',
31
      'opcache.max_accelerated_files',
32
      'opcache.wasted_percentage',
33
      'opcache.validate_timestamps',
34
      'opcache.revalidate_freq',
35
      'opcache.revalidate_path',
36
      'opcache.save_comments',
37
      'opcache.load_comments',
38
      'opcache.fast_shutdown',
39
      'opcache.enable_file_override',
40
      'opcache.optimization_level',
41
      'opcache.inherited_hack',
42
      'opcache.dups_fix',
43
      'opcache.blacklist_filename',
44
      'opcache.max_file_size',
45
      'opcache.consistency_checks',
46
      'opcache.force_restart_timeout',
47
      'opcache.error_log',
48
      'opcache.log_verbosity_level',
49
      'opcache.preferred_memory_model',
50
      'opcache.protect_memory',
51
      'opcache.mmap_base',
52
    );
53
  }
54
55
  public function getBlacklist() {
56
    return $this->configurationData['blacklist'];
57
  }
58
59
  public function getVersion() {
60
    return $this->configurationData['version'];
61
  }
62
63
  public function checkDirectives() {
64
    $recommended = array();
65
    $required = array();
66
    $severity = REQUIREMENT_OK;
67
    foreach ($this->getDirectiveKeys() as $directive) {
68
      if ($message = $this->recommendedDirectiveSetting($directive)) {
69
        $recommended[$directive] = $message;
70
      }
71
      if ($message = $this->requiredDirectiveSetting($directive)) {
72
        $required[$directive] = $message;
73
      }
74
    }
75
76
    if (!empty($recommended)) {
77
      $severity = REQUIREMENT_WARNING;
78
    }
79
    if (!empty($required)) {
80
      $severity = REQUIREMENT_ERROR;
81
    }
82
83
    $messages = array_merge($recommended, $required);
84
    return array(
85
      'messages' => $messages,
86
      'severity' => $severity,
87
    );
88
  }
89
90 View Code Duplication
  private function recommendedDirectiveSetting($directive) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    $setting = $this->getDirective($directive);
92
93
    switch ($directive) {
94
      case 'opcache.validate_timestamps':
95
        if ($setting == TRUE) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $setting of type string to the boolean TRUE. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
96
          return t('OPcache is still checking the timestamps of files to see if they have been updated. For maximum performance, set <tt>opcache.validate_timestamps = 0</tt> in your PHP installation&rsquo;s configuration file, and use the &ldquo;Clear all caches&rdquo; button on the <a href="!perf_path">Performance page</a> to clear the OPcache cache after updating files (or use Drush).', array('!perf_path' => url('admin/config/development/performance')));
97
        }
98
        break;
99
      default:
100
        break;
101
    }
102
  }
103
104 View Code Duplication
  private function requiredDirectiveSetting($directive) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    $setting = $this->getDirective($directive);
106
107
    switch ($directive) {
108
      case 'opcache.save_comments':
109
      case 'opcache.load_comments':
110
        if ($setting == FALSE) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $setting of type string to the boolean FALSE. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
111
          return t('Comment saving and/or loading is disabled. This functionality <em>must</em> be enabled, or Drupal will not function correctly. Ensure that you have <tt>opcache.save_comments = 1</tt> and <tt>opcache.load_comments = 1</tt> in this PHP installation&rsquo;s configuration file.');
112
        }
113
        break;
114
      default:
115
        break;
116
    }
117
  }
118
}
119