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  | 
            ||
| 10 | class ConsoleOutputStream extends OutputStream  | 
            ||
| 11 | { | 
            ||
| 12 | /**  | 
            ||
| 13 | * constructor  | 
            ||
| 14 | */  | 
            ||
| 15 | public function __construct()  | 
            ||
| 16 |     { | 
            ||
| 17 |         parent::__construct(""); | 
            ||
| 18 | }  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 |      * {@inheritdoc} | 
            ||
| 22 | */  | 
            ||
| 23 | public function write($buf, int $off = null, int $len = null)  | 
            ||
| 24 |     { | 
            ||
| 25 | $data = null;  | 
            ||
| 26 |         if ($off === null && $len === null) { | 
            ||
| 27 | $data = $buf;  | 
            ||
| 28 |         } elseif ($off !== null && $len === null) { | 
            ||
| 29 | $data = substr($buf, $off);  | 
            ||
| 30 |         } elseif ($off === null && $len !== null) { | 
            ||
| 31 | $data = substr($buf, 0, $len);  | 
            ||
| 32 |         } else { | 
            ||
| 33 | $data = substr($buf, $off, $len);  | 
            ||
| 34 | }  | 
            ||
| 35 | |||
| 36 | $this->stream .= $data;  | 
            ||
| 37 | }  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 |      * {@inheritdoc} | 
            ||
| 41 | */  | 
            ||
| 42 | public function close()  | 
            ||
| 43 |     { | 
            ||
| 44 | $this->stream = null;  | 
            ||
| 45 | }  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 |      * {@inheritdoc} | 
            ||
| 49 | */  | 
            ||
| 50 | public function flush()  | 
            ||
| 51 |     { | 
            ||
| 52 | echo $this->stream;  | 
            ||
| 53 | }  | 
            ||
| 54 | }  | 
            ||
| 55 |