Setting   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 12
loc 66
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createCache() 0 9 3
A flushCache() 0 4 1
B getValue() 0 15 7
A setValue() 11 11 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
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class Setting extends Base {
5
  protected $table = 'settings';
6
  private $cache = array();
7
8
  /**
9
   * Fetch all values available and cache them in this class
10
   * That way we don't fetch them from DB for each call
11
   */
12
  public function createCache() {
13
    if ($aSettings = $this->getAllAssoc()) {
14
      foreach ($aSettings as $key => $aData) {
15
        $this->cache[$aData['name']] = $aData['value'];
16
      }
17
      return true;
18
    }
19
    return false;
20
  }
21
22
  /**
23
   * Flush our local cache, may be required for upgrades
24
   * or other places where we need live data
25
   **/
26
  public function flushCache() {
27
    $this->cache = array();
28
    return true;
29
  }
30
31
  /**
32
   * Fetch a value from our table
33
   * @param name string Setting name
34
   * @return value string Value
35
   **/
36
  public function getValue($name, $default="") {
37
    // Try our class cache first
38
    if (isset($this->cache[$name])) return $this->cache[$name];
39
    $stmt = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name = ? LIMIT 1");
40
    if ($this->checkStmt($stmt) && $stmt->bind_param('s', $name) && $stmt->execute() && $result = $stmt->get_result()) {
41
      if ($result->num_rows > 0) {
42
        return $result->fetch_object()->value;
43
      } else {
44
        return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (string) is incompatible with the return type documented by Setting::getValue of type value.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
      }
46
    }
47
    // Log error but return empty string
48
    $this->sqlError();
49
    return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (string) is incompatible with the return type documented by Setting::getValue of type value.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
50
  }
51
52
  /**
53
   * Insert or update a setting
54
   * @param name string Name of the variable
55
   * @param value string Variable value
56
   * @return bool
57
   **/
58 View Code Duplication
  public function setValue($name, $value) {
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...
59
    // Update local cache too
60
    $this->cache[$name] = $value;
61
    $stmt = $this->mysqli->prepare("
62
      INSERT INTO $this->table (name, value)
63
      VALUES (?, ?)
64
      ON DUPLICATE KEY UPDATE value = ?");
65
    if ($stmt && $stmt->bind_param('sss', $name, $value, $value) && $stmt->execute())
66
      return true;
67
    return $this->sqlError();
68
  }
69
}
70
71
$setting = new Setting($debug, $mysqli);
0 ignored issues
show
Unused Code introduced by
The call to Setting::__construct() has too many arguments starting with $debug.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72
$setting->setDebug($debug);
73
$setting->setMysql($mysqli);
74
$setting->setErrorCodes($aErrorCodes);
75
// Fill our class cache with data so we don't have to run SQL queries all the time
76
$setting->createCache();
77