Conditions | 11 |
Paths | 4 |
Total Lines | 52 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
122 | public static function scanDirectory( |
||
123 | $dir, |
||
124 | $mask, |
||
125 | $nomask = array('.', '..', 'CVS'), |
||
126 | $callback = null, |
||
127 | $recurse = true, |
||
128 | $key = 'filename', |
||
129 | $min_depth = 0, |
||
130 | $depth = 0 |
||
131 | ) { |
||
132 | $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename'); |
||
133 | $files = array(); |
||
134 | |||
135 | if (is_dir($dir) && $handle = opendir($dir)) { |
||
136 | while (false !== ($file = readdir($handle))) { |
||
137 | if (!in_array($file, $nomask)) { |
||
138 | if (is_dir($dir . DIRECTORY_SEPARATOR . $file) && $recurse) { |
||
139 | // Give priority to files in this folder by merging them in after any subdirectory files. |
||
140 | $files = array_merge( |
||
141 | self::scanDirectory( |
||
142 | $dir . DIRECTORY_SEPARATOR . $file, |
||
143 | $mask, |
||
144 | $nomask, |
||
145 | $callback, |
||
146 | $recurse, |
||
147 | $key, |
||
148 | $min_depth, |
||
149 | $depth + 1 |
||
150 | ), |
||
151 | $files |
||
152 | ); |
||
153 | } elseif ($depth >= $min_depth && preg_match($mask, $file)) { |
||
154 | // Always use this match over anything already set in $files with the same $$key. |
||
155 | $filename = $dir . DIRECTORY_SEPARATOR . $file; |
||
156 | $basename = basename($file); |
||
157 | $name = substr($basename, 0, strrpos($basename, '.')); |
||
158 | $files[$$key] = new \stdClass(); |
||
159 | $files[$$key]->filename = $filename; |
||
160 | $files[$$key]->basename = $basename; |
||
161 | $files[$$key]->name = $name; |
||
162 | if (is_callable($callback)) { |
||
163 | $callback($filename); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | closedir($handle); |
||
170 | } |
||
171 | |||
172 | return $files; |
||
173 | } |
||
174 | |||
279 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.