Completed
Push — master ( dd0ca4...f00659 )
by Julien
03:18
created

AdministratorsRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 1
dl 13
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B rules() 0 25 2
A messages() 13 13 1
A authorize() 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
namespace App\Http\Requests;
4
5
use App\Http\Models\Administrators;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Illuminate\Support\Facades\Auth;
8
9
use Illuminate\Support\Facades\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Http\Requests\Request.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
/**
12
 * Class AdministratorsRequest
13
 * Classe qui modélisera le formulaire d'administrateurs
14
 * @package App\Http\Requests
15
 */
16
class AdministratorsRequest extends FormRequest
17
{
18
19
20
    /**
21
     * Retourne un tableau de validation par champ
22
     * @return array
23
     */
24
    public function rules()
25
    {
26
        // get id since router
27
        $id = $this->route('id');
28
29
        if($id == null) {
30
            return [
31
                'firstname' => 'required|max:255',
32
                'lastname' => 'required|max:255',
33
                'description' => 'required|min:10',
34
                'email' => 'required|email|max:255|unique:administrators',
35
                'password' => 'required|confirmed|min:6',
36
                'image' => 'required|image',
37
            ];
38
        }else{
39
            return [
40
                'firstname' => 'required|max:255',
41
                'lastname' => 'required|max:255',
42
                'description' => 'required|min:10',
43
                'email' => 'required|email|max:255|unique:administrators,email,' . $id,
44
                'password' => 'confirmed|min:6',
45
                'image' => 'image',
46
            ];
47
        }
48
    }
49
50
    /**
51
     * Customisation des messages par champs
52
     * @return array
53
     */
54 View Code Duplication
    public function messages()
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...
55
    {
56
        return [
57
            'title.required' => 'Un titre pour le film!',
58
            'required' => ':attribute  est obligatoire',
59
            'min' => 'Ce champ doit faire plus de :min caractères',
60
            'max' => 'Ce champ doit faire moins de :max caractères',
61
            'integer' => 'Ce champ doit être un chiffre',
62
            'regex' => ':attribute a un mauvais format',
63
            'date_format' => 'Le format de date doit etre valide',
64
            'image' => "Le format de l'image est invalide",
65
        ];
66
    }
67
68
69
    /**
70
     * Autoriser l'accès de mon formulaire
71
     * pour tout utilisateur
72
     * @return bool
73
     */
74
    public function authorize()
75
    {
76
        return true;
77
    }
78
79
80
}
81