| Conditions | 18 |
| Paths | 72 |
| Total Lines | 71 |
| Code Lines | 54 |
| 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 |
||
| 22 | function __construct($name='egm2008-1.pgm') { |
||
|
|
|||
| 23 | if ($name == '') $name = dirname(__FILE__).'/install/tmp/egm2008-1.pgm'; |
||
| 24 | $this->offset = null; |
||
| 25 | $this->scale = null; |
||
| 26 | |||
| 27 | $f = @fopen($name,"r"); |
||
| 28 | if ($f === FALSE) { |
||
| 29 | throw new Exception("Can't open ".$name); |
||
| 30 | } |
||
| 31 | $line = fgets($f,4096); |
||
| 32 | if (trim($line) != 'P5') { |
||
| 33 | throw new Exception('No PGM header'); |
||
| 34 | } |
||
| 35 | $headerlen = strlen($line); |
||
| 36 | while (true) { |
||
| 37 | $line = fgets($f,4096); |
||
| 38 | if ((strlen($line) == 0)) { |
||
| 39 | throw new Exception('EOF before end of file header'); |
||
| 40 | } |
||
| 41 | $headerlen += strlen($line); |
||
| 42 | if (strpos($line,'# Offset ') !== FALSE) { |
||
| 43 | try { |
||
| 44 | $this->offset = substr($line, 9); |
||
| 45 | } catch(ValueError $e) { |
||
| 46 | throw new Exception('Error reading offset '.$e); |
||
| 47 | } |
||
| 48 | } else if (strpos($line,'# Scale ') !== FALSE) { |
||
| 49 | try { |
||
| 50 | $this->scale = substr($line, 8); |
||
| 51 | } catch(ValueError $e) { |
||
| 52 | throw new Exception('Error reading scale '.$e); |
||
| 53 | } |
||
| 54 | } else if ((strpos($line,'#') === FALSE)) { |
||
| 55 | try { |
||
| 56 | list($this->width, $this->height) = preg_split('/\s+/',$line); |
||
| 57 | } catch(ValueError $e) { |
||
| 58 | throw new Exception('Bad PGM width&height line'. $e); |
||
| 59 | } |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | $line = fgets($f,4096); |
||
| 64 | $headerlen += strlen($line); |
||
| 65 | $levels = (int)$line; |
||
| 66 | $this->width = (int)$this->width; |
||
| 67 | $this->height = (int)$this->height; |
||
| 68 | if (($levels != 65535)) { |
||
| 69 | throw new Exception('PGM file must have 65535 gray levels ('.$levels.')'); |
||
| 70 | } |
||
| 71 | if (($this->offset == null)) { |
||
| 72 | throw new Exception('PGM file does not contain offset'); |
||
| 73 | } |
||
| 74 | if (($this->scale == null)) { |
||
| 75 | throw new Exception('PGM file does not contain scale'); |
||
| 76 | } |
||
| 77 | if (($this->width < 2) || ($this->height < 2)) { |
||
| 78 | throw new Exception('Raster size too small'); |
||
| 79 | } |
||
| 80 | |||
| 81 | $fullsize = filesize($name); |
||
| 82 | if ((($fullsize - $headerlen) != (($this->width * $this->height) * 2))) { |
||
| 83 | throw new Exception('File has the wrong length'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->headerlen = $headerlen; |
||
| 87 | $this->raw= $f; |
||
| 88 | $this->rlonres = ($this->width / 360.0); |
||
| 89 | $this->rlatres = (($this->height - 1) / 180.0); |
||
| 90 | $this->ix = null; |
||
| 91 | $this->iy = null; |
||
| 92 | } |
||
| 93 | |||
| 169 | ?> |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.