Completed
Push — master ( ac011d...56c397 )
by Mikołaj
03:19
created

QuestionAddController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 24.56 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 14
loc 57
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addAction() 0 21 1
A postAddAction() 14 14 2

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 Egzaminer\Controller;
4
5
use Egzaminer\Model\QuestionAddModel;
6
7
class QuestionAddController extends AdminController
8
{
9
    /**
10
     * Add question.
11
     *
12
     * GET /admin/exam/edit/[i:tid]/question/add
13
     *
14
     * @param int $examID Exam id
15
     *
16
     * @return void
17
     */
18
    public function addAction($examID)
19
    {
20
        $question = [
21
            'content' => '',
22
            'correct' => '',
23
            'image'   => '',
24
        ];
25
        $answers = [
26
            ['content' => '', 'id' => '1'],
27
            ['content' => '', 'id' => '2'],
28
            ['content' => '', 'id' => '3'],
29
            ['content' => '', 'id' => '4'],
30
        ];
31
32
        $this->render('admin/question', [
33
            'title'    => 'Dodawanie pytania',
34
            'examID'   => $examID,
35
            'question' => $question,
36
            'answers'  => $answers,
37
        ]);
38
    }
39
40
    /**
41
     * Add question post action.
42
     *
43
     * POST /admin/exam/edit/[i:tid]/question/add
44
     *
45
     * @param int $examID Exam id
46
     *
47
     * @return void
48
     */
49 View Code Duplication
    public function postAddAction($examID)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        $model = new QuestionAddModel($this->get('dbh'));
52
53
        if ($id = $model->add($examID, $this->getFromRequest('post'))) {
54
            $this->setMessage('success', 'Dodano pomyślnie!');
55
            $this->redirect('/admin/exam/edit/'.$examID.'/question/edit/'.$id);
56
57
            return;
58
        }
59
60
        $this->setMessage('warning', 'Coś się zepsuło!');
61
        $this->redirect('/admin/exam/edit/'.$examID.'/question/add');
62
    }
63
}
64