Passed
Pull Request — master (#171)
by Zaahid
03:23
created

PartFilter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 18
eloc 22
c 3
b 0
f 0
dl 0
loc 99
ccs 25
cts 25
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromContentType() 0 4 1
A fromInlineContentType() 0 5 2
A fromDisposition() 0 7 6
A fromHeaderValue() 0 10 4
A fromAttachmentFilter() 0 9 5
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\IMessagePart;
10
use ZBateson\MailMimeParser\Message\IMimePart;
11
12
/**
13
 * Collection of static methods that return callables for common IMultiPart
14
 * child filters.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
abstract class PartFilter
19
{
20
    /**
21
     * Provides an 'attachment' filter used by Message::getAttachmentPart.
22
     *
23
     * The method filters out the following types of parts:
24
     *  - text/plain and text/html parts that do not have an 'attachment'
25
     *    disposition
26
     *  - any part that returns true for isMultiPart()
27
     *  - any part that returns true for isSignaturePart()
28
     *
29
     * @return callable
30
     */
31 58
    public static function fromAttachmentFilter()
32
    {
33
        return function (IMessagePart $part) {
34 58
            $type = strtolower($part->getContentType());
35 58
            if (in_array($type, [ 'text/plain', 'text/html' ]) && strcasecmp($part->getContentDisposition(), 'inline') === 0) {
0 ignored issues
show
Bug introduced by
It seems like $part->getContentDisposition() can also be of type null; however, parameter $string1 of strcasecmp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            if (in_array($type, [ 'text/plain', 'text/html' ]) && strcasecmp(/** @scrutinizer ignore-type */ $part->getContentDisposition(), 'inline') === 0) {
Loading history...
36 51
                return false;
37
            }
38 58
            return !(($part instanceof IMimePart)
39 58
                && ($part->isMultiPart() || $part->isSignaturePart()));
40 58
        };
41
    }
42
43
    /**
44
     * Provides a filter that keeps parts that contain a header of $name with a
45
     * value that matches $value (case insensitive).
46
     *
47
     * By default signed parts are excluded. Pass FALSE to the third parameter
48
     * to include them.
49
     *
50
     * @param string $name The header name to look up
51
     * @param string $value The value to match
52
     * @param bool $excludeSignedParts Optional signed parts exclusion (defaults
53
     *        to true).
54
     * @return callable
55
     */
56 3
    public static function fromHeaderValue($name, $value, $excludeSignedParts = true)
57
    {
58
        return function(IMessagePart $part) use ($name, $value, $excludeSignedParts) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
59 3
            if ($part instanceof IMimePart) {
60 2
                if ($excludeSignedParts && $part->isSignaturePart()) {
61 1
                    return false;
62
                }
63 1
                return strcasecmp($part->getHeaderValue($name), $value) === 0;
0 ignored issues
show
Bug introduced by
It seems like $part->getHeaderValue($name) can also be of type null; however, parameter $string1 of strcasecmp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
                return strcasecmp(/** @scrutinizer ignore-type */ $part->getHeaderValue($name), $value) === 0;
Loading history...
64
            }
65 1
            return false;
66 3
        };
67
    }
68
69
    /**
70
     * Includes only parts that match the passed $mimeType in the return value
71
     * of a call to 'getContentType()'.
72
     * 
73
     * @param string $mimeType Mime type of parts to find.
74
     * @return callable
75
     */
76 10
    public static function fromContentType($mimeType)
77
    {
78
        return function(IMessagePart $part) use ($mimeType) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
79 10
            return strcasecmp($part->getContentType(), $mimeType) === 0;
80 10
        };
81
    }
82
83
    /**
84
     * Returns parts matching $mimeType that do not have a Content-Disposition
85
     * set to 'attachment'.
86
     *
87
     * @param string $mimeType Mime type of parts to find.
88
     * @return callable
89
     */
90 82
    public static function fromInlineContentType($mimeType)
91
    {
92
        return function(IMessagePart $part) use ($mimeType) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
93 82
            return strcasecmp($part->getContentType(), $mimeType) === 0
94 82
                && strcasecmp($part->getContentDisposition(), 'attachment') !== 0;
0 ignored issues
show
Bug introduced by
It seems like $part->getContentDisposition() can also be of type null; however, parameter $string1 of strcasecmp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                && strcasecmp(/** @scrutinizer ignore-type */ $part->getContentDisposition(), 'attachment') !== 0;
Loading history...
95 82
        };
96
    }
97
98
    /**
99
     * Finds parts with the passed disposition (matching against
100
     * IMessagePart::getContentDisposition()), optionally including
101
     * multipart parts and signed parts.
102
     * 
103
     * @param string $disposition The disposition to find.
104
     * @param bool $includeMultipart Optionally include multipart parts by
105
     *        passing true (defaults to false).
106
     * @param bool $includeSignedParts Optionally include signed parts (defaults
107
     *        to false).
108
     * @return callable
109
     */
110 7
    public static function fromDisposition($disposition, $includeMultipart = false, $includeSignedParts = false)
111
    {
112
        return function(IMessagePart $part) use ($disposition, $includeMultipart, $includeSignedParts) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
113 7
            if (($part instanceof IMimePart) && ((!$includeMultipart && $part->isMultiPart()) || (!$includeSignedParts && $part->isSignaturePart()))) {
114 3
                return false;
115
            }
116 7
            return strcasecmp($part->getContentDisposition(), $disposition) === 0;
0 ignored issues
show
Bug introduced by
It seems like $part->getContentDisposition() can also be of type null; however, parameter $string1 of strcasecmp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
            return strcasecmp(/** @scrutinizer ignore-type */ $part->getContentDisposition(), $disposition) === 0;
Loading history...
117 7
        };
118
    }
119
}
120