Completed
Push — master ( d2f081...a05bf8 )
by Andrii
03:19
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
use PHPUnit\Framework\Assert;
5
6
class BillingContext extends BaseContext
7
{
8
    /**
9
     * @Given reseller :reseller
10
     */
11
    public function reseller($reseller)
12
    {
13
        $this->builder->buildReseller($reseller);
14
    }
15
16
    /**
17
     * @Given customer :customer
18
     */
19
    public function customer($customer)
20
    {
21
        $this->builder->buildCustomer($customer);
22
    }
23
24
    /**
25
     * @Given /^(grouping )?(\S+) tariff plan (\S+)/
26
     */
27
    public function plan($grouping, $type, $plan)
28
    {
29
        $this->builder->buildPlan($plan, $type, !empty($grouping));
30
    }
31
32
    protected function fullPrice(array $data)
33
    {
34
        $this->builder->buildPrice($data);
35
    }
36
37
    /**
38
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (\S+)/
39
     */
40
    public function priceWithObject($type, $price, $currency, $unit, $target)
41
    {
42
        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...
43
    }
44
45
    /**
46
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)/
47
     */
48
    public function priceWithOver($type, $price, $currency, $unit, $prepaid)
49
    {
50
        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...
51
    }
52
53
    /**
54
     * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/
55
     */
56
    public function price($type, $price, $currency, $unit)
57
    {
58
        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...
59
    }
60
61
    /**
62
     * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/
63
     */
64
    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

64
    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

64
    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...
65
    {
66
        $sums = [1 => $price, 2 => $price2];
67
        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...
68
    }
69
70
    /**
71
     * @Given /^remove and recreate tariff plan (\S+)/
72
     */
73
    public function recreatePlan($plan)
74
    {
75
        $this->builder->recreatePlan($plan);
76
    }
77
78
    /**
79
     * @Given /sale (\S+) for (\S+) plan:(\S+) time:(\S+)/
80
     */
81
    public function sale($id, $target, $plan, $time): void
82
    {
83
        $this->builder->buildSale($id, $target, $plan, $time);
84
    }
85
86
    /**
87
     * @Given /resource consumption for (\S+) is (\d+) (\S+) (\S+) for target (\S+)/
88
     */
89
    public function setConsumption($type, $amount, $unit, $time, $target): void
0 ignored issues
show
Unused Code introduced by
The parameter $type 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

89
    public function setConsumption(/** @scrutinizer ignore-unused */ $type, $amount, $unit, $time, $target): void

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 $unit 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

89
    public function setConsumption($type, $amount, /** @scrutinizer ignore-unused */ $unit, $time, $target): void

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 $amount 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

89
    public function setConsumption($type, /** @scrutinizer ignore-unused */ $amount, $unit, $time, $target): void

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 $time 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

89
    public function setConsumption($type, $amount, $unit, /** @scrutinizer ignore-unused */ $time, $target): void

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 $target 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

89
    public function setConsumption($type, $amount, $unit, $time, /** @scrutinizer ignore-unused */ $target): void

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...
90
    {
91
    }
92
93
    /**
94
     * @Given /perform billing for time (\S+) for sales/
95
     */
96
    public function performBilling($time)
0 ignored issues
show
Unused Code introduced by
The parameter $time 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

96
    public function performBilling(/** @scrutinizer ignore-unused */ $time)

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...
97
    {
98
        foreach ($this->sales as $sale) {
0 ignored issues
show
Bug Best Practice introduced by
The property sales does not exist on hiqdev\php\billing\tests...ootstrap\BillingContext. Did you maybe forget to declare it?
Loading history...
99
            // TODO: perform billing for sales
100
        }
101
    }
102
103
    /**
104
     * @Given /bill +for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
105
     */
106
    public function bill($type, $sum, $currency, $quantity, $unit, $target)
107
    {
108
        $target = $this->builder->buildTarget($target);
109
        $bill = $this->builder->findBill([
110
            'type' => $type,
111
            'target' => $target,
112
            'sum' => "$sum $currency",
113
            'quantity' => "$quantity $unit",
114
        ]);
115
        Assert::assertSame($type, $bill->getType()->getName());
116
        Assert::assertSame($target->getType(), $bill->getTarget()->getType());
117
        Assert::assertSame($target->getName(), $bill->getTarget()->getName());
118
        Assert::assertEquals($sum*100, $bill->getSum()->getAmount());
119
        Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode());
120
        Assert::assertEquals($quantity, $bill->getQuantity()->getQuantity());
121
        Assert::assertSame($unit, $bill->getQuantity()->getUnit()->getName());
122
    }
123
124
    /**
125
     * @Given /bills number is (\d+) for (\S+) for target (\S+)/
126
     */
127
    public function billsNumber($number, $type, $target)
128
    {
129
        $count = count($this->builder->findBills([
130
            'type' => $type,
131
            'target' => $target,
132
        ]));
133
134
        Assert::assertEquals($number, $count);
135
    }
136
137
    /**
138
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+) for target (\S+)$/
139
     */
140
    public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target)
141
    {
142
        $target = $this->builder->buildTarget($target);
143
        $charge = $this->builder->getNextCharge();
144
        Assert::assertSame($type, $charge->getType()->getName());
145
        Assert::assertSame($target->getType(), $charge->getTarget()->getType());
146
        Assert::assertSame($target->getName(), $charge->getTarget()->getName());
147
        Assert::assertEquals($amount*100, $charge->getSum()->getAmount());
148
        Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode());
149
        Assert::assertSame($quantity, $charge->getUsage()->getQuantity());
150
        Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName());
151
    }
152
153
    /**
154
     * @Given /charge for (\S+) is +(\S+) (\S+) per (\d+) (\S+)$/
155
     */
156
    public function charge($type, $amount, $currency, $quantity, $unit)
157
    {
158
        $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null);
159
    }
160
}
161