Completed
Push — master ( 5c4932...5604b2 )
by Mauro
02:39
created

TaskValidation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 3.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 2
dl 3
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateInputOnCreateTask() 0 13 3
B validateInputOnUpdateTask() 3 16 5
A validateNameOnUpdateTask() 0 9 2
A validateStatusOnUpdateTask() 0 9 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\Exception\TaskException;
6
7
/**
8
 * Task Validation.
9
 */
10
abstract class TaskValidation extends BaseValidation
11
{
12
    /**
13
     * Validate and sanitize input data when create new task.
14
     *
15
     * @param array|object|null $input
16
     * @return array
17
     * @throws \Exception
18
     */
19
    public static function validateInputOnCreateTask($input)
20
    {
21
        if (empty($input['name'])) {
22
            throw new TaskException(TaskException::TASK_NAME_REQUIRED, 400);
23
        }
24
        $task = self::validateTaskName($input['name']);
25
        $status = 0;
26
        if (isset($input['status'])) {
27
            $status = self::validateStatus($input['status']);
28
        }
29
30
        return ['name' => $task, 'status' => $status];
31
    }
32
33
    /**
34
     * Validate and sanitize input data when update a task.
35
     *
36
     * @param array|object|null $input
37
     * @param object $task
38
     * @return array
39
     * @throws \Exception
40
     */
41
    public static function validateInputOnUpdateTask($input, $task)
42
    {
43 View Code Duplication
        if (!isset($input['name']) && !isset($input['status'])) {
1 ignored issue
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...
44
            throw new TaskException(TaskException::TASK_INFO_REQUIRED, 400);
45
        }
46
        $name = $task->name;
47
        if (isset($input['name'])) {
48
            $name = self::validateTaskName($input['name']);
49
        }
50
        $status = $task->status;
51
        if (isset($input['status'])) {
52
            $status = self::validateStatus($input['status']);
53
        }
54
55
        return ['name' => $name, 'status' => $status];
56
    }
57
58
    /**
59
     * @param array $input
60
     * @param object $task
61
     * @return string
62
     */
63
    public static function validateNameOnUpdateTask($input, $task)
64
    {
65
        $name = $task->name;
66
        if (isset($input['name'])) {
67
            $name = self::validateTaskName($input['name']);
68
        }
69
70
        return $name;
71
    }
72
73
    /**
74
     * @param array $input
75
     * @param object $task
76
     * @return int
77
     */
78
    public static function validateStatusOnUpdateTask($input, $task)
79
    {
80
        $status = $task->status;
81
        if (isset($input['status'])) {
82
            $status = self::validateStatus($input['status']);
83
        }
84
85
        return $status;
86
    }
87
}
88