Completed
Pull Request — master (#1515)
by
unknown
08:44
created

IspDb::queryUrl()   C

Complexity

Conditions 13
Paths 46

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 13.0245

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 49
ccs 36
cts 38
cp 0.9474
rs 5.1401
cc 13
eloc 33
nc 46
nop 1
crap 13.0245

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
			$content = @file_get_contents($url, false, stream_context_create([
52
				'http' => [
53 3
					'timeout' => 7,
54
					'ignore_errors' => true
55 3
				]
56 3
			]));
57 3
			if ($content !== false) {
58 3
				$xml = @simplexml_load_string($content);
59 3
			} else {
60 2
				$this->logger->debug("IsbDb: <$url> request timed out");
61 2
				return [];
62
			}
63
64 3
			if (libxml_get_last_error() !== false || !is_object($xml) || !$xml->emailProvider) {
65 2
				libxml_clear_errors();
66 2
				return [];
67
			}
68
			$provider = [
69 3
				'displayName' => (string) $xml->emailProvider->displayName,
70 3
			];
71 3
			foreach ($xml->emailProvider->children() as $tag => $server) {
72 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
73 3
					continue;
74
				}
75 3
				foreach ($server->attributes() as $name => $value) {
76 3
					if ($name == 'type') {
77 3
						$type = (string) $value;
78 3
					}
79 3
				}
80 3
				$data = [];
81 3
				foreach ($server as $name => $value) {
82 3
					foreach ($value->children() as $tag => $val) {
83 2
						$data[$name][$tag] = (string) $val;
84 3
					}
85 3
					if (!isset($data[$name])) {
86 3
						$data[$name] = (string) $value;
87 3
					}
88 3
				}
89 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...
90 3
			}
91 3
		} catch (Exception $e) {
92
			// ignore own not-found exception or xml parsing exceptions
93
			unset($e);
94
			$provider = [];
95
		}
96 3
		return $provider;
97
	}
98
99
	/**
100
	 * @param string $domain
101
	 * @return array
102
	 */
103 3
	public function query($domain, $tryMx = true) {
104 3
		$this->logger->debug("IsbDb: querying <$domain>");
105 3
		if (strpos($domain, '@') !== false) {
106
			// TODO: use horde mail address parsing instead
107
			list(, $domain) = explode('@', $domain);
108
		}
109
110 3
		$provider = [];
111 3
		foreach ($this->getUrls() as $url) {
112 3
			$url = str_replace("{DOMAIN}", $domain, $url);
113 3
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
114
115 3
			$provider = $this->queryUrl($url);
116 3
			if (!empty($provider)) {
117 3
				return $provider;
118
			}
119 2
		}
120
121
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
122
			$domain = $dns[0]['target'];
123
			if (!($provider = $this->query($domain, false))) {
124
				list(, $domain) = explode('.', $domain, 2);
125
				$provider = $this->query($domain, false);
126
			}
127
		}
128
		return $provider;
129
	}
130
131
}
132