Completed
Push — main ( a7617a...20a3bd )
by Emmanuel
01:13
created

IncomeExpense::set_transaction_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
/**
5
 * Author: Emmanuel Paul Mnzava
6
 * Twitter: @epmnzava
7
 * Github:https://github.com/dbrax/income-expense
8
 * Email: [email protected]
9
 * 
10
 */
11
12
namespace Epmnzava\IncomeExpense;
13
14
use Epmnzava\IncomeExpense\Models\Expense;
15
use Epmnzava\IncomeExpense\Models\ExpenseCategory;
16
use Epmnzava\IncomeExpense\Models\Income;
17
use Epmnzava\IncomeExpense\Models\IncomeCategory;
18
use Epmnzava\IncomeExpense\Models\Ledger;
19
20
class IncomeExpense extends AccountingData
21
{
22
23
24 View Code Duplication
    public function add_income(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Income
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...
25
    {
26
27
        $income = $this->newIncome($categoryid, $income_title, $amount, $notes, $transaction_id);
0 ignored issues
show
Unused Code introduced by
The call to IncomeExpense::newIncome() has too many arguments starting with $transaction_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
29
        if ($transaction_id == 0)
30
            $transaction_id = $this->set_transaction_id($income);
31
32
33
        $ledger = $this->add_transaction_on_ledger($income, "INC", $transaction_id);
0 ignored issues
show
Unused Code introduced by
$ledger 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...
34
35
        return $income;
36
    }
37
38
39 View Code Duplication
    public function add_expense(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Expense
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...
40
    {
41
42
        $expense = $this->newExpense($categoryid, $income_title, $amount, $notes, $transaction_id);
0 ignored issues
show
Unused Code introduced by
The call to IncomeExpense::newExpense() has too many arguments starting with $transaction_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
44
        if ($transaction_id == 0)
45
            $transaction_id = $this->set_transaction_id($expense);
46
47
48
        $ledger = $this->add_transaction_on_ledger($expense, "EXP", $transaction_id);
0 ignored issues
show
Unused Code introduced by
$ledger 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...
49
50
        return $expense;
51
    }
52
53
    public function set_transaction_id($income)
0 ignored issues
show
Unused Code introduced by
The parameter $income is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        $prefix = config('income-expense.transaction_id_prefix');
56
        $length = config('income-expense.transaction_id_length');
57
58
        $keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
59
60
        $str = '';
61
62
        $max = mb_strlen($keyspace, '8bit') - 1;
63
64
        for ($i = 0; $i < $length; ++$i) {
65
            $str .= $keyspace[random_int(0, $max)];
66
        }
67
68
        return $prefix . $str;
69
    }
70
    public function newIncome(int $categoryid, string $income_title, int $amount, string $notes = ""): Income
71
    {
72
        return Income::create([
73
            "incomecategory" => $categoryid,
74
            "income_title" => $income_title,
75
            "amount" => $amount,
76
            "notes" => $notes,
77
            "date" => date('Y-m-d')
78
        ]);
79
    }
80
81
    public function add_transaction_on_ledger($transactionObj, $type, $transaction_id): Ledger
82
    {
83
84
        $ledger = new Ledger;
85
        $ledger->transaction_id = $transaction_id;
86
        $ledger->transaction_type = $type;
87
        $ledger->transaction_type_category = $transactionObj->incomecategory;
88
        $ledger->amount = $transactionObj->amount;
89
        $ledger->save();
90
        return $ledger;
91
    }
92
93
    public function newExpense(int $categoryid, string $expense_title, int  $amount, string $notes = ""): Expense
94
    {
95
        return Expense::create([
96
            "expense_category" => $categoryid,
97
            "expense_title" => $expense_title,
98
            "amount" => $amount,
99
            "notes" => $notes,
100
            "date" => date('Y-m-d')
101
        ]);
102
    }
103
104
105
    public function addExpenseCategory($categoryname, $description): ExpenseCategory
106
    {
107
        return  ExpenseCategory::create([
108
            "category" => $categoryname,
109
            "description" => $description,
110
            "date" => date('Y-m-d')
111
        ]);
112
    }
113
114
115
    public function addIncomeCategory($categoryname, $description): IncomeCategory
116
    {
117
118
        return  IncomeCategory::create([
119
            "category" => $categoryname,
120
            "description" => $description,
121
            "date" => date('Y-m-d')
122
        ]);
123
    }
124
125
    public function deleteExpenseCategory($expensecategoryid)
0 ignored issues
show
Unused Code introduced by
The parameter $expensecategoryid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127
    }
128
129
130
    public function deleteIncomeCategory($incomecategoryid)
0 ignored issues
show
Unused Code introduced by
The parameter $incomecategoryid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
    }
133
134
135
    public function updateExpenseCategory($expensecategoryid)
0 ignored issues
show
Unused Code introduced by
The parameter $expensecategoryid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
    }
138
139
140
    public function updateIncomeCategory($incomecategoryid)
0 ignored issues
show
Unused Code introduced by
The parameter $incomecategoryid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
    }
143
}
144