Completed
Push — master ( cceadc...3df73a )
by Mikołaj
04:41
created

AddController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 21.95 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 9
loc 41
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A add() 9 35 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Rudolf\Modules\Users\One\Admin\Profile;
4
5
use Rudolf\Component\Alerts\Alert;
6
use Rudolf\Component\Alerts\AlertsCollection;
7
use Rudolf\Framework\Controller\AdminController;
8
9
class AddController extends AdminController
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function add()
0 ignored issues
show
Coding Style introduced by
add uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        if (isset($_POST['add'])) {
17
            if (empty($_POST['password']) || trim($_POST['password']) !== $_POST['password_again']) {
18
                AlertsCollection::add(
19
                    new Alert(
20
                        'error',
21
                        'Podane hasła nie są identyczne!'
22
                    )
23
                );
24
                $this->redirect(DIR.'/admin/users/add');
25
            }
26
27
            $id = (new AddModel())->add($_POST);
28
29 View Code Duplication
            if ($id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
                AlertsCollection::add(
31
                    new Alert(
32
                        'success',
33
                        'Dodano użytkownika'
34
                    )
35
                );
36
                $this->redirect(DIR.'/admin/users/edit/'.$id);
37
            }
38
            AlertsCollection::add(
39
                new Alert(
40
                    'error',
41
                    'Wystąpił nieoczekiwany błąd'
42
                )
43
            );
44
        }
45
        $view = new AddView();
46
        $view->display();
47
        $view->render();
48
    }
49
}
50