Passed
Push — master ( 1955b9...c5754d )
by Darío
06:42
created

Module::init()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 78
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 78
rs 8.9688
c 0
b 0
f 0
cc 5
nc 13
nop 1

How to fix   Long Method   

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
namespace Catcher;
4
5
use Drone\Dom\Element\Form;
6
Use Drone\Mvc\AbstractionModule;
7
use Drone\Mvc\AbstractionController;
8
use Drone\Mvc\Layout;
9
use Drone\Validator\FormValidator;
10
use Drone\Util\ArrayDimension;
11
12
class Module extends AbstractionModule
13
{
14
	public function init(AbstractionController $c)
15
	{
16
        $config = $this->getUserConfig();
17
18
        $_config = ArrayDimension::toUnidimensional($config, "_");
19
20
        $this->setTranslator($c);
21
22
        # config constraints
23
24
        $components = [
25
            "attributes" => [
26
                "project_name" => [
27
                    "required"  => true,
28
                    "type"      => "text",
29
                    "minlength" => 2,
30
                    "maxlength" => 60
31
                ],
32
            ],
33
        ];
34
35
        $options = [
36
            "project" => [
37
                "label"      => "project -> name"
38
            ],
39
        ];
40
41
        $form = new Form($components);
42
        $form->fill($_config);
43
44
        $validator = new FormValidator($form, $options);
45
        $validator->validate();
46
47
        $data["validator"] = $validator;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
48
49
        try
50
        {
51
            if (!$validator->isValid())
52
            {
53
                $data["messages"] = $validator->getMessages();
54
                throw new \Exception("Module config errros in user.config!", 300);
55
            }
56
        }
57
        catch (\Exception $e)
58
        {
59
            $file = str_replace('\\', '', __CLASS__);
60
            $storage = new \Drone\Exception\Storage("cache/$file.json");
61
62
            # stores the error code
63
            if (($errorCode = $storage->store($e)) === false)
0 ignored issues
show
introduced by
The condition $errorCode = $storage->store($e) === false is always true.
Loading history...
64
            {
65
                $errors = $storage->getErrors();
66
67
                # if error storing is not possible, handle it (internal app error)
68
                //$this->handleErrors($errors, __METHOD__);
69
            }
70
71
            $data["code"]    = $errorCode;
72
            $data["message"] = $e->getMessage();
73
74
            $config = include 'config/application.config.php';
75
            $data["dev_mode"] = $config["environment"]["dev_mode"];
76
77
            # stops current controller execution
78
            $c->stopExecution(false);
0 ignored issues
show
Unused Code introduced by
The call to Drone\Mvc\AbstractionController::stopExecution() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            $c->/** @scrutinizer ignore-call */ 
79
                stopExecution(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
79
80
            # loads error view
81
            $layoutManager = new Layout();
82
            $layoutManager->setBasePath($c->getBasePath());
83
84
            $layoutManager->setView($this, "validation");
85
            $layoutManager->setParams($data);
86
87
            # for AJAX requests!
88
            if ($c->isXmlHttpRequest())
89
                $layoutManager->content();
90
            else
91
                $layoutManager->fromTemplate($this, 'blank');
92
        }
93
	}
94
95
    private function setTranslator(AbstractionController $c)
96
    {
97
        $config = include('config/application.config.php');
98
        $locale = $config["environment"]["locale"];
99
100
        $i18nTranslator = \Zend\I18n\Translator\Translator::factory(
101
            [
102
                'locale'  => "$locale",
103
                'translation_files' => [
104
                    [
105
                        "type" => 'phparray',
106
                        "filename" => __DIR__ . "/lang/$locale.php"
107
                    ]
108
                ]
109
            ]
110
        );
111
112
        $c->translator = new \Zend\Mvc\I18n\Translator($i18nTranslator);
0 ignored issues
show
Bug introduced by
The property translator does not seem to exist on Drone\Mvc\AbstractionController.
Loading history...
113
    }
114
115
	public function getUserConfig()
116
	{
117
		return include __DIR__ . "/config/user.config.php";
118
	}
119
}