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

SecurityWarning::getPanel()   F

Complexity

Conditions 12
Paths 600

Size

Total Lines 54
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 41
nc 600
nop 0
dl 0
loc 54
rs 3.6991
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 OC\Lock\NoopLockingProvider;
25
use OC\Settings\Panels\Helper;
26
use OCP\IConfig;
27
use OCP\IDBConnection;
28
use OCP\IL10N;
29
use OCP\Lock\ILockingProvider;
30
use OCP\Settings\ISettings;
31
use OCP\Template;
32
33
class SecurityWarning implements ISettings {
34
35
	/** @var IL10N */
36
	protected $l;
37
	/** @var IConfig */
38
	protected $config;
39
	/** @var IDBConnection */
40
	protected $dbconnection;
41
	/** @var Helper */
42
	protected $helper;
43
	/** @var ILockingProvider */
44
	protected $lockingProvider;
45
46
	public function __construct(IL10N $l,
47
								IConfig $config,
48
								IDBConnection $dbconnection,
49
								Helper $helper,
50
								ILockingProvider $lockingProvider) {
51
		$this->l = $l;
52
		$this->config = $config;
53
		$this->dbconnection = $dbconnection;
54
		$this->helper = $helper;
55
		$this->lockingProvider = $lockingProvider;
56
	}
57
58
	public function getPriority() {
59
		return 1000;
60
	}
61
62
	public function getPanel() {
63
		$template = new Template('settings', 'panels/admin/securitywarning');
64
		// warn if php is not setup properly to get system variables with getenv
65
		$path = getenv('PATH');
66
		$template->assign('getenvServerNotWorking', empty($path));
67
		$template->assign('readOnlyConfigEnabled', $this->helper->isReadOnlyConfigEnabled());
68
		$template->assign('isAnnotationsWorking', $this->helper->isAnnotationsWorking());
69
		try {
70
			if ($this->dbconnection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
71
				$template->assign('invalidTransactionIsolationLevel', false);
72
			} else {
73
				$template->assign('invalidTransactionIsolationLevel', $this->dbconnection->getTransactionIsolation() !== \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED);
74
			}
75
		} catch (\Doctrine\DBAL\DBALException $e) {
76
			// ignore
77
			$template->assign('invalidTransactionIsolationLevel', false);
78
		}
79
		// warn if outdated version of a memcache module is used
80
		$caches = [
81
		'apcu'	=> ['name' => $this->l->t('APCu'), 'version' => '4.0.6'],
82
		'redis'	=> ['name' => $this->l->t('Redis'), 'version' => '2.2.5'],
83
		];
84
		$outdatedCaches = [];
85
		foreach ($caches as $php_module => $data) {
86
			$isOutdated = extension_loaded($php_module) && version_compare(phpversion($php_module), $data['version'], '<');
87
			if ($isOutdated) {
88
				$outdatedCaches[$php_module] = $data;
89
			}
90
		}
91
		$template->assign('OutdatedCacheWarning', $outdatedCaches);
92
		$template->assign('has_fileinfo', $this->helper->fileInfoLoaded());
93
		$databaseOverload = (strpos($this->config->getSystemValue('dbtype'), 'sqlite') !== false);
94
		$template->assign('databaseOverload', $databaseOverload);
95
		if ($this->lockingProvider instanceof NoopLockingProvider) {
96
			$template->assign('fileLockingType', 'none');
97
		} else if ($this->lockingProvider instanceof \OC\Lock\DBLockingProvider) {
98
			$template->assign('fileLockingType', 'db');
99
		} else {
100
			$template->assign('fileLockingType', 'cache');
101
		}
102
		$template->assign('isLocaleWorking', $this->helper->isSetLocaleWorking());
103
104
		// If the current web root is non-empty but the web root from the config is,
105
		// and system cron is used, the URL generator fails to build valid URLs.
106
		$shouldSuggestOverwriteCliUrl = $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'cron' &&
107
		\OC::$WEBROOT && \OC::$WEBROOT !== '/' &&
108
		!$this->config->getSystemValue('overwrite.cli.url', '');
109
		$suggestedOverwriteCliUrl = ($shouldSuggestOverwriteCliUrl) ? \OC::$WEBROOT : '';
110
		$template->assign('suggestedOverwriteCliUrl', $suggestedOverwriteCliUrl);
111
		$template->assign('backgroundjobs_mode', $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax'));
112
		$template->assign('cronErrors', $this->config->getAppValue('core', 'cronErrors'));
113
		$template->assign('checkForWorkingWellKnownSetup', $this->config->getSystemValue('check_for_working_wellknown_setup', true));
114
		return $template;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $template; (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...
115
	}
116
117
	public function getSectionID() {
118
		return 'general';
119
	}
120
121
}
122