Completed
Push — master ( 59ae2c...edeca8 )
by Christoph
15s
created

IspDb::queryUrl()   C

Complexity

Conditions 12
Paths 43

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.0427

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 37
ccs 28
cts 30
cp 0.9333
rs 5.1612
cc 12
eloc 25
nc 43
nop 1
crap 12.0427

How to fix   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
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * ownCloud - 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
22
namespace OCA\Mail\Service\AutoConfig;
23
24
use Exception;
25
use OCA\Mail\Service\Logger;
26
27
class IspDb {
28
29
	/** @var Logger */
30
	private $logger;
31
32
	/** @var string[] */
33 2
	public function getUrls() {
34
		return [
35 2
			'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
36 2
			'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
37 2
			'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
38 2
		];
39
	}
40
41
	/**
42
	 * @param Logger $logger
43
	 * @param string[] $ispUrls
0 ignored issues
show
Bug introduced by
There is no parameter named $ispUrls. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
	 */
45 3
	public function __construct(Logger $logger) {
46 3
		$this->logger = $logger;
47 3
	}
48
49 3
	private function queryUrl($url) {
50
		try {
51 3
			$xml = @simplexml_load_file($url);
52 3
			if (libxml_get_last_error() !== False || !is_object($xml) || !$xml->emailProvider) {
53 2
				libxml_clear_errors();
54 2
				return [];
55
			}
56
			$provider = [
57 3
				'displayName' => (string) $xml->emailProvider->displayName,
58 3
			];
59 3
			foreach ($xml->emailProvider->children() as $tag => $server) {
60 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
61 3
					continue;
62
				}
63 3
				foreach ($server->attributes() as $name => $value) {
64 3
					if ($name == 'type') {
65 3
						$type = (string) $value;
66 3
					}
67 3
				}
68 3
				$data = [];
69 3
				foreach ($server as $name => $value) {
70 3
					foreach ($value->children() as $tag => $val) {
71 2
						$data[$name][$tag] = (string) $val;
72 3
					}
73 3
					if (!isset($data[$name])) {
74 3
						$data[$name] = (string) $value;
75 3
					}
76 3
				}
77 3
				$provider[$type][] = $data;
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
78 3
			}
79 3
		} catch (Exception $e) {
80
			// ignore own not-found exception or xml parsing exceptions
81
			unset($e);
82
			$provider = [];
83
		}
84 3
		return $provider;
85
	}
86
87
	/**
88
	 * @param string $domain
89
	 * @return array
90
	 */
91 3
	public function query($domain, $tryMx = true) {
92 3
		$this->logger->debug("IsbDb: querying <$domain>");
93 3
		if (strpos($domain, '@') !== false) {
94
			// TODO: use horde mail address parsing instead
95
			list(, $domain) = explode('@', $domain);
96
		}
97
98 3
		$provider = [];
99 3
		foreach ($this->getUrls() as $url) {
100 3
			$url = str_replace("{DOMAIN}", $domain, $url);
101 3
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
102
103 3
			$provider = $this->queryUrl($url);
104 3
			if (!empty($provider)) {
105 3
				return $provider;
106
			}
107 2
		}
108
109
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
110
			$domain = $dns[0]['target'];
111
			if (!($provider = $this->query($domain, false))) {
112
				list(, $domain) = explode('.', $domain, 2);
113
				$provider = $this->query($domain, false);
114
			}
115
		}
116
		return $provider;
117
	}
118
119
}
120