Passed
Push — master ( 2955c9...ee5866 )
by Fran
05:26
created

GeneratorController::doGenerateModule()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 7
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 21
cp 0
crap 12
1
<?php
2
namespace PSFS\controller;
3
4
use PSFS\base\config\ModuleForm;
5
use PSFS\base\exception\ConfigException;
6
use PSFS\base\Logger;
7
use PSFS\base\Security;
8
use PSFS\controller\base\Admin;
9
10
/**
11
 * Class GeneratorController
12
 * @package PSFS\controller
13
 * @domain ROOT
14
 */
15
class GeneratorController extends Admin
16
{
17
    /**
18
     * @Inyectable
19
     * @var  \PSFS\services\GeneratorService Servicio de generación de estructura de directorios
20
     */
21
    protected $gen;
22
23
    /**
24
     * Método que genera un nuevo módulo
25
     * @GET
26
     * @route /admin/module
27
     *
28
     * @return string HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
29
     * @throws \HttpException
30
     */
31
    public function generateModule()
32
    {
33
        Logger::log("Arranque generador de módulos al solicitar " . $this->getRequest()->getRequestUri());
34
        /* @var $form \PSFS\base\config\ConfigForm */
35
        $form = new ModuleForm();
36
        $form->build();
37
        return $this->render("modules.html.twig", array(
38
            'properties' => $this->config->getPropelParams(),
39
            'form' => $form,
40
        ));
41
    }
42
43
    /**
44
     * @POST
45
     * @route /admin/module
46
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48
    public function doGenerateModule()
49
    {
50
        $form = new ModuleForm();
51
        $form->build();
52
        $form->hydrate();
53
        if ($form->isValid()) {
54
            $module = $form->getFieldValue("module");
55
            $force = $form->getFieldValue("force");
56
            $type = $form->getFieldValue("controllerType");
57
            try {
58
                $module = preg_replace('/(\\\|\/)/', '/', $module);
59
                $module = preg_replace('/^\//', '', $module);
60
                $this->gen->createStructureModule($module, $force, $type);
0 ignored issues
show
Bug introduced by
It seems like $module defined by preg_replace('/^\\//', '', $module) on line 59 can also be of type array<integer,string>; however, PSFS\Services\GeneratorS...createStructureModule() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
                Security::getInstance()->setFlash("callback_message", str_replace("%s", $module, _("Módulo %s generado correctamente")));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
62
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin-module", true));
63
            } catch (\Exception $e) {
64
                Logger::getInstance()->infoLog($e->getMessage() . " [" . $e->getFile() . ":" . $e->getLine() . "]");
65
                throw new ConfigException('Error al generar el módulo, prueba a cambiar los permisos', 403);
0 ignored issues
show
Unused Code introduced by
The call to ConfigException::__construct() has too many arguments starting with 403.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66
            }
67
        }
68
        return $this->render("modules.html.twig", array(
69
            'properties' => $this->config->getPropelParams(),
70
            'form' => $form,
71
        ));
72
    }
73
}