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:
Complex classes like CheckSetupController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CheckSetupController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class CheckSetupController extends Controller { |
||
| 56 | /** @var IConfig */ |
||
| 57 | private $config; |
||
| 58 | /** @var IClientService */ |
||
| 59 | private $clientService; |
||
| 60 | /** @var \OC_Util */ |
||
| 61 | private $util; |
||
| 62 | /** @var IURLGenerator */ |
||
| 63 | private $urlGenerator; |
||
| 64 | /** @var IL10N */ |
||
| 65 | private $l10n; |
||
| 66 | /** @var Checker */ |
||
| 67 | private $checker; |
||
| 68 | /** @var ILogger */ |
||
| 69 | private $logger; |
||
| 70 | /** @var EventDispatcherInterface */ |
||
| 71 | private $dispatcher; |
||
| 72 | |||
| 73 | View Code Duplication | public function __construct($AppName, |
|
| 74 | IRequest $request, |
||
| 75 | IConfig $config, |
||
| 76 | IClientService $clientService, |
||
| 77 | IURLGenerator $urlGenerator, |
||
| 78 | \OC_Util $util, |
||
| 79 | IL10N $l10n, |
||
| 80 | Checker $checker, |
||
| 81 | ILogger $logger, |
||
| 82 | EventDispatcherInterface $dispatcher) { |
||
| 83 | parent::__construct($AppName, $request); |
||
| 84 | $this->config = $config; |
||
| 85 | $this->clientService = $clientService; |
||
| 86 | $this->util = $util; |
||
| 87 | $this->urlGenerator = $urlGenerator; |
||
| 88 | $this->l10n = $l10n; |
||
| 89 | $this->checker = $checker; |
||
| 90 | $this->logger = $logger; |
||
| 91 | $this->dispatcher = $dispatcher; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Checks if the server can connect to the internet using HTTPS and HTTP |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | private function isInternetConnectionWorking() { |
||
| 99 | if ($this->config->getSystemValue('has_internet_connection', true) === false) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | $siteArray = ['www.nextcloud.com', |
||
| 104 | 'www.startpage.com', |
||
| 105 | 'www.eff.org', |
||
| 106 | 'www.edri.org', |
||
| 107 | ]; |
||
| 108 | |||
| 109 | foreach($siteArray as $site) { |
||
| 110 | if ($this->isSiteReachable($site)) { |
||
| 111 | return true; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Checks if the Nextcloud server can connect to a specific URL using both HTTPS and HTTP |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | private function isSiteReachable($sitename) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Checks whether a local memcache is installed or not |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | private function isMemcacheConfigured() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Whether /dev/urandom is available to the PHP controller |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | private function isUrandomAvailable() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Public for the sake of unit-testing |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function getCurlVersion() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if the used SSL lib is outdated. Older OpenSSL and NSS versions do |
||
| 172 | * have multiple bugs which likely lead to problems in combination with |
||
| 173 | * functionality required by ownCloud such as SNI. |
||
| 174 | * |
||
| 175 | * @link https://github.com/owncloud/core/issues/17446#issuecomment-122877546 |
||
| 176 | * @link https://bugzilla.redhat.com/show_bug.cgi?id=1241172 |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | private function isUsedTlsLibOutdated() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Whether the version is outdated |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | protected function isPhpOutdated() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Whether the php version is still supported (at time of release) |
||
| 248 | * according to: https://secure.php.net/supported-versions.php |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | private function isPhpSupported() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Check if the reverse proxy configuration is working as expected |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | private function forwardedForHeadersWorking() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Checks if the correct memcache module for PHP is installed. Only |
||
| 275 | * fails if memcached is configured and the working module is not installed. |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | private function isCorrectMemcachedPHPModuleInstalled() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Checks if set_time_limit is not disabled. |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | private function isSettimelimitAvailable() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return RedirectResponse |
||
| 306 | */ |
||
| 307 | public function rescanFailedIntegrityCheck() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @NoCSRFRequired |
||
| 316 | * @return DataResponse |
||
| 317 | */ |
||
| 318 | public function getFailedIntegrityCheckFiles() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Checks whether a PHP opcache is properly set up |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | protected function isOpcacheProperlySetup() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Check if the required FreeType functions are present |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | protected function hasFreeTypeSupport() { |
||
| 417 | |||
| 418 | protected function hasMissingIndexes(): array { |
||
| 426 | |||
| 427 | protected function isSqliteUsed() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return DataResponse |
||
| 433 | */ |
||
| 434 | public function check() { |
||
| 459 | } |
||
| 460 |
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: