| Conditions | 10 |
| Paths | 62 |
| Total Lines | 51 |
| Code Lines | 30 |
| 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 |
||
| 66 | protected static function getTempParentFolder($base) |
||
| 67 | { |
||
| 68 | // first, try finding a silverstripe-cache dir built off the base path |
||
| 69 | $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
||
| 70 | if (@file_exists($tempPath)) { |
||
| 71 | if ((fileperms($tempPath) & 0777) != 0777) { |
||
| 72 | @chmod($tempPath, 0777); |
||
|
|
|||
| 73 | } |
||
| 74 | return $tempPath; |
||
| 75 | } |
||
| 76 | |||
| 77 | // failing the above, try finding a namespaced silverstripe-cache dir in the system temp |
||
| 78 | $tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . |
||
| 79 | 'silverstripe-cache-php' . preg_replace('/[^\w-\.+]+/', '-', PHP_VERSION) . |
||
| 80 | str_replace(array(' ', '/', ':', '\\'), '-', $base); |
||
| 81 | if (!@file_exists($tempPath)) { |
||
| 82 | $oldUMask = umask(0); |
||
| 83 | @mkdir($tempPath, 0777); |
||
| 84 | umask($oldUMask); |
||
| 85 | |||
| 86 | // if the folder already exists, correct perms |
||
| 87 | } else { |
||
| 88 | if ((fileperms($tempPath) & 0777) != 0777) { |
||
| 89 | @chmod($tempPath, 0777); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | $worked = @file_exists($tempPath) && @is_writable($tempPath); |
||
| 94 | |||
| 95 | // failing to use the system path, attempt to create a local silverstripe-cache dir |
||
| 96 | if (!$worked) { |
||
| 97 | $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
||
| 98 | if (!@file_exists($tempPath)) { |
||
| 99 | $oldUMask = umask(0); |
||
| 100 | @mkdir($tempPath, 0777); |
||
| 101 | umask($oldUMask); |
||
| 102 | } |
||
| 103 | |||
| 104 | $worked = @file_exists($tempPath) && @is_writable($tempPath); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!$worked) { |
||
| 108 | throw new Exception( |
||
| 109 | 'Permission problem gaining access to a temp folder. ' . |
||
| 110 | 'Please create a folder named silverstripe-cache in the base folder ' . |
||
| 111 | 'of the installation and ensure it has the correct permissions' |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $tempPath; |
||
| 116 | } |
||
| 117 | } |
||
| 118 |
If you suppress an error, we recommend checking for the error condition explicitly: