Completed
Branch Recipes (cae097)
by Laurent
14:13 queued 11:11
created

AbstractInstallController::nameToVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * AbstractInstallController controller d'installation de l'application GLSR.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version GIT: <git_id>
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller\Install;
16
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\HttpFoundation\Request;
19
use Doctrine\Common\Persistence\ObjectManager;
20
21
/**
22
 * AbstractInstall controller
23
 *
24
 * @category Controller
25
 */
26
abstract class AbstractInstallController extends Controller
27
{
28
    /**
29
     * Etape X de l'installation.
30
     * Fonction adaptable aux différentes étapes de l'installation.
31
     *
32
     * @param \Symfony\Component\HttpFoundation\Request $request    Form request
33
     * @param string                                    $entity     Entity name
34
     * @param string                                    $entityPath Entity Namespace
35
     * @param string                                    $typePath   Type of Namespace
36
     * @param int|string                                $number     Step number
37
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|
38
     *     array<string,string|null|Settings|\Symfony\Component\Form\FormView> Rendered view
39
     */
40
    public function stepAction(Request $request, $entity, $entityPath, $typePath, $number)
41
    {
42
        $etm = $this->getDoctrine()->getManager();
43
        $ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll());
44
        $entityNew = $etm->getClassMetadata($entityPath)->newInstance();
45
        $message = null;
46
        
47
        if ($ctEntity > 0 && $request->getMethod() == 'GET' && is_int($number)) {
48
            $message = 'gestock.install.st'.$number.'.yet_exist';
49
        }
50
        $form = $this->createForm($typePath, $entityNew, ['action' => $this->generateUrl('gs_install_st'.$number),]);
51
        if (is_int($number)) {
52
            $return = ['message' => $message, 'form' => $form->createView(),];
53
        } else {
54
            $return = [$this->nameToVariable($entity) => $entityNew, 'form' => $form->createView(),];
55
        }
56
57
        if ($form->handleRequest($request)->isValid()) {
58
            $return = $this->validInstall($entityNew, $form, $etm, $number);
59
60
            $this->addFlash('info', 'gestock.install.st'.$number.'.flash');
61
        }
62
        
63
        return $return;
64
    }
65
66
    /**
67
     * Valid install step.
68
     *
69
     * @param object                                     $entityNew Entity
70
     * @param \Symfony\Component\Form\Form               $form      Form of Entity
71
     * @param \Doctrine\Common\Persistence\ObjectManager $etm       Entity Manager
72
     * @param integer                                    $number    Number of step install
73
     * @return array Route after valid or not
74
     */
75
    private function validInstall($entityNew, $form, ObjectManager $etm, $number)
76
    {
77
        $etm->persist($entityNew);
78
        $etm->flush();
79
80
        if (null !== $form->get('save') || null !== $form->get('addmore')) {
81
            if ($form->get('save')->isClicked()) {
82
                $return = $this->redirect($this->generateUrl('gs_install_st4'));
83
            } elseif ($form->get('addmore')->isClicked()) {
84
                $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
85
            }
86
        } else {
87
            $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
88
        }
89
90
        return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
91
    }
92
93
    private function nameToVariable($name)
94
    {
95
        
96
        $array = explode('\\', $name);
97
        $return = strtolower(end($array));
98
    
99
        return $return;
100
    }
101
}
102