Completed
Push — master ( 8a505e...14bc9b )
by Morris
51:24 queued 31:58
created

NavigationController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 6
loc 62
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAppsNavigation() 0 7 2
A getSettingsNavigation() 0 7 2
A rewriteToAbsoluteUrls() 6 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
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Core\Controller;
24
25
use OCP\AppFramework\Http\DataResponse;
26
use OCP\AppFramework\OCSController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OC\Core\Controller\OCSController.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
use OCP\INavigationManager;
28
use OCP\IRequest;
29
use OCP\IURLGenerator;
30
31
class NavigationController extends OCSController {
32
33
	/** @var INavigationManager */
34
	private $navigationManager;
35
36
	/** @var IURLGenerator */
37
	private $urlGenerator;
38
39
	public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) {
40
		parent::__construct($appName, $request);
41
		$this->navigationManager = $navigationManager;
42
		$this->urlGenerator = $urlGenerator;
43
	}
44
45
	/**
46
	 * @NoAdminRequired
47
	 * @NoCSRFRequired
48
	 *
49
	 * @param bool $absolute
50
	 * @return DataResponse
51
	 */
52
	public function getAppsNavigation(bool $absolute = false): DataResponse {
53
		$navigation = $this->navigationManager->getAll();
54
		if ($absolute) {
55
			$navigation = $this->rewriteToAbsoluteUrls($navigation);
56
		}
57
		return new DataResponse($navigation);
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 *
64
	 * @param bool $absolute
65
	 * @return DataResponse
66
	 */
67
	public function getSettingsNavigation(bool $absolute = false): DataResponse {
68
		$navigation = $this->navigationManager->getAll('settings');
69
		if ($absolute) {
70
			$navigation = $this->rewriteToAbsoluteUrls($navigation);
71
		}
72
		return new DataResponse($navigation);
73
	}
74
75
	/**
76
	 * Rewrite href attribute of navigation entries to an absolute URL
77
	 *
78
	 * @param array $navigation
79
	 * @return array
80
	 */
81
	private function rewriteToAbsoluteUrls(array $navigation): array {
82
		foreach ($navigation as &$entry) {
83 View Code Duplication
			if (0 !== strpos($entry['href'], $this->urlGenerator->getBaseUrl())) {
84
				$entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
85
			}
86 View Code Duplication
			if (0 !== strpos($entry['icon'], $this->urlGenerator->getBaseUrl())) {
87
				$entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
88
			}
89
		}
90
		return $navigation;
91
	}
92
}
93