Completed
Push — master ( f7ad33...cd335b )
by Rudie
02:05
created

IMAPMessagePart   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 33.64 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 3
cbo 2
dl 36
loc 107
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 6
A parts() 0 3 1
A allParts() 20 20 4
A part() 0 4 1
A structure() 0 3 1
A parameters() 16 16 4
A parameter() 0 4 1
A content() 0 9 1
A decode() 0 3 1

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
namespace rdx\imap;
4
5
class IMAPMessagePart implements IMAPMessagePartInterface {
6
7
	public $section = [];
8
	public $subtype = '';
9
10
	public $parts = array();
11
	public $parameters = array();
12
13
	public $message; // typeof IMAPMessage
14
	public $structure; // typeof stdClass
15
	public $skippedParts = []; // Array<stdClass>
16
17
	public function __construct( IMAPMessage $message, $structure, array $section ) {
18
		$this->message = $message;
19
		$this->structure = $structure;
20
		$this->section = $section;
21
		$this->subtype = strtoupper($structure->subtype);
22
23
		if ( !empty($structure->parts) ) {
24
			$parts = $structure->parts;
25
26
			while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) {
27
				$this->skippedParts[] = $parts[0];
28
				$parts = $parts[0]->parts;
29
			}
30
31
			foreach ( $parts as $n => $part ) {
32
				$this->parts[] = new IMAPMessagePart(
33
					$this->message,
34
					$part,
35
					array_merge($this->section, [$n+1])
36
				);
37
			}
38
		}
39
	}
40
41
	public function parts() {
42
		return $this->parts;
43
	}
44
45 View Code Duplication
	public function allParts( $withContainers = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
		$parts = [];
47
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
48
			foreach ( $message->parts() as $part ) {
49
				if ( $part->parts() ) {
50
					if ( $withContainers ) {
51
						$parts[] = $part;
52
					}
53
54
					$iterate($part);
55
				}
56
				else {
57
					$parts[] = $part;
58
				}
59
			}
60
		};
61
62
		$iterate($this);
63
		return $parts;
64
	}
65
66
	public function part( $index ) {
67
		$parts = $this->parts();
68
		return @$parts[$index];
69
	}
70
71
	public function structure() {
72
		return $this->structure;
73
	}
74
75 View Code Duplication
	public function parameters() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
76
		if ( !$this->parameters ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
			$structure = $this->structure();
78
79
			$this->parameters['bytes'] = @$structure->bytes;
80
81
			foreach ((array) @$structure->parameters as $param) {
82
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
83
			}
84
			foreach ((array) @$structure->dparameters as $param) {
85
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
86
			}
87
		}
88
89
		return $this->parameters;
90
	}
91
92
	public function parameter( $name ) {
93
		$parameters = $this->parameters();
94
		return @$parameters[ strtolower($name) ];
95
	}
96
97
	public function content() {
98
		$body = imap_fetchbody(
99
			$this->message->mailbox->imap(),
100
			$this->message->msgNumber,
101
			implode('.', $this->section),
102
			FT_PEEK
103
		);
104
		return $this->decode($body);
105
	}
106
107
	public function decode( $content ) {
108
		return quoted_printable_decode($content);
109
	}
110
111
}
112