Completed
Push — master ( 3b79d8...b88358 )
by Mauro
02:24
created

BaseValidation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 23.88 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 16
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateName() 8 8 2
A validateEmail() 0 9 2
A validateTaskName() 8 8 2
A validateStatus() 0 8 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 App\Validation;
4
5
use App\Message\TaskMessage;
6
use App\Message\UserMessage;
7
use Respect\Validation\Validator as v;
8
9
/**
10
 * Base Validation.
11
 */
12
abstract class BaseValidation
13
{
14
    /**
15
     * Validate and sanitize a username.
16
     *
17
     * @param string $name
18
     * @return string
19
     * @throws \Exception
20
     */
21 View Code Duplication
    protected static function validateName($name)
22
    {
23
        if (!v::alnum()->length(2, 100)->validate($name)) {
24
            throw new \Exception(UserMessage::USER_NAME_INVALID, 400);
25
        }
26
27
        return $name;
28
    }
29
30
    /**
31
     * Validate and sanitize a email address.
32
     *
33
     * @param string $emailValue
34
     * @return string
35
     * @throws \Exception
36
     */
37
    protected static function validateEmail($emailValue)
38
    {
39
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
40
        if (!v::email()->validate($email)) {
41
            throw new \Exception(UserMessage::USER_EMAIL_INVALID, 400);
42
        }
43
44
        return $email;
45
    }
46
47
    /**
48
     * Validate and sanitize a task name.
49
     *
50
     * @param string $name
51
     * @return string
52
     * @throws \Exception
53
     */
54 View Code Duplication
    protected static function validateTaskName($name)
55
    {
56
        if (!v::alnum()->length(2, 100)->validate($name)) {
57
            throw new \Exception(TaskMessage::TASK_NAME_INVALID, 400);
58
        }
59
60
        return $name;
61
    }
62
63
    /**
64
     * Validate and sanitize a task status.
65
     *
66
     * @param int $status
67
     * @return string
68
     * @throws \Exception
69
     */
70
    protected static function validateStatus($status)
71
    {
72
        if (!v::numeric()->between(0, 1)->validate($status)) {
73
            throw new \Exception(TaskMessage::TASK_STATUS_INVALID, 400);
74
        }
75
76
        return $status;
77
    }
78
}
79