Completed
Push — master ( a7c5f9...ae6cde )
by Andrii
02:34
created

BillingContext::enumPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 4
rs 10

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 hiqdev\php\billing\tests\behat\bootstrap;
4
5
use hiqdev\php\billing\bill\BillInterface;
6
use hiqdev\php\billing\charge\ChargeInterface;
7
use PHPUnit\Framework\Assert;
8
9
class BillingContext extends BaseContext
10
{
11
    protected $bill;
12
13
    protected array $charges = [];
14
15
    /**
16
     * @Given reseller :reseller
17
     */
18
    public function reseller($reseller)
19
    {
20
        $this->builder->buildReseller($reseller);
21
    }
22
23
    /**
24
     * @Given customer :customer
25
     */
26
    public function customer($customer)
27
    {
28
        $this->builder->buildCustomer($customer);
29
    }
30
31
    /**
32
     * @Given manager :manager
33
     */
34
    public function manager($manager)
35
    {
36
        $this->builder->buildManager($manager);
37
    }
38
39
    /**
40
     * @Given /^(grouping )?(\S+) tariff plan (\S+)/
41
     */
42
    public function plan($grouping, $type, $plan)
43
    {
44
        $this->builder->buildPlan($plan, $type, !empty($grouping));
45
    }
46
47
    protected function fullPrice(array $data)
48
    {
49
        $this->builder->buildPrice($data);
50
    }
51
52
    /**
53
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (\S+)/
54
     */
55
    public function priceWithObject($type, $price, $currency, $unit, $target)
56
    {
57
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'target'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fullPrice(compact...cy', 'unit', 'target')) targeting hiqdev\php\billing\tests...ingContext::fullPrice() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
    }
59
60
    /**
61
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)/
62
     */
63
    public function priceWithPrepaid($type, $price, $currency, $unit, $prepaid)
64
    {
65
        $prepaid = "$prepaid $unit";
66
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fullPrice(compact...y', 'unit', 'prepaid')) targeting hiqdev\php\billing\tests...ingContext::fullPrice() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
    }
68
69
    /**
70
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/
71
     */
72
    public function price($type, $price, $currency, $unit)
73
    {
74
        return $this->fullPrice(compact('type', 'price', 'currency', 'unit'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fullPrice(compact...', 'currency', 'unit')) targeting hiqdev\php\billing\tests...ingContext::fullPrice() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
    }
76
77
    /**
78
     * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/
79
     */
80
    public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, $unit2, $target)
0 ignored issues
show
Unused Code introduced by
The parameter $currency2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    public function enumPrice($type, $price, $currency, $unit, $price2, /** @scrutinizer ignore-unused */ $currency2, $unit2, $target)

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

Loading history...
Unused Code introduced by
The parameter $unit2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, /** @scrutinizer ignore-unused */ $unit2, $target)

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

Loading history...
81
    {
82
        $sums = [1 => $price, 2 => $price2];
83
        return $this->fullPrice(compact('type', 'sums', 'currency', 'unit', 'target'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fullPrice(compact...cy', 'unit', 'target')) targeting hiqdev\php\billing\tests...ingContext::fullPrice() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
    }
85
86
    /**
87
     * @Given /^remove and recreate tariff plan (\S+)/
88
     */
89
    public function recreatePlan($plan)
90
    {
91
        $this->builder->recreatePlan($plan);
92
    }
93
94
    /**
95
     * @Given /sale (\S+) for (\S+) plan:(\S+) time:(\S+)/
96
     */
97
    public function sale($id, $target, $plan, $time): void
98
    {
99
        $this->builder->buildSale($id, $target, $plan, $time);
100
    }
101
102
103
    /**
104
     * @Given /purchase target (\S+) by plan (\S+) at (.+)$/
105
     */
106
    public function purchaseTarget(string $target, string $plan, string $time): void
107
    {
108
        $this->builder->buildPurchase($target, $plan, $time);
109
    }
110
111
    /**
112
     * @Given /resource consumption for (\S+) is (\d+) (\S+) for target (\S+) at (.+)$/
113
     */
114
    public function setConsumption(string $type, int $amount, string $unit, string $target, string $time): void
115
    {
116
        $this->builder->setConsumption($type, $amount, $unit, $target, $time);
117
    }
118
119
    /**
120
     * @Given /perform billing for time (\S+) for sales/
121
     */
122
    public function performBilling(string $time): void
123
    {
124
        $this->builder->performBilling($time);
125
    }
126
127
    /**
128
     * @Given /bill +for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
129
     */
130
    public function bill($type, $sum, $currency, $quantity, $unit, $target)
131
    {
132
        $bill = $this->findBill([
133
            'type' => $type,
134
            'target' => $target,
135
            'sum' => "$sum $currency",
136
            'quantity' => "$quantity $unit",
137
        ]);
138
        Assert::assertSame($type, $bill->getType()->getName());
139
        Assert::assertSame($target, $bill->getTarget()->getFullName());
140
        Assert::assertEquals($sum*100, $bill->getSum()->getAmount());
141
        Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode());
142
        Assert::assertEquals($quantity, $bill->getQuantity()->getQuantity());
143
        Assert::assertSame($unit, $bill->getQuantity()->getUnit()->getName());
144
    }
145
146
    public function findBill(array $params): BillInterface
147
    {
148
        $bills = $this->builder->findBills($params);
149
        $this->bill = reset($bills);
150
        $this->charges = $this->bill->getCharges();
151
152
        return $this->bill;
153
    }
154
155
    /**
156
     * @Given /bills number is (\d+) for (\S+) for target (\S+)/
157
     */
158
    public function billsNumber($number, $type, $target)
159
    {
160
        $count = count($this->builder->findBills([
161
            'type' => $type,
162
            'target' => $target,
163
        ]));
164
165
        Assert::assertEquals($number, $count);
166
    }
167
168
    /**
169
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
170
     */
171
    public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target)
172
    {
173
        $charge = $this->findCharge($type, $target);
174
        Assert::assertNotNull($charge);
175
        Assert::assertSame($type, $charge->getType()->getName());
176
        Assert::assertSame($target, $charge->getTarget()->getFullName());
177
        Assert::assertEquals($amount*100, $charge->getSum()->getAmount());
178
        Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode());
179
        Assert::assertEquals($quantity, $charge->getUsage()->getQuantity());
180
        Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName());
181
    }
182
183
    /**
184
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+)$/
185
     */
186
    public function charge($type, $amount, $currency, $quantity, $unit)
187
    {
188
        $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null);
189
    }
190
191
    public function findCharge($type, $target): ?ChargeInterface
192
    {
193
        foreach ($this->charges as $charge) {
194
            if ($charge->getType()->getName() !== $type) {
195
                continue;
196
            }
197
            if ($charge->getTarget()->getFullName() !== $target) {
198
                continue;
199
            }
200
            return $charge;
201
        }
202
203
        return null;
204
    }
205
206
    public function getNextCharge(): ChargeInterface
207
    {
208
        $charge = current($this->charges);
209
        next($this->charges);
210
211
        return $charge;
212
    }
213
}
214