Conditions | 10 |
Paths | 40 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 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 |
||
51 | function getTempParentFolder($base = null) { |
||
52 | if(!$base && defined('BASE_PATH')) $base = BASE_PATH; |
||
53 | |||
54 | $worked = true; |
||
55 | |||
56 | // first, try finding a silverstripe-cache dir built off the base path |
||
57 | $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
||
58 | if(@file_exists($tempPath)) { |
||
59 | if((fileperms($tempPath) & 0777) != 0777) { |
||
60 | @chmod($tempPath, 0777); |
||
|
|||
61 | } |
||
62 | return $tempPath; |
||
63 | } |
||
64 | |||
65 | // failing the above, try finding a namespaced silverstripe-cache dir in the system temp |
||
66 | $tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . |
||
67 | 'silverstripe-cache-php' . preg_replace('/[^\w-\.+]+/', '-', PHP_VERSION) . |
||
68 | str_replace(array(' ', '/', ':', '\\'), '-', $base); |
||
69 | if(!@file_exists($tempPath)) { |
||
70 | $oldUMask = umask(0); |
||
71 | $worked = @mkdir($tempPath, 0777); |
||
72 | umask($oldUMask); |
||
73 | |||
74 | // if the folder already exists, correct perms |
||
75 | } else { |
||
76 | if((fileperms($tempPath) & 0777) != 0777) { |
||
77 | @chmod($tempPath, 0777); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // failing to use the system path, attempt to create a local silverstripe-cache dir |
||
82 | if(!$worked) { |
||
83 | $worked = true; |
||
84 | $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
||
85 | if(!@file_exists($tempPath)) { |
||
86 | $oldUMask = umask(0); |
||
87 | $worked = @mkdir($tempPath, 0777); |
||
88 | umask($oldUMask); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if(!$worked) { |
||
93 | throw new Exception( |
||
94 | 'Permission problem gaining access to a temp folder. ' . |
||
95 | 'Please create a folder named silverstripe-cache in the base folder ' . |
||
96 | 'of the installation and ensure it has the correct permissions' |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | return $tempPath; |
||
101 | } |
||
102 |
If you suppress an error, we recommend checking for the error condition explicitly: