Completed
Push — master ( 62f4cb...329d4d )
by Rudie
02:07
created

IMAPTransport   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 16 3
A headers() 0 3 1
A utf8() 0 3 1
A headerinfo() 0 3 1
A fetchstructure() 0 3 1
A fetchbody() 0 3 1
A expunge() 0 3 1
A delete() 0 3 1
A unflag() 0 3 1
A flag() 0 3 1
A mailboxmsginfo() 0 3 1
A iteratorToLowercaseArray() 0 8 2
1
<?php
2
3
namespace rdx\imap;
4
5
use rdx\imap\IMAPException;
6
use rdx\imap\IMAPTransportInterface;
7
8
class IMAPTransport implements IMAPTransportInterface {
9
10
	protected $resource; // imap_open() resource
11
12
	public function open( $server, $username, $password, $mailbox, array $flags ) {
13
		if ( !empty($flags) ) {
14
			$server .= '/' . implode('/', $flags);
15
		}
16
17
		$mailbox = '{' . $server . '}' . $mailbox;
18
		$this->resource = @imap_open($mailbox, $username, $password);
19
		if ( !$this->resource ) {
20
			$error = imap_last_error();
21
			imap_errors();
22
			imap_alerts();
23
			throw new IMAPException($error);
24
		}
25
26
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (rdx\imap\IMAPTransport) is incompatible with the return type declared by the interface rdx\imap\IMAPTransportInterface::open of type rdx\imap\rdx\imap\IMAPTransportInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
27
	}
28
29
	public function headers() {
30
		return imap_headers($this->resource);
31
	}
32
33
	public function utf8( $string ) {
34
		return imap_utf8($string);
35
	}
36
37
	public function headerinfo( $msgNumber ) {
38
		return $this->iteratorToLowercaseArray(imap_headerinfo($this->resource, $msgNumber));
39
	}
40
41
	public function unflag( $msgNumber, $flag ) {
42
		return imap_clearflag_full($this->resource, $msgNumber, $flag);
43
	}
44
45
	public function flag( $msgNumber, $flag ) {
46
		return imap_setflag_full($this->resource, $msgNumber, $flag);
47
	}
48
49
	public function fetchstructure( $msgNumber ) {
50
		return imap_fetchstructure($this->resource, $msgNumber);
51
	}
52
53
	public function fetchbody( $msgNumber, $section ) {
54
		return quoted_printable_decode(imap_fetchbody($this->resource, $msgNumber, $section, FT_PEEK));
55
	}
56
57
	public function expunge() {
58
		return imap_expunge($this->resource);
59
	}
60
61
	public function delete( $msgNumber ) {
62
		return imap_delete($this->resource, $msgNumber);
63
	}
64
65
	public function mailboxmsginfo() {
66
		return (object) $this->iteratorToLowercaseArray(imap_mailboxmsginfo($this->resource));
67
	}
68
69
	protected function iteratorToLowercaseArray( $iterator ) {
70
		$data = [];
71
		foreach ( $iterator as $name => $value ) {
72
			$data[ strtolower($name) ] = $value;
73
		}
74
75
		return $data;
76
	}
77
78
}
79