OutputStream   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 21.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 20
loc 94
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A open() 0 7 1
A close() 0 7 1
A push() 20 20 4
A count() 0 4 1
A send() 0 6 1

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
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Sitemap\Stream;
11
12
use GpsLab\Component\Sitemap\Render\SitemapRender;
13
use GpsLab\Component\Sitemap\Stream\Exception\LinksOverflowException;
14
use GpsLab\Component\Sitemap\Stream\Exception\SizeOverflowException;
15
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
16
use GpsLab\Component\Sitemap\Stream\State\StreamState;
17
use GpsLab\Component\Sitemap\Url\Url;
18
19
class OutputStream implements Stream
20
{
21
    /**
22
     * @var SitemapRender
23
     */
24
    private $render;
25
26
    /**
27
     * @var StreamState
28
     */
29
    private $state;
30
31
    /**
32
     * @var int
33
     */
34
    private $counter = 0;
35
36
    /**
37
     * @var int
38
     */
39
    private $used_bytes = 0;
40
41
    /**
42
     * @var string
43
     */
44
    private $end_string = '';
45
46
    /**
47
     * @param SitemapRender $render
48
     */
49 10
    public function __construct(SitemapRender $render)
50
    {
51 10
        $this->render = $render;
52 10
        $this->state = new StreamState();
53 10
    }
54
55 8
    public function open()
56
    {
57 8
        $this->state->open();
58 8
        $this->send($this->render->start());
59
        // render end string only once
60 8
        $this->end_string = $this->render->end();
61 8
    }
62
63 9
    public function close()
64
    {
65 9
        $this->state->close();
66 8
        $this->send($this->end_string);
67 8
        $this->counter = 0;
68 8
        $this->used_bytes = 0;
69 8
    }
70
71
    /**
72
     * @param Url $url
73
     */
74 6 View Code Duplication
    public function push(Url $url)
75
    {
76 6
        if (!$this->state->isReady()) {
77 2
            throw StreamStateException::notReady();
78
        }
79
80 4
        if ($this->counter >= self::LINKS_LIMIT) {
81 1
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
82
        }
83
84 4
        $render_url = $this->render->url($url);
85 4
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
86
87 4
        if ($expected_bytes > self::BYTE_LIMIT) {
88 1
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
89
        }
90
91 4
        $this->send($render_url);
92 4
        ++$this->counter;
93 4
    }
94
95
    /**
96
     * @return int
97
     */
98 2
    public function count()
99
    {
100 2
        return $this->counter;
101
    }
102
103
    /**
104
     * @param string $string
105
     */
106 8
    private function send($string)
107
    {
108 8
        echo $string;
109 8
        flush();
110 8
        $this->used_bytes += strlen($string);
111 8
    }
112
}
113