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 |
||
| 21 | class RenderGzipFileStream implements FileStream |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var SitemapRender |
||
| 25 | */ |
||
| 26 | private $render; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var StreamState |
||
| 30 | */ |
||
| 31 | private $state; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var resource|null |
||
| 35 | */ |
||
| 36 | private $handle; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $filename = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $tmp_filename = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $compression_level = 9; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | private $counter = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $end_string = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | private $used_bytes = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param SitemapRender $render |
||
| 70 | * @param string $filename |
||
| 71 | * @param int $compression_level |
||
| 72 | */ |
||
| 73 | 15 | public function __construct(SitemapRender $render, $filename, $compression_level = 9) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | 1 | public function getFilename() |
|
| 92 | |||
| 93 | 8 | View Code Duplication | public function open() |
| 107 | |||
| 108 | 15 | View Code Duplication | public function close() |
| 125 | |||
| 126 | /** |
||
| 127 | * @param Url $url |
||
| 128 | */ |
||
| 129 | 5 | View Code Duplication | public function push(Url $url) |
| 130 | { |
||
| 131 | 5 | if (!$this->state->isReady()) { |
|
| 132 | 2 | throw StreamStateException::notReady(); |
|
| 133 | } |
||
| 134 | |||
| 135 | 3 | if ($this->counter >= self::LINKS_LIMIT) { |
|
| 136 | 1 | throw LinksOverflowException::withLimit(self::LINKS_LIMIT); |
|
| 137 | } |
||
| 138 | |||
| 139 | 3 | $render_url = $this->render->url($url); |
|
| 140 | |||
| 141 | 3 | $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string); |
|
| 142 | 3 | if ($expected_bytes > self::BYTE_LIMIT) { |
|
| 143 | throw SizeOverflowException::withLimit(self::BYTE_LIMIT); |
||
| 144 | } |
||
| 145 | |||
| 146 | 3 | $this->write($render_url); |
|
| 147 | 3 | ++$this->counter; |
|
| 148 | 3 | } |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @return int |
||
| 152 | */ |
||
| 153 | 2 | public function count() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $string |
||
| 160 | */ |
||
| 161 | 8 | private function write($string) |
|
| 166 | } |
||
| 167 |