Conditions | 11 |
Paths | 110 |
Total Lines | 57 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
123 | protected function toSVG(){ |
||
124 | $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; |
||
125 | $class = 'f' . hash('crc32', microtime(true)); |
||
126 | $foreground = 'rgb(' . $this->options->fgRed . ',' . $this->options->fgGreen . ',' . $this->options->fgBlue . ')'; |
||
127 | $background = (bool)$this->options->transparent |
||
128 | ? 'transparent' |
||
129 | : 'rgb(' . $this->options->bgRed . ',' . $this->options->bgGreen . ',' . $this->options->bgBlue . ')'; |
||
130 | |||
131 | ob_start(); |
||
132 | |||
133 | // svg header |
||
134 | echo '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $length . '" height="' . $length . '" viewBox="0 0 ' . $length . ' ' . $length . '" style="background-color:' . $background . '"><defs><style>.' . $class . '{fill:' . $foreground . '} rect{shape-rendering:crispEdges}</style></defs>'; |
||
135 | |||
136 | // svg body |
||
137 | foreach($this->matrix AS $r=>$row){ |
||
138 | //we'll combine active blocks within a single row as a lightweight compression technique |
||
139 | $from = -1; |
||
140 | $count = 0; |
||
141 | |||
142 | foreach($row AS $c=>$pixel){ |
||
143 | if($pixel){ |
||
144 | $count++; |
||
145 | if($from < 0) |
||
146 | $from = $c; |
||
147 | } |
||
148 | else if($from >= 0){ |
||
149 | echo '<rect x="' . ($from * $this->options->pixelSize + $this->options->marginSize) . '" y="' . ($r * $this->options->pixelSize + $this->options->marginSize) . '" width="' . ($this->options->pixelSize * $count) . '" height="' . $this->options->pixelSize . '" class="' . $class . '" />'; |
||
150 | |||
151 | // reset count |
||
152 | $from = -1; |
||
153 | $count = 0; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // close off the row, if applicable |
||
158 | if($from >= 0){ |
||
159 | echo '<rect x="' . ($from * $this->options->pixelSize + $this->options->marginSize) . '" y="' . ($r * $this->options->pixelSize + $this->options->marginSize) . '" width="' . ($this->options->pixelSize * $count) . '" height="' . $this->options->pixelSize . '" class="' . $class . '" />'; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // close svg |
||
164 | echo '</svg>'; |
||
165 | $imageData = ob_get_clean(); |
||
166 | |||
167 | // if saving to file, append the correct headers |
||
168 | if($this->options->cachefile){ |
||
169 | if(false === @file_put_contents($this->options->cachefile, '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n" . $imageData)){ |
||
170 | throw new QRCodeOutputException('Could not write to cache file.'); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if((bool)$this->options->base64){ |
||
175 | $imageData = 'data:image/svg+xml;base64,'.base64_encode($imageData); |
||
176 | } |
||
177 | |||
178 | return $imageData; |
||
179 | } |
||
180 | |||
182 |
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.