Completed
Push — feature/pilot_information ( 13ff1e...cc067b )
by Laurent
01:46
created

PilotEditController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 26.76 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 19
loc 71
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 19 45 6
A handle() 0 4 1
A getHandler() 0 3 1
A getPilotRepository() 0 4 1

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
4
namespace FlightLog\Http\Web\Controller;
5
6
7
use FlightLog\Application\Damage\Command\CreateDamageCommand;
8
use FlightLog\Application\Damage\Command\CreateDamageCommandHandler;
9
use FlightLog\Application\Pilot\Command\CreateUpdatePilotInformationCommand;
10
use FlightLog\Application\Pilot\Command\CreateUpdatePilotInformationCommandHandler;
11
use FlightLog\Domain\Pilot\ValueObject\PilotId;
12
use FlightLog\Http\Web\Form\DamageCreationForm;
13
use FlightLog\Http\Web\Form\PilotForm;
14
use FlightLog\Infrastructure\Pilot\Repository\PilotRepository;
15
16
final class PilotEditController extends WebController
17
{
18
19
    public function index(){
20
        $id = $this->request->getParam('id');
21
22
        if($id === null){
23
            $this->renderHtml('<p>Paramètre ID non fournis.</p>');
24
            return;
25
        }
26
27
        $command = new CreateUpdatePilotInformationCommand($id);
28
29
        if($this->getPilotRepository()->exist(PilotId::create($id))){
30
            $command->fromPilot($this->getPilotRepository()->getById(PilotId::create($id)));
31
        }
32
33
        $form = new PilotForm('pilot', $this->db);
34
        $form->bind($command);
0 ignored issues
show
Documentation introduced by
$command is of type object<FlightLog\Applica...ilotInformationCommand>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36
        $user = new \User($this->db);
37
        $user->fetch($id);
38
39 View Code Duplication
        if($this->request->isPost()){
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...
40
            $form->setData($this->request->getPostParameters());
41
42
            if(!$form->validate()){
43
                return $this->render('pilot/edit.phtml', [
44
                    'form' => $form,
45
                ]);
46
            }
47
48
            try{
49
                $this->handle($form->getObject());
0 ignored issues
show
Bug introduced by
It seems like $form->getObject() targeting FlightLog\Http\Web\Form\PilotForm::getObject() can also be of type null or object<stdClass>; however, FlightLog\Http\Web\Contr...ditController::handle() does only seem to accept object<FlightLog\Applica...ilotInformationCommand>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            }catch(\Exception $e){
51
                print $e->getMessage();
52
                dol_syslog($e->getMessage(), LOG_ERR);
53
            }
54
55
56
            return $this->redirect($_SERVER["PHP_SELF"].'?id='.$id.'&r=edit_pilot');
57
        }
58
59
        return $this->render('pilot/edit.phtml', [
60
            'pilotForm' => $form,
61
            'pilot' => $user,
62
        ]);
63
    }
64
65
    /**
66
     * @param CreateUpdatePilotInformationCommand $command
67
     *
68
     * @throws \Exception
69
     */
70
    private function handle(CreateUpdatePilotInformationCommand $command)
71
    {
72
        $this->getHandler()->__invoke($command);
73
    }
74
75
    /**
76
     * @return CreateUpdatePilotInformationCommandHandler()
0 ignored issues
show
Documentation introduced by
The doc-type CreateUpdatePilotInformationCommandHandler() could not be parsed: Expected "|" or "end of type", but got "(" at position 42. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
77
     */
78
    private function getHandler(){
79
        return new CreateUpdatePilotInformationCommandHandler($this->getPilotRepository());
80
    }
81
82
    private function getPilotRepository()
83
    {
84
        return new PilotRepository($this->db);
85
    }
86
}