Conditions | 10 |
Paths | 66 |
Total Lines | 53 |
Code Lines | 27 |
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 |
||
79 | protected function toSVG(){ |
||
80 | $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; |
||
81 | $class = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true)); |
||
82 | |||
83 | // svg header |
||
84 | $svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'" height="'.$length.'" viewBox="0 0 '.$length.' '.$length.'" style="background-color:'.$this->options->bgColor.'">'.$this->options->eol. |
||
85 | '<defs><style>.'.$class.'{fill:'.$this->options->fgColor.'} rect{shape-rendering:crispEdges}</style></defs>'.$this->options->eol; |
||
86 | |||
87 | // svg body |
||
88 | foreach($this->matrix as $r => $row){ |
||
89 | //we'll combine active blocks within a single row as a lightweight compression technique |
||
90 | $from = -1; |
||
91 | $count = 0; |
||
92 | |||
93 | foreach($row as $c => $pixel){ |
||
94 | if($pixel){ |
||
95 | $count++; |
||
96 | |||
97 | if($from < 0){ |
||
98 | $from = $c; |
||
99 | } |
||
100 | } |
||
101 | elseif($from >= 0){ |
||
102 | $svg .= '<rect x="'.($from * $this->options->pixelSize + $this->options->marginSize).'" y="'.($r * $this->options->pixelSize + $this->options->marginSize) |
||
103 | .'" width="'.($this->options->pixelSize * $count).'" height="'.$this->options->pixelSize.'" class="'.$class.'" />'.$this->options->eol; |
||
104 | |||
105 | // reset count |
||
106 | $from = -1; |
||
107 | $count = 0; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | // close off the row, if applicable |
||
112 | if($from >= 0){ |
||
113 | $svg .= '<rect x="'.($from * $this->options->pixelSize + $this->options->marginSize).'" y="'.($r * $this->options->pixelSize + $this->options->marginSize) |
||
114 | .'" width="'.($this->options->pixelSize * $count).'" height="'.$this->options->pixelSize.'" class="'.$class.'" />'.$this->options->eol; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // close svg |
||
119 | $svg .= '</svg>'.$this->options->eol; |
||
120 | |||
121 | // if saving to file, append the correct headers |
||
122 | if($this->options->cachefile){ |
||
123 | $svg = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg; |
||
124 | |||
125 | if(@file_put_contents($this->options->cachefile, $svg) === false){ |
||
126 | throw new QRCodeOutputException('Could not write to cache file.'); // @codeCoverageIgnore |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return $svg; |
||
131 | } |
||
132 | |||
134 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.