Completed
Push — 5.x ( 99e715...8b54b2 )
by Lars
03:38
created

Swift_Mime_Headers_AbstractHeader::createPhrase()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 4
eloc 13
c 3
b 2
f 0
nc 4
nop 5
dl 0
loc 25
ccs 7
cts 7
cp 1
crap 4
rs 8.5806
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * An abstract base MIME Header.
13
 *
14
 * @author Chris Corbyn
15
 */
16
abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?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\t]*(?:\r\n))?[ \t])?"(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)';
19
20
    /**
21
     * The name of this Header.
22
     *
23
     * @var string
24
     */
25
    private $_name;
26
27
    /**
28
     * The Encoder used to encode this Header.
29
     *
30
     * @var Swift_Mime_HeaderEncoder
31
     */
32
    private $_encoder;
33
34
    /**
35
     * The maximum length of a line in the header.
36
     *
37
     * @var int
38
     */
39
    private $_lineLength = 78;
40
41
    /**
42
     * The language used in this Header.
43
     *
44
     * @var string|null
45
     */
46
    private $_lang;
47
48
    /**
49
     * The character set of the text in this Header.
50
     *
51
     * @var string
52
     */
53
    private $_charset = 'utf-8';
54
55
    /**
56
     * The value of this Header, cached.
57
     *
58
     * @var string|null
59
     */
60
    private $_cachedValue;
61
62
    /**
63
     * Creates a new Header.
64
     */
65 256
    public function __construct()
66
    {
67 256
    }
68
69
    /**
70
     * Set the character set used in this Header.
71
     *
72
     * @param string $charset
73
     */
74 125
    public function setCharset($charset)
75
    {
76 125
        if ($charset && $charset !== $this->_charset) {
77 19
            $this->clearCachedValue();
78
79 19
            $this->_charset = $charset;
80
81 19
            if ($this->_encoder) {
82 19
                $this->_encoder->charsetChanged($charset);
83
            }
84
        }
85 125
    }
86
87
    /**
88
     * Get the character set used in this Header.
89
     *
90
     * @return string
91
     */
92 83
    public function getCharset()
93
    {
94 83
        return $this->_charset;
95
    }
96
97
    /**
98
     * Set the language used in this Header.
99
     *
100
     * For example, for US English, 'en-us'.
101
     * This can be unspecified.
102
     *
103
     * @param string $lang
104
     */
105 4
    public function setLanguage($lang)
106
    {
107 4
        if ($lang && $this->_lang !== $lang) {
108 4
            $this->clearCachedValue();
109 4
            $this->_lang = (string)$lang;
110
        }
111 4
    }
112
113
    /**
114
     * Get the language used in this Header.
115
     *
116
     * @return string
117
     */
118 7
    public function getLanguage()
119
    {
120 7
        return $this->_lang;
121
    }
122
123
    /**
124
     * Set the encoder used for encoding the header.
125
     *
126
     * @param Swift_Mime_HeaderEncoder $encoder
127
     */
128 216
    public function setEncoder(Swift_Mime_HeaderEncoder $encoder)
129
    {
130 216
        $this->_encoder = $encoder;
131 216
        $this->_encoder->charsetChanged($this->_charset);
132
133 216
        $this->setCachedValue(null);
134 216
    }
135
136
    /**
137
     * Get the encoder used for encoding this Header.
138
     *
139
     * @return Swift_Mime_HeaderEncoder
140
     */
141 77
    public function getEncoder()
142
    {
143 77
        return $this->_encoder;
144
    }
145
146
    /**
147
     * Get the name of this header (e.g. charset).
148
     *
149
     * @return string
150
     */
151 153
    public function getFieldName()
152
    {
153 153
        return $this->_name;
154
    }
155
156
    /**
157
     * Set the maximum length of lines in the header (excluding EOL).
158
     *
159
     * @param int $lineLength
160
     *
161
     * @return bool
162
     */
163 20
    public function setMaxLineLength($lineLength)
164
    {
165 20
        if ($lineLength && $this->_lineLength !== $lineLength) {
166 16
            $this->clearCachedValue();
167
168 16
            $this->_lineLength = (int)$lineLength;
169
170 16
            return true;
171
        }
172
173 5
        return false;
174
    }
175
176
    /**
177
     * Get the maximum permitted length of lines in this Header.
178
     *
179
     * @return int
180
     */
181 82
    public function getMaxLineLength()
182
    {
183 82
        return $this->_lineLength;
184
    }
185
186
    /**
187
     * Get this Header rendered as a RFC 2822 compliant string.
188
     *
189
     * @return string
190
     */
191 157
    public function toString()
192
    {
193 157
        return $this->_tokensToString($this->toTokens());
194
    }
195
196
    /**
197
     * Returns a string representation of this object.
198
     *
199
     * @return string
200
     *
201
     * @see toString()
202
     */
203
    public function __toString()
204
    {
205
        return $this->toString();
206
    }
207
208
    /**
209
     * Set the name of this Header field.
210
     *
211
     * @param string $name
212
     */
213 256
    protected function setFieldName($name)
214
    {
215 256
        $this->_name = $name;
216 256
    }
217
218
    /**
219
     * Produces a compliant, formatted RFC 2822 'phrase' based on the string given.
220
     *
221
     * @param Swift_Mime_Header        $header
222
     * @param string                   $string  as displayed
223
     * @param string                   $charset of the text
224
     * @param Swift_Mime_HeaderEncoder $encoder
225
     * @param bool                     $shorten the first line to make remove for header name
226 77
     *
227
     * @return string
228
     */
229 77
    protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false)
0 ignored issues
show
Unused Code introduced by
The parameter $charset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $encoder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
230
    {
231
        // Treat token as exactly what was given
232 77
        $phraseStr = $string;
233
        // If it's not valid
234 6
        if (!preg_match('/^' . self::PHRASE_PATTERN . '$/D', $phraseStr)) {
235
            // .. but it is just ascii text, try escaping some characters
236 3
            // and make it a quoted-string
237
            if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) {
238
                $phraseStr = $this->escapeSpecials($phraseStr, array('"'));
239 4
                $phraseStr = '"' . $phraseStr . '"';
240
            } else {
241
                // ... otherwise it needs encoding
242
                // Determine space remaining on line if first line
243 6
                if ($shorten) {
244
                    $usedLength = strlen($header->getFieldName() . ': ');
245
                } else {
246 77
                    $usedLength = 0;
247
                }
248
                $phraseStr = $this->encodeWords($header, $string, $usedLength);
249
            }
250
        }
251
252
        return $phraseStr;
253
    }
254
255
    /**
256
     * Escape special characters in a string (convert to quoted-pairs).
257 3
     *
258
     * @param string   $token
259 3
     * @param string[] $include additional chars to escape
260 3
     *
261
     * @return string
262
     */
263 3
    protected function escapeSpecials($token, array $include = array())
264
    {
265
        foreach (array_merge(array('\\'), $include) as $char) {
266
            $token = str_replace($char, '\\' . $char, $token);
267
        }
268
269
        return $token;
270
    }
271
272
    /**
273
     * Encode needed word tokens within a string of input.
274 161
     *
275
     * @param Swift_Mime_Header|Swift_Mime_Headers_AbstractHeader $header
276 161
     * @param string            $input
277
     * @param int               $usedLength optional
278 161
     *
279
     * @return string
280 161
     */
281
    protected function encodeWords(Swift_Mime_Header $header, $input, $usedLength = -1)
282 161
    {
283
        $value = '';
284 16
        $tokens = $this->getEncodableWordTokens($input);
285
        foreach ($tokens as $token) {
286 16
            // See RFC 2822, Sect 2.2 (really 2.2 ??)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
287 12
            if ($this->tokenNeedsEncoding($token)) {
288 5
                // Don't encode starting WSP
289 5
                $firstChar = substr($token, 0, 1);
290
                switch ($firstChar) {
291
                    case ' ':
292 16
                    case "\t":
293 16
                        $value .= $firstChar;
294
                        $token = substr($token, 1);
295 16
                }
296
                if (-1 == $usedLength) {
297 161
                    $usedLength = strlen($header->getFieldName().': ') + strlen($value);
298
                }
299
                $value .= $this->getTokenAsEncodedWord($token, $usedLength);
300
                $header->setMaxLineLength(76); // Forcefully override
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Swift_Mime_Header as the method setMaxLineLength() does only exist in the following implementations of said interface: Swift_Mime_Headers_AbstractHeader, Swift_Mime_Headers_DateHeader, Swift_Mime_Headers_IdentificationHeader, Swift_Mime_Headers_MailboxHeader, Swift_Mime_Headers_ParameterizedHeader, Swift_Mime_Headers_PathHeader, Swift_Mime_Headers_UnstructuredHeader.

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...
301 161
            } else {
302
                $value .= $token;
303
            }
304
        }
305
        return $value;
306
    }
307
308
    /**
309
     * Test if a token needs to be encoded or not.
310
     *
311 161
     * @param string $token
312
     *
313 161
     * @return integer
314 16
     */
315
    protected function tokenNeedsEncoding($token)
316 150
    {
317
        if (preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token)) {
318
            return 1;
319
        } else {
320
            return 0;
321
        }
322
    }
323
324
    /**
325
     * Splits a string into tokens in blocks of words which can be encoded quickly.
326
     *
327 161
     * @param string $string
328
     *
329 161
     * @return string[]
330
     */
331 161
    protected function getEncodableWordTokens($string)
332
    {
333 161
        $tokens = array();
334 161
335 16
        $encodedToken = '';
336
        // Split at all whitespace boundaries
337 150
        foreach (preg_split('~(?=[\t ])~', $string) as $token) {
338 3
            if ($this->tokenNeedsEncoding($token)) {
339 3
                $encodedToken .= $token;
340
            } else {
341 161
                if ($encodedToken !== '') {
342
                    $tokens[] = $encodedToken;
343
                    $encodedToken = '';
344
                }
345 161
                $tokens[] = $token;
346 16
            }
347
        }
348
349 161
        if ($encodedToken !== '') {
350
            $tokens[] = $encodedToken;
351
        }
352
353
        return $tokens;
354
    }
355
356
    /**
357
     * Get a token as an encoded word for safe insertion into headers.
358
     *
359
     * @param string $token           token to encode
360 17
     * @param int    $firstLineOffset optional
361
     *
362
     * @return string
363
     */
364 17
    protected function getTokenAsEncodedWord($token, $firstLineOffset = 0)
365
    {
366 17
        // Adjust $firstLineOffset to account for space needed for syntax.
367 2
368
        $charsetDecl = $this->_charset;
369
370 17
        if ($this->_lang) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_lang of type string|null is loosely compared to true; 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...
371
            $charsetDecl .= '*' . $this->_lang;
372 17
        }
373
374
        $encodingWrapperLength = strlen('=?' . $charsetDecl . '?' . $this->_encoder->getName() . '??=');
375
376
        if ($firstLineOffset >= 75) {
377
            // Does this logic need to be here?
378 17
            $firstLineOffset = 0;
379 17
        }
380 17
381
        /** @noinspection PhpMethodParametersCountMismatchInspection */
382
        $encodedTextLines = explode(
383 17
            "\r\n",
384
            $this->_encoder->encodeString($token, $firstLineOffset, 75 - $encodingWrapperLength, $this->_charset)
0 ignored issues
show
Unused Code introduced by
The call to Swift_Mime_HeaderEncoder::encodeString() has too many arguments starting with $this->_charset.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
385 17
        );
386 17
387
        if (Swift::strtolowerWithStaticCache($this->_charset) !== 'iso-2022-jp') {
388
            // special encoding for iso-2022-jp using mb_encode_mimeheader
389
            foreach ($encodedTextLines as $lineNum => $line) {
390 17
                $encodedTextLines[$lineNum] = '=?' . $charsetDecl . '?' . $this->_encoder->getName() . '?' . $line . '?=';
391
            }
392
        }
393
394
        return implode("\r\n ", $encodedTextLines);
395
    }
396
397
    /**
398
     * Generates tokens from the given string which include CRLF as individual tokens.
399
     *
400 157
     * @param string $token
401
     *
402 157
     * @return string[]
403
     */
404
    protected function generateTokenLines($token)
405
    {
406
        return preg_split('~(\r\n)~', $token, -1, PREG_SPLIT_DELIM_CAPTURE);
407
    }
408
409
    /**
410 245
     * Set a value into the cache.
411
     *
412 245
     * @param string $value
413 245
     */
414
    protected function setCachedValue($value)
415
    {
416
        $this->_cachedValue = $value;
417
    }
418
419
    /**
420 176
     * Get the value in the cache.
421
     *
422 176
     * @return string
423
     */
424
    protected function getCachedValue()
425 206
    {
426
        return $this->_cachedValue;
427 206
    }
428 206
429
    private function clearCachedValue()
430
    {
431
        $this->setCachedValue(null);
432
    }
433
434
    /**
435 203
     * Clear the cached value if $condition is met.
436
     *
437 203
     * @param bool $condition
438 203
     */
439
    protected function clearCachedValueIf($condition)
440 203
    {
441
        if (true === $condition) {
442
            $this->clearCachedValue();
443
        }
444
    }
445
446
    /**
447
     * Generate a list of all tokens in the final header.
448
     *
449 157
     * @param string $string The string to tokenize
450
     *
451 157
     * @return array An array of tokens as strings
452 152
     */
453
    protected function toTokens($string = null)
454
    {
455 157
        if (null === $string) {
456
            $string = $this->getFieldBody();
457
        }
458 157
459 157
        $tokens = array();
460 157
461 157
        // Generate atoms; split at all invisible boundaries followed by WSP
462
        foreach (preg_split('~(?=[ \t])~', $string) as $token) {
463
            $newTokens = $this->generateTokenLines($token);
464
            foreach ($newTokens as $newToken) {
465 157
                $tokens[] = $newToken;
466
            }
467
        }
468
469
        return $tokens;
470
    }
471
472
    /**
473
     * Takes an array of tokens which appear in the header and turns them into
474
     * an RFC 2822 compliant string, adding FWSP where needed.
475
     *
476 157
     * @param string[] $tokens
477
     *
478 157
     * @return string
479 157
     */
480 157
    private function _tokensToString(array $tokens)
481 157
    {
482
        $lineCount = 0;
483
        $headerLines = array();
484 157
        $headerLines[] = $this->_name . ': ';
485
        $currentLine = &$headerLines[$lineCount++];
486
487 157
        // Build all tokens back into compliant header
488
        foreach ($tokens as $i => $token) {
489
            // Line longer than specified maximum or token was just a new line
490 157
            if (
491
                ("\r\n" === $token)
492
                ||
493 157
                (
494
                    strlen($currentLine) > 0
495 157
                    &&
496
                    (
497
                        $i > 0
498
                        &&
499 48
                        strlen($currentLine . $token) > $this->_lineLength
500 48
                    )
501
                )
502
            ) {
503
                $headerLines[] = '';
504 157
                $currentLine = &$headerLines[$lineCount++];
505 157
            }
506
507
            // Append token to the line
508
            if ("\r\n" !== $token) {
509
                $currentLine .= $token;
510 157
            }
511
        }
512
513
        // Implode with FWS (RFC 2822, 2.2.3)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
514
        return implode("\r\n", $headerLines) . "\r\n";
515
    }
516 5
517
    /**
518 5
     * Make a deep copy of object
519 5
     */
520
    public function __clone()
521 5
    {
522
        if ($this->_encoder !== null) {
523
            $this->_encoder = clone $this->_encoder;
524
        }
525
    }
526
}
527