| Conditions | 10 |
| Paths | 10 |
| Total Lines | 47 |
| Code Lines | 21 |
| 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 |
||
| 30 | public function __construct($p) |
||
| 31 | { |
||
| 32 | if (!is_string($p)) { |
||
| 33 | throw new PathException('invalid path: not a string'); |
||
| 34 | } |
||
| 35 | |||
| 36 | // MUST contain at least one slash and start with it |
||
| 37 | if (0 !== strpos($p, '/')) { |
||
| 38 | throw new PathException('invalid path: does not start with /'); |
||
| 39 | } |
||
| 40 | |||
| 41 | // MUST NOT contain encoded "/" |
||
| 42 | if (false !== stripos($p, '%2f')) { |
||
| 43 | throw new PathException('invalid path: contains encoded "/"'); |
||
| 44 | } |
||
| 45 | |||
| 46 | // MUST NOT contain encoded "\0" |
||
| 47 | if (false !== strpos($p, '%00')) { |
||
| 48 | throw new PathException('invalid path: contains encoded "\0"'); |
||
| 49 | } |
||
| 50 | |||
| 51 | // MUST NOT contain ".." |
||
| 52 | if (false !== strpos($p, '..')) { |
||
| 53 | throw new PathException('invalid path: contains ..'); |
||
| 54 | } |
||
| 55 | |||
| 56 | // MUST NOT contain "%2e%2e" |
||
| 57 | if (false !== stripos($p, '%2e%2e')) { |
||
| 58 | throw new PathException('invalid path: contains encoded ".."'); |
||
| 59 | } |
||
| 60 | |||
| 61 | // MUST NOT contain "//" |
||
| 62 | if (false !== strpos($p, '//')) { |
||
| 63 | throw new PathException('invalid path: contains //'); |
||
| 64 | } |
||
| 65 | |||
| 66 | // MUST contain a user |
||
| 67 | $pathParts = explode('/', $p); |
||
| 68 | if (count($pathParts) < 3) { |
||
| 69 | throw new PathException('invalid path: no user specified'); |
||
| 70 | } |
||
| 71 | |||
| 72 | foreach ($pathParts as $pathPart) { |
||
| 73 | $this->pathParts[] = rawurldecode($pathPart); |
||
| 74 | } |
||
| 75 | $this->p = implode('/', $this->pathParts); |
||
| 76 | } |
||
| 77 | |||
| 145 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.