Conditions | 18 |
Paths | 33 |
Total Lines | 83 |
Lines | 13 |
Ratio | 15.66 % |
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 |
||
72 | function xoops_module_update_randomquote(\XoopsModule $module, $previousVersion = null) |
||
73 | { |
||
74 | $moduleDirName = basename(dirname(__DIR__)); |
||
75 | $moduleDirNameUpper = strtoupper($moduleDirName); |
||
76 | |||
77 | /** @var randomquote\Helper $helper */ |
||
78 | /** @var randomquote\Utility $utility */ |
||
79 | /** @var randomquote\common\Configurator $configurator */ |
||
80 | $helper = randomquote\Helper::getInstance(); |
||
81 | $utility = new randomquote\Utility(); |
||
82 | $configurator = new randomquote\common\Configurator(); |
||
83 | $helper->loadLanguage('common'); |
||
84 | |||
85 | if ($previousVersion < 240) { |
||
86 | |||
87 | //delete old HTML templates |
||
88 | if (count($configurator->templateFolders) > 0) { |
||
89 | foreach ($configurator->templateFolders as $folder) { |
||
90 | $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder); |
||
91 | if (is_dir($templateFolder)) { |
||
92 | $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']); |
||
93 | foreach ($templateList as $k => $v) { |
||
94 | $fileInfo = new SplFileInfo($templateFolder . $v); |
||
95 | if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) { |
||
96 | if (file_exists($templateFolder . $v)) { |
||
97 | unlink($templateFolder . $v); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // --- DELETE OLD FILES --------------- |
||
106 | if (count($configurator->oldFiles) > 0) { |
||
107 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
108 | foreach (array_keys($configurator->oldFiles) as $i) { |
||
109 | $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]); |
||
110 | if (is_file($tempFile)) { |
||
111 | unlink($tempFile); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | // --- DELETE OLD FOLDERS --------------- |
||
117 | xoops_load('XoopsFile'); |
||
118 | if (count($configurator->oldFolders) > 0) { |
||
119 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
120 | foreach (array_keys($configurator->oldFolders) as $i) { |
||
121 | $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]); |
||
122 | /** @var XoopsObjectHandler $folderHandler */ |
||
123 | $folderHandler = \XoopsFile::getHandler('folder', $tempFolder); |
||
124 | $folderHandler->delete($tempFolder); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // --- CREATE FOLDERS --------------- |
||
129 | View Code Duplication | if (count($configurator->uploadFolders) > 0) { |
|
130 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
131 | foreach (array_keys($configurator->uploadFolders) as $i) { |
||
132 | $utility::createFolder($configurator->uploadFolders[$i]); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // --- COPY blank.png FILES --------------- |
||
137 | View Code Duplication | if (count($configurator->copyBlankFiles) > 0) { |
|
138 | $file = __DIR__ . '/../assets/images/blank.png'; |
||
139 | foreach (array_keys($configurator->copyBlankFiles) as $i) { |
||
140 | $dest = $configurator->copyBlankFiles[$i] . '/blank.png'; |
||
141 | $utility::copyFile($file, $dest); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | //delete .html entries from the tpl table |
||
146 | $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'"; |
||
147 | $GLOBALS['xoopsDB']->queryF($sql); |
||
148 | |||
149 | /** @var XoopsGroupPermHandler $gpermHandler */ |
||
150 | $gpermHandler = xoops_getHandler('groupperm'); |
||
151 | return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read'); |
||
152 | } |
||
153 | return true; |
||
154 | } |
||
155 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: