UserAdministration   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 77.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 38
loc 49
rs 10
c 0
b 0
f 0
ccs 0
cts 37
cp 0
wmc 8
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C update() 38 38 7

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
 *
4
 * Administration of user data and rights
5
 * Please read in User.php for description of relations
6
 *
7
 * @package Intraface_Administration
8
 * @author  Sune Jensen <[email protected]>
9
 * @author  Lars Olesen <[email protected]>
10
 * @since   0.1.0
11
 * @version     @package-version@
12
 *
13
 *
14
 */
15
class UserAdministration extends Intraface_User
16
{
17
    function __construct($kernel, $id)
18
    {
19
        parent::__construct($id);
20
    }
21
22
    /**
23
     * @todo why use this instead of the one in user?
24
     */
25 View Code Duplication
    function update($input)
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...
26
    {
27
        $this->validate($input);
28
        $validator = new Intraface_Validator($this->error);
29
30
        if (!empty($input["password"])) {
31
            if ($this->id == 0) {
32
                $validator->isPassword($input["password"], 6, 16, "Ugyldig adgangskode. Den skal være mellem 6 og 16 tegn, og må indeholde store og små bogstaver samt tal");
33
            } else {
34
                $validator->isPassword($input["password"], 6, 16, "Ugyldig adgangskode. Den skal være mellem 6 og 16 tegn, og må indeholde store og små bogstaver samt tal", "allow_empty");
35
            }
36
        }
37
38
        $sql = "email = \"".$input["email"]."\"";
39
40
        if (!empty($input["password"])) {
41
            if ($input["password"] === $input["confirm_password"]) {
42
                $sql .= ", password = \"".md5($input["password"])."\"";
43
            } else {
44
                $this->error->set("De to adgangskoder er ikke ens!");
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            }
46
        }
47
48
        if ($this->error->isError()) {
0 ignored issues
show
Bug introduced by
The method isError() does not seem to exist on object<Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Intraface_User::update of type integer.

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
        if ($this->id) {
53
            $this->db->exec("UPDATE user SET ".$sql." WHERE id = ".$this->id);
54
            $this->load();
55
            return $this->id;
56
        } else {
57
            $this->db->exec("INSERT INTO user SET ".$sql);
58
            $this->id = $this->db->lastInsertId();
59
            $this->load();
60
            return $this->id;
61
        }
62
    }
63
}
64