Completed
Push — main ( 430b4f...e47493 )
by Emmanuel
01:46
created

IncomeExpense::getExpenseCategoryById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
use Illuminate\Support\Str;
20
21
class IncomeExpense extends AccountingData
22
{
23
24
25
    /**
26
     * @param int $categoryid
27
     * @param string $income_title
28
     * @param int $amount
29
     * @param string $notes
30
     * @param string $transaction_id
31
     * @return Income
32
     *
33
     * function to add an income and ledger at the same time
34
     */
35
    public function add_income(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Income
36
    {
37
38
        $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...
39
40
        if ($transaction_id == 0)
41
            $transaction_id = $this->set_transaction_id($income);
42
43
44
        $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...
45
46
        return $income;
47
    }
48
49
    /**
50
     * @param int $categoryid
51
     * @param string $income_title
52
     * @param int $amount
0 ignored issues
show
Bug introduced by
There is no parameter named $income_title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     * @param string $notes
54
     * @param string $transaction_id
55
     * @return Expense
56
     *
57
     * function to add expense and to a ledger at the sametime
58
     */
59
60
    public function add_expense(int $categoryid, string $expense_title, int $amount, string $notes = "", $transaction_id = "0"): Expense
61
    {
62
63
        $expense = $this->newExpense($categoryid, $expense_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...
64
65
        if ($transaction_id == 0)
66
            $transaction_id = $this->set_transaction_id($expense);
67
68
69
        $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...
70
71
        return $expense;
72
    }
73
74
75
    /**
76
     * @param $income
77
     * @return string
78
     * @throws \Exception
79
     *
80
     * function that sets a transaction id
81
     */
82
83
    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...
84
    {
85
        $prefix = config('income-expense.transaction_id_prefix');
86
        $length = config('income-expense.transaction_id_length');
87
88
        $keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
89
90
        $str = '';
91
92
        $max = mb_strlen($keyspace, '8bit') - 1;
93
94
        for ($i = 0; $i < $length; ++$i) {
95
            $str .= $keyspace[random_int(0, $max)];
96
        }
97
98
        return $prefix . $str;
99
    }
100
101
    /**
102
     * @param int $categoryid
103
     * @param string $income_title
104
     * @param int $amount
105
     * @param string $notes
106
     * @return Income
107
     *
108
     * Unit function that adds income
109
     */
110
    private function newIncome(int $categoryid, string $income_title, int $amount, string $notes = ""): Income
111
    {
112
        return Income::create([
113
            "incomecategory" => $categoryid,
114
            "income_title" => $income_title,
115
            "amount" => $amount,
116
            "notes" => $notes,
117
            "date" => date('Y-m-d')
118
        ]);
119
    }
120
121
    /**
122
     * @param $transactionObj
123
     * @param $type
124
     * @param $transaction_id
125
     * @return Ledger
126
     *
127
     * unit function that adds a ledger transaction
128
     */
129
    public function add_transaction_on_ledger($transactionObj, $type, $transaction_id): Ledger
130
    {
131
132
        $ledger = new Ledger;
133
        $ledger->transaction_id = $transaction_id;
134
        $ledger->transaction_type = $type;
135
        $ledger->transaction_type_category = $transactionObj->incomecategory;
136
        $ledger->amount = $transactionObj->amount;
137
        $ledger->save();
138
        return $ledger;
139
    }
140
141
    /**
142
     * @param int $categoryid
143
     * @param string $expense_title
144
     * @param int $amount
145
     * @param string $notes
146
     * @return Expense
147
     *
148
     * A unit function that adds new expense
149
     */
150
151
    private function newExpense(int $categoryid, string $expense_title, int  $amount, string $notes = ""): Expense
152
    {
153
        return Expense::create([
154
            "expense_category" => $categoryid,
155
            "expense_title" => $expense_title,
156
            "amount" => $amount,
157
            "notes" => $notes,
158
            "date" => date('Y-m-d')
159
        ]);
160
    }
161
162
163
    /**
164
     * @param $categoryname
165
     * @param $description
166
     * @return ExpenseCategory
167
     *
168
     * A unit function that adds expense category
169
     */
170 View Code Duplication
    public function addExpenseCategory($categoryname, $description): ExpenseCategory
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...
171
    {
172
        return  ExpenseCategory::create([
173
            "category" => $categoryname,
174
            "description" => $description,
175
            "slug"=>Str::slug($categoryname),
176
            "date" => date('Y-m-d')
177
        ]);
178
    }
179
180
    /**
181
     * @param $categoryname
182
     * @param $description
183
     * @return IncomeCategory
184
     *
185
     * A unit function that adds income category
186
     */
187
188
189 View Code Duplication
    public function addIncomeCategory($categoryname, $description): IncomeCategory
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...
190
    {
191
192
        return  IncomeCategory::create([
193
            "category" => $categoryname,
194
            "description" => $description,
195
            "slug"=>Str::slug('($categoryname', '-'),
196
            "date" => date('Y-m-d')
197
        ]);
198
    }
199
200
201
    public function getExpenseCategoryById($expensecategoryid)
202
    {
203
        return ExpenseCategory::find($expensecategoryid);
204
    }
205
206
207
208
    public function getIncomeCategoryById($incomecategoryid)
209
    {
210
        return IncomeCategory::find($incomecategoryid);
211
    }
212
213
    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...
214
    {
215
    }
216
217
218
    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...
219
    {
220
    }
221
222
223
    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...
224
    {
225
    }
226
227
228
    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...
229
    {
230
    }
231
}
232