Conditions | 11 |
Paths | 13 |
Total Lines | 48 |
Code Lines | 32 |
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 |
||
82 | public function checkStatus(): Result |
||
83 | { |
||
84 | $result = new Result($this->label); |
||
85 | |||
86 | if (!file_exists($this->filename)) { |
||
87 | $result->setSuccess(false); |
||
88 | $result->setError($this->filename." does not exist!"); |
||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | if (null !== $this->maxage) { |
||
93 | $mtime = filemtime($this->filename); |
||
94 | if ($mtime === false) { |
||
95 | $result->setError("mtime() returns error"); |
||
96 | return $result; |
||
97 | } |
||
98 | $age = (time() - $mtime); |
||
99 | $age = round($age / 60); // sec-to-min |
||
100 | if ($age > (int) $this->maxage) { |
||
101 | $result->setError($this->filename." is to old!"); |
||
102 | return $result; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if (null !== $this->writable && !is_writable($this->filename)) { |
||
107 | $result->setError($this->filename." is not writable!"); |
||
108 | return $result; |
||
109 | } |
||
110 | |||
111 | if (null !== $this->unwantedRegex) { |
||
112 | $fp = fopen($this->filename, 'r'); |
||
113 | if ($fp === false) { |
||
114 | $result->setError("fopen() returns error"); |
||
115 | return $result; |
||
116 | } |
||
117 | $linenr = 0; |
||
118 | while ($line = fgets($fp)) { |
||
119 | $linenr++; |
||
120 | if (preg_match('~'.$this->unwantedRegex.'~i', $line)) { |
||
121 | $result->setError("Found '".$this->unwantedRegex."' in '".$line."' [".$this->filename.":".$linenr."]"); |
||
122 | fclose($fp); |
||
123 | return $result; |
||
124 | } |
||
125 | } |
||
126 | fclose($fp); |
||
127 | } |
||
128 | |||
129 | return $result; |
||
130 | } |
||
132 |