Completed
Push — master ( c1617c...820ca9 )
by Rudie
02:11
created

IMAPMessagePart   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 116
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 23
c 4
b 0
f 0
lcom 1
cbo 3
dl 36
loc 116
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 3 1
A message() 0 3 1
A section() 0 3 1
A subtype() 0 3 1
A __construct() 0 8 1
B parts() 0 17 6
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 7 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
use rdx\imap\IMAPMessage;
6
use rdx\imap\IMAPMessagePart;
7
8
class IMAPMessagePart implements IMAPMessagePartInterface {
9
10
	protected $section = [];
11
	protected $subtype = '';
12
13
	protected $parts = [];
14
	protected $parameters = [];
15
16
	protected $message; // typeof IMAPMessage
17
	protected $structure; // typeof stdClass
18
	protected $skippedParts = []; // Array<stdClass>
19
20
	public function __construct( IMAPMessage $message, $structure, array $section ) {
21
		$this->message = $message;
22
		$this->structure = $structure;
23
		$this->section = $section;
24
		$this->subtype = strtoupper($structure->subtype);
25
26
		$this->parts();
27
	}
28
29
	public function parts() {
30
		if ( !empty($this->structure->parts) ) {
31
			$parts = $this->structure->parts;
32
33
			while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) {
34
				$this->skippedParts[] = $parts[0];
35
				$parts = $parts[0]->parts;
36
			}
37
38
			foreach ( $parts as $n => $part ) {
39
				$this->parts[] = $this->message()->createMessagePart(
40
					$part,
41
					array_merge($this->section(), [$n+1])
42
				);
43
			}
44
		}
45
	}
46
47 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...
48
		$parts = [];
49
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
50
			foreach ( $message->parts() as $part ) {
51
				if ( $part->parts() ) {
52
					if ( $withContainers ) {
53
						$parts[] = $part;
54
					}
55
56
					$iterate($part);
57
				}
58
				else {
59
					$parts[] = $part;
60
				}
61
			}
62
		};
63
64
		$iterate($this);
65
		return $parts;
66
	}
67
68
	public function part( $index ) {
69
		$parts = $this->parts();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parts is correct as $this->parts() (which targets rdx\imap\IMAPMessagePart::parts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
		return @$parts[$index];
71
	}
72
73
	public function structure() {
74
		return $this->structure;
75
	}
76
77 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...
78
		if ( empty($this->parameters) ) {
79
			$structure = $this->structure();
80
81
			$this->parameters['bytes'] = @$structure->bytes;
82
83
			foreach ((array) @$structure->parameters as $param) {
84
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
85
			}
86
			foreach ((array) @$structure->dparameters as $param) {
87
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
88
			}
89
		}
90
91
		return $this->parameters;
92
	}
93
94
	public function parameter( $name ) {
95
		$parameters = $this->parameters();
96
		return @$parameters[ strtolower($name) ];
97
	}
98
99
	public function content() {
100
		$body = $this->message()->mailbox()->imap()->fetchbody(
101
			$this->message()->msgNumber(),
102
			implode('.', $this->section())
103
		);
104
		return $this->decode($body);
105
	}
106
107
	public function decode( $content ) {
108
		return quoted_printable_decode($content);
109
	}
110
111
	public function message() {
112
		return $this->message;
113
	}
114
115
	public function section() {
116
		return $this->section;
117
	}
118
119
	public function subtype() {
120
		return $this->subtype;
121
	}
122
123
}
124