Test Failed
Push — master ( 46a9f3...87423a )
by Alexey
05:20
created

FilesController::clearMissingAction()   D

Complexity

Conditions 15
Paths 364

Size

Total Lines 58
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 42
c 0
b 0
f 0
nc 364
nop 0
dl 0
loc 58
rs 4.7492

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Files admin controller
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class FilesController extends adminController {
12
13
    public function managerForEditorAction() {
14
        $this->view->page(['page' => 'blank']);
15
    }
16
17
    public function clearMissingAction() {
18
        if (!empty($_POST['files'])) {
19
            var_dump($_POST['files']);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($_POST['files']); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
20
            $files = \Files\File::getList(['where' => ['id', $_POST['files'], 'IN']]);
21
            foreach ($files as $file) {
22
                $file->delete();
23
            }
24
        }
25
        $usedImages = [];
26
        $installedModules = \Module::getInstalled(App::$primary);
27
        foreach ($installedModules as $module) {
28
            foreach (\Module::getModels($module) as $modelPath => $modelName) {
0 ignored issues
show
Bug introduced by
The method getModels() does not seem to exist on object<Module>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
                foreach ($modelName::$cols as $colName => $col) {
30
                    if ($col['type'] == 'image') {
31
                        $items = $modelName::getList(['where' => [$colName, 0, '!='], 'array' => true, 'key' => false, 'cols' => $modelName::colPrefix() . $colName]);
32
                        if ($items) {
33
                            foreach ($items as $item) {
34
                                $usedImages[$item[$modelName::colPrefix() . $colName]] = true;
35
                            }
36
37
                        }
38
                    }
39
                }
40
            }
41
        }
42
        echo '<form method="post"><table>';
43
        $missingImages = \Files\File::getList(['where' => ['id', array_keys($usedImages), 'NOT IN'], 'key' => 'path', 'array' => true]);
44
        $texts = '';
45
        foreach (\Materials\Material::getList(['array' => true]) as $material) {
46
            $texts .= $material['material_preview'];
47
            $texts .= $material['material_text'];
48
        }
49
        if (!empty($installedModules['TextBlocks'])) {
50
            foreach (\TextBlocks\Block::getList(['array' => true]) as $block) {
51
                var_dump($block);
52
                exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method clearMissingAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
53
                $texts .= $material['material_preview'];
0 ignored issues
show
Unused Code introduced by
$texts .= $material['material_preview']; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
54
                $texts .= $material['material_text'];
55
            }
56
        }
57
        $deleted = 0;
58
        foreach ($missingImages as $path => $file) {
59
            if (strpos($texts, $path)) {
60
                unset($missingImages[$path]);
61
                $deleted++;
62
            }
63
        }
64
        foreach ($missingImages as $path => $file) {
65
            echo '<tr>';
66
            echo "<td><input type='checkbox' name='files[]' value='{$file['file_id']}' checked /></td>";
67
            echo "<td>{$file['file_id']}</td>";
68
            echo "<td>{$file['file_code']}</td>";
69
            echo "<td>{$file['file_upload_code']}</td>";
70
            echo "<td>{$file['file_name']}</td>";
71
            echo '</tr>';
72
        }
73
        echo '</table><button>Удалить</button></form>';
74
    }
75
76
}
77