Completed
Push — master ( 0c6754...1d3a5f )
by Asmir
08:13
created

MimeParser::getContentType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Goetas\Mail\ToSwiftMailParser;
4
5
use Goetas\Mail\ToSwiftMailParser\Mime\ContentDecoder;
6
use Goetas\Mail\ToSwiftMailParser\Mime\HeaderDecoder;
7
8
class MimeParser
9
{
10
    protected $removeHeaders = array("Received", "From", "X-Original-To", "MIME-Version", "Received-SPF", "Delivered-To");
11
    protected $allowedHeaders = array("return-path", "subject");
12
    private $cache;
13
    private $grammar;
14
    private $contentDecoder;
15
    private $headerDecoder;
16
17 1
    public function __construct(array $allowedHeaders = array(), array $removeHeaders = array())
18
    {
19 1
        $this->cache = \Swift_DependencyContainer::getInstance()->lookup('cache');
20 1
        $this->grammar = \Swift_DependencyContainer::getInstance()->lookup('mime.grammar');
21 1
        $this->contentDecoder = new ContentDecoder ();
22 1
        $this->headerDecoder = new HeaderDecoder ();
23
24 1
        $this->allowedHeaders = array_merge($this->allowedHeaders, $allowedHeaders);
25 1
        $this->removeHeaders = array_merge($this->removeHeaders, $removeHeaders);
26 1
    }
27
28
    /**
29
     *
30
     * @param string $string
31
     *            The message
32
     * @return \Swift_Message
33
     */
34
    public function parseString($string)
35
    {
36
        $fp = fopen("php://memory", "wb");
37
        fwrite($fp, $string);
38
        rewind($fp);
39
        $message = $this->parseStream($fp);
0 ignored issues
show
Documentation introduced by
$fp is of type resource, but the function expects a object<Goetas\Mail\ToSwiftMailParser\stream>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
        fclose($fp);
41
        return $message;
42
    }
43
44
    /**
45
     *
46
     * @param stream $stream
47
     * @param boolean $fillHeaders (default to false)
48
     * @param \Swift_Mime_MimeEntity $message (default to null)
49
     * @return Swift_Mime_MimeEntity|\Swift_Message
50
     */
51 1
    public function parseStream($stream, $fillHeaders = false, \Swift_Mime_MimeEntity $message = null)
52
    {
53 1
        $partHeaders = $this->extractHeaders($stream);
54
55 1
        $filteredHeaders = $this->filterHeaders($partHeaders);
56
57 1
        $parts = $this->parseParts($stream, $partHeaders);
58
59 1
        if (!$message) {
60 1
            $message = new \Swift_Message ();
61 1
        }
62
63 1
        $headers = $this->createHeadersSet($filteredHeaders);
64
65 1
        foreach ($headers->getAll() as $name => $header) {
66 1
            if ($fillHeaders || in_array(strtolower($header->getFieldName()), $this->allowedHeaders)) {
67 1
                $message->getHeaders()->set($header);
68 1
            }
69 1
        }
70 1
        $this->createMessage($parts, $message);
71
72 1
        return $message;
73
    }
74
75 1
    protected function extractHeaders($stream)
76
    {
77 1
        $headers = array();
78 1
        $hName = null;
79 1
        while (!feof($stream)) {
80 1
            $row = fgets($stream);
81 1
            if ($row == "\r\n" || $row == "\n" || $row == "\r") {
82 1
                break;
83
            }
84 1
            if (preg_match('/^([a-z0-9\-]+)\s*:(.*)/i', $row, $mch)) {
85 1
                $hName = strtolower($mch [1]);
86 1
                if (!in_array($hName, array("content-type", "content-transfer-encoding"))) {
87 1
                    $hName = $mch [1];
88 1
                }
89 1
                $row = $mch [2];
90 1
            }
91 1
            if (!$hName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
                continue;
93
            }
94 1
            $headers [$hName] [] = trim($row);
95 1
        }
96 1
        foreach ($headers as $header => $values) {
97 1
            $headers [$header] = $this->headerDecoder->decode(trim(implode(" ", $values)));
98 1
        }
99 1
        return $headers;
100
    }
101
102 1
    private function filterHeaders(array $headers)
103
    {
104 1
        foreach ($headers as $header => $values) {
105 1
            if (in_array(strtolower($header), $this->removeHeaders) && !in_array(strtolower($header), $this->allowedHeaders)) {
106
                unset ($headers [$header]);
107
            }
108 1
        }
109 1
        return $headers;
110
    }
111
112 1
    protected function parseParts($stream, $partHeaders)
113
    {
114 1
        $parts = array();
115 1
        $contentType = $this->extractValueHeader($this->getContentType($partHeaders));
116
117 1
        if (stripos($contentType, 'multipart/') !== false) {
118 1
            $headerParts = $this->extractHeaderParts($this->getContentType($partHeaders));
119 1
            $boundary = $headerParts ["boundary"];
120 1
        } else {
121
            $boundary = null;
122
        }
123
124
        try {
125
            // body
126 1
            $this->extractPart($stream, $boundary, $this->getTransferEncoding($partHeaders));
127 1
        } catch (Exception\EndOfPartReachedException $e) {
128
            $parts = array(
129 1
                "type" => $contentType,
130 1
                "headers" => $partHeaders,
131 1
                "body" => $e->getData(),
132 1
                "boundary" => $boundary,
133 1
                "parts" => array()
134 1
            );
135
        }
136
137 1
        if ($boundary) {
138 1
            while (!feof($stream)) {
139
                try {
140 1
                    $partHeaders = $this->extractHeaders($stream);
141 1
                    $childContentType = $this->extractValueHeader($this->getContentType($partHeaders));
142
143 1
                    if (stripos($childContentType, 'multipart/') !== false) {
144
                        $parts ["parts"] [] = $this->parseParts($stream, $partHeaders);
145
                        try {
146
                            $this->extractPart($stream, $boundary, $this->getTransferEncoding($partHeaders));
147
                        } catch (Exception\EndOfPartReachedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
148
                        }
149
                    } else {
150 1
                        $this->extractPart($stream, $boundary, $this->getTransferEncoding($partHeaders));
151
                    }
152 1
                } catch (Exception\EndOfPartReachedException $e) {
153 1
                    $parts ["parts"] [] = array(
154 1
                        "type" => $childContentType,
0 ignored issues
show
Bug introduced by
The variable $childContentType does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
155 1
                        "parent-type" => $contentType,
156 1
                        "headers" => $partHeaders,
157 1
                        "body" => $e->getData(),
158 1
                        "parts" => array()
159 1
                    );
160
161 1
                    if ($e instanceof Exception\EndOfMultiPartReachedException) {
162 1
                        break;
163
                    }
164
                }
165 1
            }
166 1
        }
167 1
        return $parts;
168
    }
169
170 1
    private function extractValueHeader($header)
171
    {
172 1
        $pos = stripos($header, ';');
173 1
        if ($pos !== false) {
174 1
            return substr($header, 0, $pos);
175
        } else {
176 1
            return $header;
177
        }
178
    }
179
180 1
    private function getContentType(array $partHeaders)
181
    {
182 1
        if (array_key_exists('content-type', $partHeaders)) {
183 1
            return $partHeaders['content-type'];
184
        }
185
186
        return '';
187
    }
188
189 1
    private function extractHeaderParts($header)
190
    {
191 1
        if (stripos($header, ';') !== false) {
192
193 1
            $parts = explode(";", $header);
194 1
            array_shift($parts);
195 1
            $p = array();
196 1
            foreach ($parts as $pv) {
197 1
                if (!trim($pv)) {
198 1
                    continue;
199
                }
200 1
                list ($k, $v) = explode("=", trim($pv), 2);
201 1
                $p [$k] = trim($v, '"');
202 1
            }
203 1
            return $p;
204
        } else {
205 1
            return array();
206
        }
207
    }
208
209 1
    protected function extractPart($stream, $boundary, $encoding)
210
    {
211 1
        $rows = array();
212 1
        while (!feof($stream)) {
213 1
            $row = fgets($stream);
214
215 1
            if ($boundary !== null) {
216 1 View Code Duplication
                if (strpos($row, "--$boundary--") === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
217 1
                    throw new Exception\EndOfMultiPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
218
                }
219 1 View Code Duplication
                if (strpos($row, "--$boundary") === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
220 1
                    throw new Exception\EndOfPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
221
                }
222 1
            }
223 1
            $rows [] = $row;
224 1
        }
225
        throw new Exception\EndOfMultiPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
226
    }
227
228 1
    private function getTransferEncoding(array $partHeaders)
229
    {
230 1
        if (array_key_exists('content-transfer-encoding', $partHeaders)) {
231
            return $partHeaders ['content-transfer-encoding'];
232
        }
233
234 1
        return '';
235
    }
236
237
    /**
238
     *
239
     * @param array $headersRaw
240
     * @return \Swift_Mime_HeaderSet
241
     */
242 1
    protected function createHeadersSet(array $headersRaw)
243
    {
244 1
        $headers = \Swift_DependencyContainer::getInstance()->lookup('mime.headerset');
245
246 1
        foreach ($headersRaw as $name => $value) {
247 1
            switch (strtolower($name)) {
248 1
                case "content-type" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
249 1
                    $parts = $this->extractHeaderParts($value);
250 1
                    unset ($parts ["boundary"]);
251 1
                    $headers->addParameterizedHeader($name, $this->extractValueHeader($value), $parts);
252 1
                    break;
253 1
                case "return-path" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
254
                    if (preg_match_all('/([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})/i', $value, $mch)) {
255
                        foreach ($mch [0] as $k => $mails) {
256
                            $headers->addPathHeader($name, $mch [1] [$k]);
257
                        }
258
                    }
259
                    break;
260 1
                case "date":
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
261
                    $headers->addDateHeader($name, strtotime($value));
262 1
                case "to":
263 1
                case "from" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
264 1
                case "bcc" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
265 1
                case "reply-to" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
266 1
                case "cc" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
267 1
                    $adresses = array();
268 1
                    if (preg_match_all('/(.*?)<([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})>\s*[;,]*/i', $value, $mch)) {
269 1
                        foreach ($mch [0] as $k => $mail) {
270 1
                            if (!$mch [1] [$k]) {
271
                                $adresses [$mch [2] [$k]] = trim($mch [2] [$k]);
272
                            } else {
273 1
                                $adresses [$mch [2] [$k]] = trim($mch [1] [$k]);
274
                            }
275 1
                        }
276 1
                    } elseif (preg_match_all('/([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})/i', $value, $mch)) {
277
                        foreach ($mch [0] as $k => $mails) {
278
                            $adresses [$mch [1] [$k]] = trim($mch [1] [$k]);
279
                        }
280
                    }
281 1
                    $headers->addMailboxHeader($name, $adresses);
282 1
                    break;
283 1
                default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
284 1
                    $headers->addTextHeader($name, $value);
285 1
                    break;
286 1
            }
287 1
        }
288 1
        return $headers;
289
    }
290
291 1
    protected function createMessage(array $message, \Swift_Mime_MimeEntity $entity)
292
    {
293 1
        if (stripos($message ["type"], 'multipart/') !== false) {
294
295 1
            if (strpos($message ["type"], '/alternative')) {
296
                $nestingLevel = \Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE;
297 1
            } elseif (strpos($message ["type"], '/related')) {
298
                $nestingLevel = \Swift_Mime_MimeEntity::LEVEL_RELATED;
299 1
            } elseif (strpos($message ["type"], '/mixed')) {
300 1
                $nestingLevel = \Swift_Mime_MimeEntity::LEVEL_MIXED;
301 1
            }
302
303 1
            $childrens = array();
304 1
            foreach ($message ["parts"] as $part) {
305
306 1
                $headers = $this->createHeadersSet($part ["headers"]);
307 1
                $encoder = $this->getEncoder($this->getTransferEncoding($part ["headers"]));
308
309 1
                if (stripos($part ["type"], 'multipart/') !== false) {
310
                    $newEntity = new \Swift_Mime_MimePart ($headers, $encoder, $this->cache, $this->grammar);
311
                } else {
312 1
                    $newEntity = new \Swift_Mime_SimpleMimeEntity ($headers, $encoder, $this->cache, $this->grammar);
313
                }
314
315 1
                $this->createMessage($part, $newEntity);
316
317 1
                $ref = new \ReflectionObject ($newEntity);
318 1
                $m = $ref->getMethod('_setNestingLevel');
319 1
                $m->setAccessible(true);
320 1
                $m->invoke($newEntity, $nestingLevel);
0 ignored issues
show
Bug introduced by
The variable $nestingLevel does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
321
322 1
                $childrens [] = $newEntity;
323 1
            }
324
325 1
            $entity->setContentType($part ["type"]);
0 ignored issues
show
Bug introduced by
The variable $part seems to be defined by a foreach iteration on line 304. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Swift_Mime_MimeEntity as the method setContentType() does only exist in the following implementations of said interface: Swift_Attachment, Swift_EmbeddedFile, Swift_Image, Swift_Message, Swift_MimePart, Swift_Mime_Attachment, Swift_Mime_EmbeddedFile, Swift_Mime_MimePart, Swift_Mime_SimpleMessage, Swift_Mime_SimpleMimeEntity, Swift_SignedMessage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
326 1
            $entity->setChildren($childrens);
327 1
        } else {
328 1
            $entity->setBody($message ["body"], $message ["type"]);
329
        }
330 1
    }
331
332
    /**
333
     *
334
     * @param string $type
335
     * @return \Swift_Mime_ContentEncoder
336
     */
337 1
    protected function getEncoder($type)
338
    {
339
        switch ($type) {
340 1
            case "base64" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
341
                return \Swift_DependencyContainer::getInstance()->lookup('mime.base64contentencoder');
342
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
343 1
            case "8bit" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
344
                return \Swift_DependencyContainer::getInstance()->lookup('mime.8bitcontentencoder');
345
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
346 1
            case "7bit" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
347
                return \Swift_DependencyContainer::getInstance()->lookup('mime.7bitcontentencoder');
348
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
349 1
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
350 1
                return \Swift_DependencyContainer::getInstance()->lookup('mime.qpcontentencoder');
351
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
352 1
        }
353
    }
354
355
    /**
356
     *
357
     * @param string $path
358
     *            The file containg a MIME message
359
     * @return \Swift_Message
360
     */
361
    public function parseFile($path)
362
    {
363
        $fp = fopen($path, "rb");
364
        $message = $this->parseStream($fp);
0 ignored issues
show
Documentation introduced by
$fp is of type resource, but the function expects a object<Goetas\Mail\ToSwiftMailParser\stream>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
365
        fclose($fp);
366
        return $message;
367
    }
368
}
369