Passed
Push — master ( 0d717b...1476df )
by Andrii
02:51
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 PHPUnit\Framework\Assert;
7
8
class BillingContext extends BaseContext
9
{
10
    protected $bill;
11
12
    protected array $charges = [];
13
14
    /**
15
     * @Given reseller :reseller
16
     */
17
    public function reseller($reseller)
18
    {
19
        $this->builder->buildReseller($reseller);
20
    }
21
22
    /**
23
     * @Given customer :customer
24
     */
25
    public function customer($customer)
26
    {
27
        $this->builder->buildCustomer($customer);
28
    }
29
30
    /**
31
     * @Given manager :manager
32
     */
33
    public function manager($manager)
34
    {
35
        $this->builder->buildManager($manager);
36
    }
37
38
    /**
39
     * @Given /^(grouping )?(\S+) tariff plan (\S+)/
40
     */
41
    public function plan($grouping, $type, $plan)
42
    {
43
        $this->builder->buildPlan($plan, $type, !empty($grouping));
44
    }
45
46
    protected function fullPrice(array $data)
47
    {
48
        $this->builder->buildPrice($data);
49
    }
50
51
    /**
52
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (\S+)/
53
     */
54
    public function priceWithObject($type, $price, $currency, $unit, $target)
55
    {
56
        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...
57
    }
58
59
    /**
60
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)/
61
     */
62
    public function priceWithOver($type, $price, $currency, $unit, $prepaid)
63
    {
64
        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...
65
    }
66
67
    /**
68
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/
69
     */
70
    public function price($type, $price, $currency, $unit)
71
    {
72
        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...
73
    }
74
75
    /**
76
     * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/
77
     */
78
    public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, $unit2, $target)
0 ignored issues
show
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

78
    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...
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

78
    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...
79
    {
80
        $sums = [1 => $price, 2 => $price2];
81
        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...
82
    }
83
84
    /**
85
     * @Given /^remove and recreate tariff plan (\S+)/
86
     */
87
    public function recreatePlan($plan)
88
    {
89
        $this->builder->recreatePlan($plan);
90
    }
91
92
    /**
93
     * @Given /sale (\S+) for (\S+) plan:(\S+) time:(\S+)/
94
     */
95
    public function sale($id, $target, $plan, $time): void
96
    {
97
        $this->builder->buildSale($id, $target, $plan, $time);
98
    }
99
100
    /**
101
     * @Given /resource consumption for (\S+) is (\d+) (\S+) (\S+) for target (\S+)/
102
     */
103
    public function setConsumption(string $type, int $amount, string $unit, string $time, string $target): void
104
    {
105
        $this->builder->setConsumption($type, $amount, $unit, $time, $target);
106
    }
107
108
    /**
109
     * @Given /perform billing for time (\S+) for sales/
110
     */
111
    public function performBilling(string $time): void
112
    {
113
        $this->builder->performBilling($time);
114
    }
115
116
    /**
117
     * @Given /bill +for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
118
     */
119
    public function bill($type, $sum, $currency, $quantity, $unit, $target)
120
    {
121
        $bill = $this->findBill([
122
            'type' => $type,
123
            'target' => $target,
124
            'sum' => "$sum $currency",
125
            'quantity' => "$quantity $unit",
126
        ]);
127
        Assert::assertSame($type, $bill->getType()->getName());
128
        Assert::assertSame($target, $bill->getTarget()->getType() . ':' . $bill->getTarget()->getName());
129
        Assert::assertEquals($sum*100, $bill->getSum()->getAmount());
130
        Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode());
131
        Assert::assertEquals($quantity, $bill->getQuantity()->getQuantity());
132
        Assert::assertSame($unit, $bill->getQuantity()->getUnit()->getName());
133
    }
134
135
    public function findBill(array $params): BillInterface
136
    {
137
        $bills = $this->builder->findBills($params);
138
        $this->bill = reset($bills);
139
        $this->charges = $this->bill->getCharges();
140
141
        return $this->bill;
142
    }
143
144
    /**
145
     * @Given /bills number is (\d+) for (\S+) for target (\S+)/
146
     */
147
    public function billsNumber($number, $type, $target)
148
    {
149
        $count = count($this->builder->findBills([
150
            'type' => $type,
151
            'target' => $target,
152
        ]));
153
154
        Assert::assertEquals($number, $count);
155
    }
156
157
    /**
158
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
159
     */
160
    public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target)
161
    {
162
        $charge = $this->getNextCharge();
163
        Assert::assertSame($type, $charge->getType()->getName());
164
        Assert::assertSame($target, $charge->getTarget()->getType() . ':' . $charge->getTarget()->getName());
165
        Assert::assertEquals($amount*100, $charge->getSum()->getAmount());
166
        Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode());
167
        Assert::assertEquals($quantity, $charge->getUsage()->getQuantity());
168
        Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName());
169
    }
170
171
    /**
172
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+)$/
173
     */
174
    public function charge($type, $amount, $currency, $quantity, $unit)
175
    {
176
        $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null);
177
    }
178
179
    public function getNextCharge()
180
    {
181
        $charge = current($this->charges);
182
        next($this->charges);
183
184
        return $charge;
185
    }
186
}
187