Completed
Push — admin_masterdata ( 2f999d )
by NOBU
18:14
created

Admin/Setting/System/MasterdataController.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Setting\System;
26
27
use Eccube\Application;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Form\Type\Admin\MasterdataDataType;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\Form\FormEvent;
32
use Symfony\Component\Form\FormEvents;
33
34
class MasterdataController extends AbstractController
0 ignored issues
show
Missing class doc comment
Loading history...
35
{
36
    public function index(Application $app, Request $request)
0 ignored issues
show
Missing function doc comment
Loading history...
37
    {
38
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
39
        $form = $builder->getForm();
40
41
        if ('POST' === $request->getMethod()) {
42
            $form->handleRequest($request);
43
            if ($form->isValid()) {
44
                $data = $form->getData();
45
46
                if ($data['masterdata']) {
47
                    $masterdata = $app['orm.em']->getRepository($data['masterdata'])->findBy(array(), array('rank' => 'ASC'));
48
49
                    foreach ($masterdata as $key => $value) {
50
                        $data['data'][$value['rank']]['id'] = $value['id'];
51
                        $data['data'][$value['rank']]['name'] = $value['name'];
52
                        $data['data'][$value['rank']]['rank'] = $value['rank'];
53
                    }
54
55
                    $data['data'][$value['rank']+1]['id'] = '';
0 ignored issues
show
The variable $value seems to be defined by a foreach iteration on line 49. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
56
                    $data['data'][$value['rank']+1]['name'] = '';
57
                    $data['data'][$value['rank']+1]['rank'] = '';
58
59
                    $data['masterdata_name'] = $data['masterdata'];
60
                }
61
            }
62
        } else {
63
            $data = array();
64
        }
65
66
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit', $data);
0 ignored issues
show
The variable $data 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...
67
        $form2 = $builder2->getForm();
68
69
        return $app->render('Setting/System/masterdata.twig', array(
70
            'form' => $form->createView(),
71
            'form2' => $form2->createView(),
72
        ));
73
    }
74
75
    public function edit(Application $app, Request $request)
0 ignored issues
show
Missing function doc comment
Loading history...
76
    {
77
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit');
78
        $form2 = $builder2->getForm();
79
80
        if ('POST' === $request->getMethod()) {
81
            $form2->handleRequest($request);
82
83
            if ($form2->isValid()) {
84
                $data = $form2->getData();
85
86
                $entity = new $data['masterdata_name']();
0 ignored issues
show
Use parentheses when instantiating classes
Loading history...
87
                foreach ($data['data'] as $key => $value) {
88
                    if (!is_null($value['id']) && !is_null($value['name'])) {
0 ignored issues
show
As per coding-style, please use === null instead of is_null.
Loading history...
89
                        $entity->setId($value['id']);
90
                        $entity->setName($value['name']);
91
                        $entity->setRank($key);
92
                        $app['orm.em']->merge($entity);
93
                    } else {
94
                        // remove
95
                        $rank = $app['orm.em']->getRepository($data['masterdata_name'])->findOneBy(array('rank' => $key));
96
                        if ($rank) {
97
                            $app['orm.em']->remove($rank);
98
                        }
99
                    }
100
                }
101
                $app['orm.em']->flush();
102
103
                $app->addSuccess('admin.register.complete', 'admin');
104
                return $app->redirect($app->url('admin_setting_system_masterdata'));
0 ignored issues
show
Missing blank line before return statement
Loading history...
105
            }
106
        }
107
108
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
109
        $form = $builder->getForm();
110
111
        return $app->render('Setting/System/masterdata.twig', array(
112
            'form' => $form->createView(),
113
            'form2' => $form2->createView(),
114
        ));
115
    }
116
}
117