| Conditions | 17 |
| Paths | 16 |
| Total Lines | 52 |
| Code Lines | 38 |
| 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 |
||
| 98 | public static function getLines($filename,$reverse=false,$maxLines=null,$lineCallback=null){ |
||
| 99 | if(file_exists($filename)){ |
||
| 100 | $result=[]; |
||
| 101 | if($reverse && isset($maxLines)){ |
||
| 102 | $fl = fopen($filename, "r"); |
||
| 103 | for($x_pos = 0, $ln = 0,$lines=[]; fseek($fl, $x_pos, SEEK_END) !== -1; $x_pos--) { |
||
|
1 ignored issue
–
show
|
|||
| 104 | $char = fgetc($fl); |
||
|
1 ignored issue
–
show
|
|||
| 105 | if ($char === "\n") { |
||
| 106 | if(is_callable($lineCallback)){ |
||
| 107 | $lineCallback($result,$lines[$ln]); |
||
| 108 | }else{ |
||
| 109 | $result[]=$lines[$ln]; |
||
| 110 | } |
||
| 111 | if(isset($maxLines) && sizeof($result)>=$maxLines){ |
||
| 112 | fclose($fl); |
||
|
1 ignored issue
–
show
|
|||
| 113 | return $result; |
||
| 114 | } |
||
| 115 | $ln++; |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | $lines[$ln] = $char . ((array_key_exists($ln, $lines)) ? $lines[$ln] : ''); |
||
| 119 | } |
||
| 120 | fclose($fl); |
||
| 121 | return $result; |
||
| 122 | }else{ |
||
| 123 | $handle = fopen($filename, "r"); |
||
| 124 | if ($handle) { |
||
|
1 ignored issue
–
show
|
|||
| 125 | while (($line = fgets($handle)) !== false) { |
||
| 126 | if(is_callable($lineCallback)){ |
||
| 127 | $lineCallback($result,$line); |
||
| 128 | }else{ |
||
| 129 | $result[]=$line; |
||
| 130 | } |
||
| 131 | if(isset($maxLines) && sizeof($result)>=$maxLines){ |
||
| 132 | fclose($handle); |
||
| 133 | if(is_array($result)){ |
||
| 134 | $result=array_reverse($result); |
||
| 135 | } |
||
| 136 | return $result; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | fclose($handle); |
||
| 140 | } else { |
||
| 141 | // error opening the file. |
||
| 142 | } |
||
| 143 | if($reverse){ |
||
| 144 | $result=array_reverse($result); |
||
| 145 | } |
||
| 146 | return $result; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | return []; |
||
| 150 | } |
||
| 152 |