Test Failed
Push — master ( 4cd501...1b9ce5 )
by Stiofan
07:38
created

Google_APCCache   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 73
Duplicated Lines 19.18 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 73
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A isLocked() 0 6 2
A createLock() 0 5 1
A removeLock() 0 4 1
A waitForLock() 14 14 4
A get() 0 11 4
A set() 0 5 2
A delete() 0 3 1

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
 * Copyright 2010 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/**
19
 * A persistent storage class based on the APC cache, which is not
20
 * really very persistent, as soon as you restart your web server
21
 * the storage will be wiped, however for debugging and/or speed
22
 * it can be useful, kinda, and cache is a lot cheaper then storage.
23
 *
24
 * @author Chris Chabot <[email protected]>
25
 */
26
class Google_APCCache extends Google_Cache {
27
28
  public function __construct() {
29
    if (! function_exists('apc_add')) {
30
      throw new Google_CacheException("Apc functions not available");
31
    }
32
  }
33
34
  private function isLocked($key) {
35
    if ((@apc_fetch($key . '.lock')) === false) {
36
      return false;
37
    }
38
    return true;
39
  }
40
41
  private function createLock($key) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
42
    // the interesting thing is that this could fail if the lock was created in the meantime..
43
    // but we'll ignore that out of convenience
44
    @apc_add($key . '.lock', '', 5);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
  }
46
47
  private function removeLock($key) {
48
    // suppress all warnings, if some other process removed it that's ok too
49
    @apc_delete($key . '.lock');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
  }
51
52 View Code Duplication
  private function waitForLock($key) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
53
    // 20 x 250 = 5 seconds
54
    $tries = 20;
55
    $cnt = 0;
56
    do {
57
      // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks..
58
      usleep(250);
59
      $cnt ++;
60
    } while ($cnt <= $tries && $this->isLocked($key));
61
    if ($this->isLocked($key)) {
62
      // 5 seconds passed, assume the owning process died off and remove it
63
      $this->removeLock($key);
64
    }
65
  }
66
67
   /**
68
   * @inheritDoc
69
   */
70
  public function get($key, $expiration = false) {
71
72
    if (($ret = @apc_fetch($key)) === false) {
73
      return false;
74
    }
75
    if (!$expiration || (time() - $ret['time'] > $expiration)) {
76
      $this->delete($key);
77
      return false;
78
    }
79
    return unserialize($ret['data']);
80
  }
81
82
  /**
83
   * @inheritDoc
84
   */
85
  public function set($key, $value) {
86
    if (@apc_store($key, array('time' => time(), 'data' => serialize($value))) == false) {
87
      throw new Google_CacheException("Couldn't store data");
88
    }
89
  }
90
91
  /**
92
   * @inheritDoc
93
   * @param String $key
94
   */
95
  public function delete($key) {
96
    @apc_delete($key);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
  }
98
}
99