Completed
Pull Request — 5.x (#24)
by Lars
06:58
created

Swift_CharacterStream_NgCharacterStream   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 258
Duplicated Lines 3.49 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 9
loc 258
ccs 82
cts 100
cp 0.82
rs 10
wmc 26
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCharacterReaderFactory() 0 4 1
A __construct() 0 5 1
A setCharacterSet() 0 6 1
A flushContents() 0 8 1
A importString() 0 5 1
A importByteStream() 9 9 2
A readBytes() 0 9 2
C read() 0 52 12
A setPointer() 0 7 2
A write() 0 23 3

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
/*
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
 * A CharacterStream implementation which stores characters in an internal array.
13
 *
14
 * @author Xavier De Cock <[email protected]>
15
 */
16
class Swift_CharacterStream_NgCharacterStream implements Swift_CharacterStream
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
    /**
19
     * The char reader (lazy-loaded) for the current charset.
20
     *
21
     * @var Swift_CharacterReader
22
     */
23
    private $_charReader;
24
25
    /**
26
     * A factory for creating CharacterReader instances.
27
     *
28
     * @var Swift_CharacterReaderFactory
29
     */
30
    private $_charReaderFactory;
31
32
    /**
33
     * The character set this stream is using.
34
     *
35
     * @var string
36
     */
37
    private $_charset;
38
39
    /**
40
     * The data's stored as-is.
41
     *
42
     * @var string
43
     */
44
    private $_datas = '';
45
46
    /**
47
     * Number of bytes in the stream.
48
     *
49
     * @var int
50
     */
51
    private $_datasSize = 0;
52
53
    /**
54
     * Map.
55
     *
56
     * @var mixed
57
     */
58
    private $_map;
59
60
    /**
61
     * Map Type.
62
     *
63
     * @var int
64
     */
65
    private $_mapType = 0;
66
67
    /**
68
     * Number of characters in the stream.
69
     *
70
     * @var int
71
     */
72
    private $_charCount = 0;
73
74
    /**
75
     * Position in the stream.
76
     *
77
     * @var int
78
     */
79
    private $_currentPos = 0;
80
81
    /**
82
     * Constructor.
83
     *
84
     * @param Swift_CharacterReaderFactory $factory
85
     * @param string                       $charset
86
     */
87 3
    public function __construct(Swift_CharacterReaderFactory $factory, $charset)
88
    {
89 3
        $this->setCharacterReaderFactory($factory);
90 3
        $this->setCharacterSet($charset);
91 3
    }
92
93
    /* -- Changing parameters of the stream -- */
94
95
    /**
96
     * Set the character set used in this CharacterStream.
97
     *
98
     * @param string $charset
99
     */
100 3
    public function setCharacterSet($charset)
101
    {
102 3
        $this->_charset = $charset;
103 3
        $this->_charReader = null;
104 3
        $this->_mapType = 0;
105 3
    }
106
107
    /**
108
     * Set the CharacterReaderFactory for multi charset support.
109
     *
110
     * @param Swift_CharacterReaderFactory $factory
111
     */
112 3
    public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
113
    {
114 3
        $this->_charReaderFactory = $factory;
115 3
    }
116
117
    /**
118
     * @see Swift_CharacterStream::flushContents()
119
     */
120 3
    public function flushContents()
121
    {
122 3
        $this->_datas = null;
123 3
        $this->_map = null;
124 3
        $this->_charCount = 0;
125 3
        $this->_currentPos = 0;
126 3
        $this->_datasSize = 0;
127 3
    }
128
129
    /**
130
     * @see Swift_CharacterStream::importByteStream()
131
     *
132
     * @param Swift_OutputByteStream $os
133
     */
134 2 View Code Duplication
    public function importByteStream(Swift_OutputByteStream $os)
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...
135
    {
136 2
        $this->flushContents();
137 2
        $blocks = 512;
138 2
        $os->setReadPointer(0);
139 2
        while (false !== ($read = $os->read($blocks))) {
140 2
            $this->write($read);
0 ignored issues
show
Bug introduced by
It seems like $read defined by $os->read($blocks) on line 139 can also be of type boolean; however, Swift_CharacterStream_NgCharacterStream::write() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
141 2
        }
142 2
    }
143
144
    /**
145
     * @see Swift_CharacterStream::importString()
146
     *
147
     * @param string $string
148
     */
149 1
    public function importString($string)
150
    {
151 1
        $this->flushContents();
152 1
        $this->write($string);
153 1
    }
154
155
    /**
156
     * @see Swift_CharacterStream::read()
157
     *
158
     * @param int $length
159
     *
160
     * @return string|false false on error
161
     */
162 3
    public function read($length)
163
    {
164 3
        if ($this->_currentPos >= $this->_charCount) {
165 3
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Swift_CharacterStream::read of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
        }
167
168 3
        $ret = false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression false; of type false adds false to the return on line 212 which is incompatible with the return type declared by the interface Swift_CharacterStream::read of type string. It seems like you forgot to handle an error condition.
Loading history...
169 3
        $length = $this->_currentPos + $length > $this->_charCount ? $this->_charCount - $this->_currentPos : $length;
170 3
        switch ($this->_mapType) {
171 3
            case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
172 1
                $len = $length * $this->_map;
173 1
                $ret = substr($this->_datas, $this->_currentPos * $this->_map, $len);
174 1
                $this->_currentPos += $length;
175 1
                break;
176
177 3
            case Swift_CharacterReader::MAP_TYPE_INVALID:
178
                $ret = '';
179
                for (; $this->_currentPos < $length; ++$this->_currentPos) {
180
                    if (isset($this->_map[$this->_currentPos])) {
181
                        $ret .= '?';
182
                    } else {
183
                        $ret .= $this->_datas[$this->_currentPos];
184
                    }
185
                }
186
                break;
187
188 3
            case Swift_CharacterReader::MAP_TYPE_POSITIONS:
189 3
                $end = $this->_currentPos + $length;
190 3
                $end = $end > $this->_charCount ? $this->_charCount : $end;
191 3
                $ret = '';
192
193 3
                $start = 0;
194 3
                if ($this->_currentPos > 0) {
195 3
                    $start = $this->_map['p'][$this->_currentPos - 1];
196 3
                }
197
198 3
                $to = $start;
199 3
                for (; $this->_currentPos < $end; ++$this->_currentPos) {
200 3
                    if (isset($this->_map['i'][$this->_currentPos])) {
201
                        $ret .= substr($this->_datas, $start, $to - $start) . '?';
202
                        $start = $this->_map['p'][$this->_currentPos];
203
                    } else {
204 3
                        $to = $this->_map['p'][$this->_currentPos];
205
                    }
206 3
                }
207
208 3
                $ret .= substr($this->_datas, $start, $to - $start);
209 3
                break;
210 3
        }
211
212 3
        return $ret;
213
    }
214
215
    /**
216
     * @see Swift_CharacterStream::readBytes()
217
     *
218
     * @param int $length
219
     *
220
     * @return int[]|false false on error
221
     */
222 3
    public function readBytes($length)
223
    {
224 3
        $read = $this->read($length);
225 3
        if ($read !== false) {
226 3
            return array_map('ord', str_split($read, 1));
227
        }
228
229 3
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Swift_CharacterStream::readBytes of type integer[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
230
    }
231
232
    /**
233
     * @see Swift_CharacterStream::setPointer()
234
     *
235
     * @param int $charOffset
236
     */
237
    public function setPointer($charOffset)
238
    {
239
        if ($this->_charCount < $charOffset) {
240
            $charOffset = $this->_charCount;
241
        }
242
        $this->_currentPos = $charOffset;
243
    }
244
245
    /**
246
     * @see Swift_CharacterStream::write()
247
     *
248
     * @param string $chars
249
     */
250 3
    public function write($chars)
251
    {
252 3
        if (!isset($this->_charReader)) {
253 3
            $this->_charReader = $this->_charReaderFactory->getReaderFor($this->_charset);
254 3
            $this->_map = array();
255 3
            $this->_mapType = $this->_charReader->getMapType();
256 3
        }
257
258 3
        $ignored = '';
259 3
        $this->_datas .= $chars;
260 3
        $this->_charCount += $this->_charReader->getCharPositions(
261 3
            substr($this->_datas, $this->_datasSize),
262 3
            $this->_datasSize,
263 3
            $this->_map,
264
            $ignored
265 3
        );
266
267 3
        if ($ignored !== false) {
268 3
            $this->_datasSize = strlen($this->_datas) - strlen($ignored);
269 3
        } else {
270
            $this->_datasSize = strlen($this->_datas);
271
        }
272 3
    }
273
}
274