| Conditions | 15 |
| Paths | 6144 |
| Total Lines | 92 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 27 | public function __construct($options = []) |
||
| 28 | { |
||
| 29 | $default = [ |
||
| 30 | 'image_extensions' => ['png', 'jpg', 'jpeg', 'gif', 'ico'], |
||
| 31 | 'spaces_to_replace_tab' => ' ', |
||
| 32 | 'ignore' => ['.', '..', '.git', '.svn', '.netrc', '.ssh'], |
||
| 33 | 'add_ignore' => null, // add array with additional filenames to ignore |
||
| 34 | 'secure_dir' => '.', // Only display files below this directory |
||
| 35 | 'base_dir' => '.', // Which directory to start look in, defaults to current working directory of the actual script. |
||
| 36 | 'query_dir' => isset($_GET['dir']) ? strip_tags(trim($_GET['dir'])) : null, // Selected directory as ?dir=xxx |
||
| 37 | 'query_file' => isset($_GET['file']) ? strip_tags(trim($_GET['file'])) : null, // Selected directory as ?dir=xxx |
||
| 38 | 'query_path' => isset($_GET['path']) ? strip_tags(trim($_GET['path'])) : null, // Selected directory as ?dir=xxx |
||
| 39 | ]; |
||
| 40 | |||
| 41 | // Add more files to ignore |
||
| 42 | if (isset($options['add_ignore'])) { |
||
| 43 | $default['ignore'] = array_merge($default['ignore'], $options['add_ignore']); |
||
| 44 | } |
||
| 45 | |||
| 46 | $this->options = $options = array_merge($default, $options); |
||
| 47 | |||
| 48 | //Backwards compatible with source.php query arguments for ?dir=xxx&file=xxx |
||
| 49 | if (!isset($this->options['query_path'])) { |
||
| 50 | $this->options['query_path'] = trim($this->options['query_dir'] . '/' . $this->options['query_file'], '/'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->validImageExtensions = $options['image_extensions']; |
||
|
|
|||
| 54 | $this->spaces = $options['spaces_to_replace_tab']; |
||
| 55 | $this->ignore = $options['ignore']; |
||
| 56 | $this->secureDir = realpath($options['secure_dir']); |
||
| 57 | $this->baseDir = realpath($options['base_dir']); |
||
| 58 | $this->queryPath = $options['query_path']; |
||
| 59 | $this->suggestedPath = $this->baseDir . '/' . $this->queryPath; |
||
| 60 | $this->realPath = realpath($this->suggestedPath); |
||
| 61 | $this->pathinfo = pathinfo($this->realPath); |
||
| 62 | $this->path = null; |
||
| 63 | |||
| 64 | // Ensure that extension is always set |
||
| 65 | if (!isset($this->pathinfo['extension'])) { |
||
| 66 | $this->pathinfo['extension'] = null; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (is_dir($this->realPath)) { |
||
| 70 | $this->file = null; |
||
| 71 | $this->extension = null; |
||
| 72 | $this->dir = $this->realPath; |
||
| 73 | $this->path = trim($this->queryPath, '/'); |
||
| 74 | } else if (is_link($this->suggestedPath)) { |
||
| 75 | $this->pathinfo = pathinfo($this->suggestedPath); |
||
| 76 | $this->file = $this->pathinfo['basename']; |
||
| 77 | $this->extension = strtolower($this->pathinfo['extension']); |
||
| 78 | $this->dir = $this->pathinfo['dirname']; |
||
| 79 | $this->path = trim(dirname($this->queryPath), '/'); |
||
| 80 | } else if (is_readable($this->realPath)) { |
||
| 81 | $this->file = basename($this->realPath); |
||
| 82 | $this->extension = strtolower($this->pathinfo['extension']); |
||
| 83 | $this->dir = dirname($this->realPath); |
||
| 84 | $this->path = trim(dirname($this->queryPath), '/'); |
||
| 85 | } else { |
||
| 86 | $this->file = null; |
||
| 87 | $this->extension = null; |
||
| 88 | $this->dir = null; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($this->path == '.') { |
||
| 92 | $this->path = null; |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->breadcrumb = empty($this->path) ? [] : explode('/', $this->path); |
||
| 96 | |||
| 97 | // Check that dir lies below securedir |
||
| 98 | $this->message = null; |
||
| 99 | $msg = "<p><i>WARNING: The path you have selected is not a valid path or restricted due to security constraints.</i></p>"; |
||
| 100 | if (substr_compare($this->secureDir, $this->dir, 0, strlen($this->secureDir))) { |
||
| 101 | $this->file = null; |
||
| 102 | $this->extension = null; |
||
| 103 | $this->dir = null; |
||
| 104 | $this->message = $msg; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Check that all parts of the path is valid items |
||
| 108 | foreach ($this->breadcrumb as $val) { |
||
| 109 | if (in_array($val, $this->ignore)) { |
||
| 110 | $this->file = null; |
||
| 111 | $this->extension = null; |
||
| 112 | $this->dir = null; |
||
| 113 | $this->message = $msg; |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 308 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: