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

Admin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A getPriority() 3 3 1
A getSectionID() 3 3 1
A getPanel() 12 12 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
namespace OCA\Files_External\Panels;
4
5
use OC\Encryption\Manager;
6
use OCP\Files\External\Service\IGlobalStoragesService;
7
use OCP\Settings\ISettings;
8
use OCP\Files\External\IStoragesBackendService;
9
use OCP\Template;
10
11 View Code Duplication
class Admin implements ISettings {
0 ignored issues
show
Duplication introduced by
This class 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...
12
13
	/** @var IGlobalStoragesService */
14
	protected $globalStoragesService;
15
	/** @var IStoragesBackendService */
16
	protected $backendService;
17
	/** @var Manager */
18
	protected $encManager;
19
20
	public function __construct(IGlobalStoragesService $globalStoragesService,
21
								IStoragesBackendService $backendService,
22
								Manager $encManager) {
23
		$this->globalStoragesService = $globalStoragesService;
24
		$this->backendService = $backendService;
25
		$this->encManager = $encManager;
26
	}
27
28
	public function getPriority() {
29
		return 0;
30
	}
31
32
	public function getSectionID() {
33
		return 'storage';
34
	}
35
36
	public function getPanel() {
37
		// we must use the same container
38
		$tmpl = new Template('files_external', 'settings');
39
		$tmpl->assign('encryptionEnabled', $this->encManager->isEnabled());
40
		$tmpl->assign('visibilityType', IStoragesBackendService::VISIBILITY_ADMIN);
41
		$tmpl->assign('storages', $this->globalStoragesService->getStorages());
42
		$tmpl->assign('backends', $this->backendService->getAvailableBackends());
43
		$tmpl->assign('authMechanisms', $this->backendService->getAuthMechanisms());
44
		$tmpl->assign('dependencies', \OC_Mount_Config::dependencyMessage($this->backendService->getBackends()));
0 ignored issues
show
Documentation introduced by
$this->backendService->getBackends() is of type array<integer,object<OCP...ernal\Backend\Backend>>, but the function expects a array<integer,object<Backend>>.

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...
45
		$tmpl->assign('allowUserMounting', $this->backendService->isUserMountingAllowed());
46
		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...
47
	}
48
49
}
50