WorkflowTrait::storeHistory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 8
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
    namespace Bantenprov\VueWorkflow\Http\Traits;
4
    use Bantenprov\VueWorkflow\Models\WorkflowType;
5
    use Bantenprov\VueWorkflow\Models\History;
6
    use Bantenprov\VueWorkflow\Models\TransitionState;
7
    use Bantenprov\VueWorkflow\Models\Transition;
8
9
10
    /**
11
     * Workflow trait
12
     */
13
    trait WorkflowTrait
14
    {
15
16
        public function insertWithWorkflow($class, $datas, $method_type = 'create', $entity_id = null)
17
        {
18
            $workflow = WorkflowType::where('content_type', class_basename($class));
19
20
            if($workflow->count() > 0){
21
22
                if($method_type == 'update' && !is_null($entity_id)){
23
                    $get = $class->$method_type($datas);
0 ignored issues
show
Unused Code introduced by
$get is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
24
                    $content_id = $entity_id;
25
                }else{
26
                    $get = $class->$method_type($datas);
27
                    $content_id = $get->id;
28
                }
29
30
                $transition = Transition::where('name','propose-to-propose')->where('workflow_id',$workflow->first()->workflow_id)->first();
31
32
                $this->storeHistory(
33
                    class_basename($class),
34
                    $content_id,
35
                    $workflow->first()->workflow_id,
36
                    $transition->id,
37
                    $transition->from,
38
                    $transition->to,
39
                    \Auth::user()->id
40
                );
41
            }else{
42
                $class->create($datas);
43
            }
44
45
        }
46
47
        public function storeHistory($content_type, $content_id, $workflow_id, $workflow_transition_id, $from_state, $to_state, $user_id, $message = '')
48
        {
49
            $save['content_type']           = $content_type;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
50
            $save['content_id']             = $content_id;
51
            $save['workflow_id']            = $workflow_id;
52
            $save['workflow_transition_id'] = $workflow_transition_id;
53
            $save['from_state']             = $from_state;
54
            $save['to_state']               = $to_state;
55
            $save['user_id']                = $user_id;
56
            $save['message']                = $message;
57
58
            $history = History::create($save);
59
60
            $this->storeTransitionState($history->to_state ,$history->content_id, $history->id);
61
        }
62
63
        public function storeTransitionState($current_state, $content_id, $history_id)
64
        {
65
            $save['current_state'] = $current_state;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
66
            $save['content_id'] = $content_id;
67
            $save['history_id'] = $history_id;
68
69
            TransitionState::create($save);
70
        }
71
72
        public function updateWithWorkflow($class, $content_id, $request)
73
        {
74
            $check = History::where('content_id', $content_id);
75
76
            $response = [];
77
78
            if($check->count() > 1){
79
                $response['message'] = "Cant update, current state must be propose !";
80
            }else{
81
                $class->update($request);
82
                $response['message'] = "Update success";
83
            }
84
85
            return $response;
86
        }
87
88
89
    }
90