ImapServerDetector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 17.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 9
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B detect() 9 26 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
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
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
namespace OCA\Mail\Service\AutoConfig;
22
23
class ImapServerDetector {
24
25
	/** @var MxRecord */
26
	private $mxRecord;
27
28
	/** @var ImapConnectivityTester */
29
	private $imapConnectivityTester;
30
31
	/**
32
	 * @param MxRecord $mxRecord
33
	 * @param ImapConnectivityTester $imapTester
34
	 */
35
	public function __construct(MxRecord $mxRecord,
36
		ImapConnectivityTester $imapTester) {
37
		$this->mxRecord = $mxRecord;
38
		$this->imapConnectivityTester = $imapTester;
39
	}
40
41
	/**
42
	 * @param $email
43
	 * @param $password
44
	 * @param $name
45
	 * @return MailAccount|null
46
	 */
47
	public function detect($email, $password, $name) {
48
		// splitting the email address into user and host part
49
		// TODO: use horde libs for email address parsing
50
		list($user, $host) = explode("@", $email);
51
52
		/*
53
		 * Try to get the mx record for the email address
54
		 */
55
		$mxHosts = $this->mxRecord->query($host);
56 View Code Duplication
		if ($mxHosts) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
			foreach ($mxHosts as $mxHost) {
58
				$result = $this->imapConnectivityTester->test($email, $mxHost,
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->imapConnectivityT...ail), $password, $name) (which targets OCA\Mail\Service\AutoCon...nectivityTester::test()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
					[$user, $email], $password, $name);
60
				if ($result) {
61
					return $result;
62
				}
63
			}
64
		}
65
66
		/*
67
		 * IMAP login with full email address as user
68
		 * works for a lot of providers (e.g. Google Mail)
69
		 */
70
		return $this->imapConnectivityTester->test($email, $host, [$user, $email],
71
				$password, $name);
72
	}
73
74
}
75