EmailParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 20 2
A parseLinksFromContent() 0 12 2
A parseRecipient() 0 10 4
1
<?php
2
/**
3
 * Defines Ingenerator\Mailhook\EmailParser
4
 *
5
 * @copyright  2014 inGenerator Ltd
6
 * @licence    BSD
7
 */
8
9
namespace Ingenerator\Mailhook;
10
11
/**
12
 * Parses a single email into an Email object
13
 *
14
 * @package Ingenerator\Mailhook
15
 * @see     spec\Ingenerator\Mailhook\EmailParserSpec
16
 */
17
class EmailParser {
18
19
	/**
20
	 * @param string $raw_message
21
	 *
22
	 * @return Email
23
	 */
24
	public function parse($raw_message)
25
	{
26
		list($headers, $content) = \explode("\n\n", $raw_message, 2);
27
28
		$data = array(
29
			'to'      => NULL,
30
			'subject' => NULL,
31
			'content' => \quoted_printable_decode($content)
32
		);
33
34
		$data['links'] = $this->parseLinksFromContent($data['content']);
35
		$data['to']    = $this->parseRecipient($headers);
36
37
		if (\preg_match('/^Subject:\s+(.+?)$/m', $headers, $matches))
38
		{
39
			$data['subject'] = $matches[1];
40
		}
41
42
		return new Email($data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,null|string|array<integer,string>>, but the function expects a array<integer,string>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
	}
44
45
	/**
46
	 * @param string $content
47
	 *
48
	 * @return string[]
49
	 */
50
	protected function parseLinksFromContent($content)
51
	{
52
		if (\preg_match_all('_https?://[^\s^"^<]+_', $content, $matches))
53
		{
54
			$links = $matches[0];
55
			return \array_values(\array_unique($links));
56
		}
57
		else
58
		{
59
			return array();
60
		}
61
	}
62
63
	/**
64
	* @param string $headers
65
	*
66
	* @return string
67
	*/
68
	protected function parseRecipient($headers)
69
	{
70
		if ( ! \preg_match('/^To:\s+([^<]*?)(<[^>]+>)?$/m', $headers, $matches))
71
			return NULL;
72
	
73
		if (isset($matches[2]) && $matches[2])
74
			return \trim($matches[2], '<>');
75
	
76
		return $matches[1];
77
	}
78
}
79