Completed
Push — master ( 147214...53e4d6 )
by Zaahid
09:05
created

ConvertStreamFilter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 8
loc 52
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onCreate() 0 15 2
A filter() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 * 
5
 * Idea taken from HHVM's ConvertFilter and coded to avoid requiring a
6
 * different license and because I don't want to deal with legalities.
7
 * 
8
 * Unfortunately the HHVM version seems to fail for larger base64-encoded files
9
 * (both encoding and decoding).  Once the stream needs to be buffered, if it
10
 * isn't buffered on a 3-byte chunk for encoding, or 4 byte-chunk for decoding,
11
 * it won't make sense.  Currently it is only used for quoted-printable encoding
12
 * and decoding.
13
 *
14
 * @license http://opensource.org/licenses/bsd-license.php BSD
15
 */
16
namespace ZBateson\MailMimeParser\Stream;
17
18
use php_user_filter;
19
20
class ConvertStreamFilter extends php_user_filter
21
{
22
    /**
23
     * Name used when registering with stream_filter_register.
24
     */
25
    const STREAM_FILTER_NAME = 'convert.*';
26
    
27
    /**
28
     * @var string function to call for encoding/decoding
29
     */
30
    private $fnFilterName;
31
    
32
    /**
33
     * Sets up which function should be called.
34
     * 
35
     * @return bool
36
     */
37
    public function onCreate()
38
    {
39
        // strip off 'convert.'
40
        $name = substr($this->filtername, 8);
41
        $this->isEncode = false;
0 ignored issues
show
Bug introduced by
The property isEncode 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...
42
        $aFilters = [
43
            'quoted-printable-encode' => true,
44
            'quoted-printable-decode' => true,
45
        ];
46
        if (!isset($aFilters[$name])) {
47
            return false;
48
        }
49
        $this->fnFilterName = str_replace('-', '_', $name);
50
        return true;
51
    }
52
    
53
    /**
54
     * Filter implementation converts calls the relevant encode/decode filter
55
     * and chunk_split if needed, before returning PSFS_PASS_ON.
56
     * 
57
     * @param resource $in
58
     * @param resource $out
59
     * @param int $consumed
60
     * @param bool $closing
61
     * @return int
62
     */
63 View Code Duplication
    public function filter($in, $out, &$consumed, $closing)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
    {
65
        while ($bucket = stream_bucket_make_writeable($in)) {
66
            $data = call_user_func($this->fnFilterName, $bucket->data);
67
            stream_bucket_append($out, stream_bucket_new($this->stream, $data));
1 ignored issue
show
Bug introduced by
The property stream 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...
68
        }
69
        return PSFS_PASS_ON;
70
    }
71
}