Completed
Push — master ( d208aa...25518b )
by Lars
12:37
created

Intraface_modules_cms_section_Picture   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 20.21 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 19
loc 94
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B load_section() 9 24 4
A validate_section() 9 12 3
B save_section() 0 46 2

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
 * Picture Section
4
 *
5
 * @package Intraface_CMS
6
 * @author  Lars Olesen <[email protected]>
7
 * @since   0.1.0
8
 * @version @package-version@
9
 */
10
class Intraface_modules_cms_section_Picture extends CMS_Section
11
{
12
    function __construct($cmspage, $id = 0)
13
    {
14
        $this->value['type'] = 'picture';
15
        parent::__construct($cmspage, $id);
16
        $this->cmspage->kernel->useModule('filemanager');
17
    }
18
19
    function load_section()
20
    {
21
22
        $this->value['pic_id'] = $this->parameter->get('pic_id');
23
        $size = $this->template_section->get('pic_size');
24
        $this->cmspage->kernel->useModule('filemanager');
25
        $this->value['picture'] = array();
26
27
        if ($this->value['pic_id'] == 0) {
28
            return;
29
        }
30
31
32
        $filemanager = new FileHandler($this->cmspage->kernel, $this->value['pic_id']);
33
34 View Code Duplication
        if ($filemanager->get('id') > 0) {
35
            if ($size == 'original') {
36
                $this->value['picture'] = $filemanager->get();
37
            } else {
38
                $filemanager->createInstance($size);
39
                $this->value['picture'] = $filemanager->instance->get();
40
            }
41
        }
42
    }
43
44 View Code Duplication
    function validate_section(& $var)
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...
45
    {
46
        $validator = new Intraface_Validator($this->error);
47
        if (!empty($var['pic_id'])) {
48
            $validator->isNumeric($var['pic_id'], 'error in pic_id', 'allow_empty');
49
        }
50
51
        if ($this->error->isError()) {
52
            return 0;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 0; (integer) is incompatible with the return type of the parent method CMS_Section::validate_section of type boolean.

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...
53
        }
54
        return 1;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 1; (integer) is incompatible with the return type of the parent method CMS_Section::validate_section of type boolean.

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...
55
    }
56
57
    function save_section($var)
58
    {
59
60
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
        Det g�res nu i page.php
62
        if (!empty($_FILES['new_picture_'.$key])) {
63
            $filehandler = new FileHandler($kernel);
64
            $filehandler->loadUpload();
65
            $filehandler->upload->setSetting('file_accessibility', 'public');
66
            $id = $filehandler->upload->upload('new_picture_'.$key);
67
68
            if ($id != 0) {
69
                $var['pic_id'] = $id;
70
            }
71
        }
72
        */
73
74
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        if (!empty($var['delete_picture'])) {
76
            $var['pic_id'] = 0;
77
        }
78
        elseif (!empty($_FILES) AND !empty($_FILES['userfile']['name'][$this->get('id')])) {
79
            $filehandler = new FileHandler($this->cmspage->kernel);
80
            $filehandler->loadUpload();
81
            if (!$id = $filehandler->upload->upload('userfile['.$this->get('id').']')) {
82
                throw new Exception('Kunne ikke uploade filen');
83
            }
84
            $var['pic_id'] = $id;
85
            if ($this->cmspage->kernel->user->hasModuleAccess('filemanager')) {
86
                $this->cmspage->kernel->useModule('filemanager');
87
                $filemanager = new FileManager($this->cmspage->kernel, $var['pic_id']);
88
                if (!$filemanager->update(array('description' => $var['pic_text']))) {
89
                    throw new Exception('Filemanager kunne ikke gemme teksten.');
90
                }
91
            }
92
93
        }
94
        else {
95
            $var['pic_id'] = $this->parameter->get('pic_id');
96
        }
97
        */
98
        if (!empty($var['pic_id'])) {
99
            $this->addParameter('pic_id', $var['pic_id']);
100
        }
101
        return 1;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 1; (integer) is incompatible with the return type of the parent method CMS_Section::save_section of type boolean.

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...
102
    }
103
}
104