Test Failed
Pull Request — master (#171)
by Zaahid
04:55
created

PartFilter::failsMultiPartFilter()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
nc 6
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 5
rs 9.6111
c 1
b 0
f 0
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 IMessagePart
14
 * 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
    public static function fromAttachmentFilter()
32
    {
33
        return function (IMessagePart $part) {
34
            $type = strtolower($part->getContentType());
35
            if (in_array($type, [ 'text/plain', 'text/html' ]) && strcasecmp($part->getContentDisposition(), 'inline') === 0) {
36
                return false;
37
            }
38
            return !(($part instanceof IMimePart) && ($part->isMultiPart() || $part->isSignaturePart()));
39
        };
40
    }
41
42
    /**
43
     * Provides a filter that keeps parts that contain a header of $name with a
44
     * value that matches $value (case insensitive).
45
     *
46
     * By default signed parts are excluded. Pass FALSE to the third parameter
47
     * to include them.
48
     *
49
     * @param string $name the header name to look up
50
     * @param string $value the value to match
51
     * @param bool $excludeSignedParts
52
     * @return callable
53
     */
54
    public static function fromHeaderValue($name, $value, $excludeSignedParts = true)
55
    {
56
        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...
57
            if ($part instanceof IMimePart) {
58
                if ($excludeSignedParts && $part->isSignaturePart()) {
59
                    return false;
60
                }
61
                return strcasecmp($part->getHeaderValue($name), $value) === 0;
62
            }
63
            return false;
64
        };
65
    }
66
67
    /**
68
     * Includes only parts that match the passed $mimeType in the return value
69
     * of a call to 'getContentType()'.
70
     * 
71
     * @param string $mimeType
72
     * @return callable
73
     */
74
    public static function fromContentType($mimeType)
75
    {
76
        return function(IMessagePart $part) use ($mimeType) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
77
            return strcasecmp($part->getContentType(), $mimeType) === 0;
78
        };
79
    }
80
81
    /**
82
     * Returns parts matching $mimeType that do not have a Content-Disposition
83
     * set to 'attachment'.
84
     *
85
     * @param string $mimeType
86
     * @return callable
87
     */
88
    public static function fromInlineContentType($mimeType)
89
    {
90
        return function(IMessagePart $part) use ($mimeType) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
91
            return strcasecmp($part->getContentType(), $mimeType) === 0
92
                && strcasecmp($part->getContentDisposition(), 'attachment') !== 0;
93
        };
94
    }
95
96
    /**
97
     * Finds parts with the passed disposition (matching against
98
     * IMessagePart::getContentDisposition()), optionally including
99
     * multipart parts and signed parts.
100
     * 
101
     * @param string $disposition
102
     * @param bool $includeMultipart
103
     * @param bool $includeSignedParts
104
     * @return type
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
     */
106
    public static function fromDisposition($disposition, $includeMultipart = false, $includeSignedParts = false)
107
    {
108 12
        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...
109
            if (($part instanceof IMimePart) && ((!$includeMultipart && $part->isMultiPart()) || (!$includeSignedParts && $part->isSignaturePart()))) {
110 12
                return false;
111
            }
112 12
            return strcasecmp($part->getContentDisposition(), $disposition) === 0;
113 12
        };
114
    }
115
}
116