Completed
Push — master ( 33d925...cc7944 )
by Thomas
09:52
created

Certificates   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 12
loc 39
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriority() 0 3 1
A getPanel() 12 12 2
A getSectionID() 0 3 1
A __construct() 0 7 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
* @author Tom Needham <[email protected]>
4
*
5
* @copyright Copyright (c) 2016, ownCloud GmbH.
6
* @license AGPL-3.0
7
*
8
* This code is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Affero General Public License, version 3,
10
* as published by the Free Software Foundation.
11
*
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Affero General Public License for more details.
16
*
17
* You should have received a copy of the GNU Affero General Public License, version 3,
18
* along with this program.  If not, see <http://www.gnu.org/licenses/>
19
*
20
*/
21
22
namespace OC\Settings\Panels\Admin;
23
24
use OCP\ICertificateManager;
25
use OCP\Settings\ISettings;
26
use OCP\Template;
27
use OCP\IConfig;
28
use OCP\IURLGenerator;
29
30
class Certificates implements ISettings {
31
32
	/** @var IConfig */
33
	protected $config;
34
	/** @var ICertificateManager */
35
	protected $certificateManager;
36
	/** @var IURLGenerator */
37
	protected $urlGenerator;
38
39
	public function __construct(IConfig $config,
40
								IURLGenerator $urlGenerator,
41
								ICertificateManager $certificateManager) {
42
		$this->config = $config;
43
		$this->urlGenerator = $urlGenerator;
44
		$this->certificateManager = $certificateManager;
45
	}
46
47
	public function getPriority() {
48
		return 0;
49
	}
50
51 View Code Duplication
	public function getPanel() {
52
		if ($this->config->getSystemValue('enable_certificate_management', false)) {
53
			$tmpl = new Template('settings', 'panels/admin/certificates');
54
			$tmpl->assign('type', 'admin');
55
			$tmpl->assign('uploadRoute', 'settings.Certificate.addSystemRootCertificate');
56
			$tmpl->assign('certs', $this->certificateManager->listCertificates());
57
			$tmpl->assign('urlGenerator', $this->urlGenerator);
0 ignored issues
show
Documentation introduced by
$this->urlGenerator is of type object<OCP\IURLGenerator>, but the function expects a array|boolean|integer|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
			return $tmpl;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $tmpl; (OCP\Template) is incompatible with the return type declared by the interface OCP\Settings\ISettings::getPanel of type OCP\AppFramework\Http\TemplateResponse.

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...
59
		} else {
60
			return null;
61
		}
62
	}
63
64
	public function getSectionID() {
65
		return 'general';
66
	}
67
68
}
69