Test Failed
Branch 1.0.0 (84f469)
by Zaahid
05:36
created

WritableMimePart::removePart()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message;
8
9
use ZBateson\MailMimeParser\Message\Writer\MimePartWriter;
10
11
/**
12
 * Description of WritableMimePart
13
 *
14
 * @author Zaahid Bateson
15
 */
16
class WritableMimePart
17
{
18
    /**
19
     * @var \ZBateson\MailMimeParser\Message\Writer\MimePartWriter the part
20
     *      writer for this MimePart
21
     */
22
    protected $partWriter = null;
23
24
    /**
25
     * Registers the passed part as a child of the current part.
26
     * 
27
     * If the $position parameter is non-null, adds the part at the passed
28
     * position index.
29
     *
30
     * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part
31
     * @param int $position
32
     */
33
    public function addPart(MimePart $part, $position = null)
34
    {
35
        if ($part !== $this) {
36
            $part->setParent($this);
37
            array_splice($this->parts, ($position === null) ? count($this->parts) : $position, 0, [ $part ]);
0 ignored issues
show
Bug introduced by
The property parts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        }
39
    }
40
41
    /**
42
     * Removes the child part from this part and returns its position or
43
     * null if it wasn't found.
44
     * 
45
     * Note that if the part is not a direct child of this part, the returned
46
     * position is its index within its parent (calls removePart on its direct
47
     * parent).
48
     *
49
     * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part
50
     * @return int or null if not found
51
     */
52
    public function removePart(MimePart $part)
53
    {
54
        $parent = $part->getParent();
55
        if ($this !== $parent && $parent !== null) {
56
            return $parent->removePart($part);
57
        } else {
58
            $position = array_search($part, $this->parts, true);
59
            if ($position !== false) {
60
                array_splice($this->parts, $position, 1);
61
                return $position;
62
            }
63
        }
64
        return null;
65
    }
66
67
    /**
68
     * Removes all parts that are matched by the passed PartFilter.
69
     * 
70
     * @param \ZBateson\MailMimeParser\Message\PartFilter $filter
71
     */
72
    public function removeAllParts(PartFilter $filter = null)
73
    {
74
        foreach ($this->getAllParts($filter) as $part) {
0 ignored issues
show
Bug introduced by
The method getAllParts() does not seem to exist on object<ZBateson\MailMime...ssage\WritableMimePart>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            $this->removePart($part);
76
        }
77
    }
78
79
80
    /**
81
     * Attaches the resource handle for the part's content.  The attached handle
82
     * is closed when the MimePart object is destroyed.
83
     *
84
     * @param resource $contentHandle
85
     */
86
    public function attachContentResourceHandle($contentHandle)
87
    {
88
        if ($this->handle !== null && $this->handle !== $contentHandle) {
89
            fclose($this->handle);
0 ignored issues
show
Bug introduced by
The property handle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
        }
91
        $this->handle = $contentHandle;
92
    }
93
    
94
    /**
95
     * Attaches the resource handle representing the original stream that
96
     * created this part (including any sub-parts).  The attached handle is
97
     * closed when the MimePart object is destroyed.
98
     * 
99
     * This stream is not modified or changed as the part is changed and is only
100
     * set during parsing in MessageParser.
101
     *
102
     * @param resource $handle
103
     */
104
    public function attachOriginalStreamHandle($handle)
105
    {
106
        if ($this->originalStreamHandle !== null && $this->originalStreamHandle !== $handle) {
0 ignored issues
show
Bug introduced by
The property originalStreamHandle does not seem to exist. Did you mean handle?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107
            fclose($this->originalStreamHandle);
0 ignored issues
show
Bug introduced by
The property originalStreamHandle does not seem to exist. Did you mean handle?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
        }
109
        $this->originalStreamHandle = $handle;
0 ignored issues
show
Bug introduced by
The property originalStreamHandle does not seem to exist. Did you mean handle?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
    }
111
    
112
    
113
    /**
114
     * Detaches the content resource handle from this part but does not close
115
     * it.
116
     */
117
    protected function detachContentResourceHandle()
118
    {
119
        $this->handle = null;
120
    }
121
122
    /**
123
     * Sets the content of the part to the passed string (effectively creates
124
     * a php://temp stream with the passed content and calls
125
     * attachContentResourceHandle with the opened stream).
126
     *
127
     * @param string $string
128
     */
129
    public function setContent($string)
130
    {
131
        $handle = fopen('php://temp', 'r+');
132
        fwrite($handle, $string);
133
        rewind($handle);
134
        $this->attachContentResourceHandle($handle);
135
    }
136
137
    
138
    /**
139
     * Adds a header with the given $name and $value.
140
     *
141
     * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and
142
     * registers it as a header.
143
     *
144
     * @param string $name
145
     * @param string $value
146
     */
147
    public function setRawHeader($name, $value)
148
    {
149
        $this->headers[strtolower($name)] = $this->headerFactory->newInstance($name, $value);
0 ignored issues
show
Bug introduced by
The property headers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property headerFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
150
    }
151
152
    /**
153
     * Removes the header with the given name
154
     *
155
     * @param string $name
156
     */
157
    public function removeHeader($name)
158
    {
159
        unset($this->headers[strtolower($name)]);
160
    }
161
162
    
163
    /**
164
     * Sets the parent part.
165
     *
166
     * @param \ZBateson\MailMimeParser\Message\Part\MessagePart $part
167
     */
168
    public function setParent(MessagePart $part)
169
    {
170
        $this->parent = $part;
0 ignored issues
show
Bug introduced by
The property parent does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
171
    }
172
}
173