Completed
Pull Request — master (#770)
by Julius
08:36
created

Admin   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 70
rs 10
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getForm() 0 27 2
A getSection() 0 3 1
A getPriority() 0 3 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[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
24
namespace OCA\Theming\Settings;
25
26
use OCA\Theming\ThemingDefaults;
27
use OCP\AppFramework\Http\TemplateResponse;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\Settings\ISettings;
32
use \OC_Defaults;
33
34
class Admin implements ISettings {
35
	/** @var IConfig */
36
	private $config;
37
	/** @var IL10N */
38
	private $l;
39
	/** @var ThemingDefaults|OC_Defaults */
40
	private $themingDefaults;
41
	/** @var IURLGenerator */
42
	private $urlGenerator;
43
44
	public function __construct(IConfig $config,
45
								IL10N $l,
46
								OC_Defaults $themingDefaults,
47
								IURLGenerator $urlGenerator) {
48
		$this->config = $config;
49
		$this->l = $l;
50
		$this->themingDefaults = $themingDefaults;
51
		$this->urlGenerator = $urlGenerator;
52
	}
53
54
	/**
55
	 * @return TemplateResponse
56
	 */
57
	public function getForm() {
58
		$path = $this->urlGenerator->linkToRoute('theming.Theming.updateLogo');
59
60
		$themable = true;
61
		$errorMessage = '';
62
		$theme = $this->config->getSystemValue('theme', '');
63
		if ($theme !== '') {
64
			$themable = false;
65
			$errorMessage = $this->l->t('You already use a custom theme');
66
		}
67
68
		$parameters = [
69
			'themable'        => $themable,
70
			'errorMessage'    => $errorMessage,
71
			'name'            => $this->themingDefaults->getEntity(),
72
			'url'             => $this->themingDefaults->getBaseUrl(),
73
			'slogan'          => $this->themingDefaults->getSlogan(),
74
			'color'           => $this->themingDefaults->getMailHeaderColor(),
75
			'logo'			  => $this->themingDefaults->getLogo(),
0 ignored issues
show
Bug introduced by
The method getLogo does only exist in OCA\Theming\ThemingDefaults, but not in OC_Defaults.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
			'logoMime'		  => $this->config->getAppValue('theming', 'logoMime', ''),
77
			'background'	  => $this->themingDefaults->getBackground(),
0 ignored issues
show
Bug introduced by
The method getBackground does only exist in OCA\Theming\ThemingDefaults, but not in OC_Defaults.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
			'backgroundMime'  => $this->config->getAppValue('theming', 'backgroundMime', ''),
79
			'uploadLogoRoute' => $path,
80
		];
81
82
		return new TemplateResponse('theming', 'settings-admin', $parameters, '');
83
	}
84
85
	/**
86
	 * @return string the section ID, e.g. 'sharing'
87
	 */
88
	public function getSection() {
89
		return 'theming';
90
	}
91
92
	/**
93
	 * @return int whether the form should be rather on the top or bottom of
94
	 * the admin section. The forms are arranged in ascending order of the
95
	 * priority values. It is required to return a value between 0 and 100.
96
	 *
97
	 * E.g.: 70
98
	 */
99
	public function getPriority() {
100
		return 5;
101
	}
102
103
}
104