Completed
Push — master ( 2b2a9c...f0c93a )
by Andrii
02:32
created

FeatureContext::secondCharge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behat\Gherkin\Node\PyStringNode;
5
use Behat\Gherkin\Node\TableNode;
6
use hiqdev\php\billing\action\Action;
7
use hiqdev\php\billing\charge\Charge;
8
use hiqdev\php\billing\customer\Customer;
9
use hiqdev\php\billing\price\SinglePrice;
10
use hiqdev\php\billing\target\Target;
11
use hiqdev\php\billing\type\Type;
12
use hiqdev\php\units\Quantity;
13
use Money\Currency;
14
use Money\Money;
15
16
/**
17
 * Defines application features from the specific context.
18
 */
19
class FeatureContext implements Context
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
{
21
    /**
22
     * Initializes context.
23
     */
24
    public function __construct()
25
    {
26
        $this->customer = new Customer(null, 'somebody');
0 ignored issues
show
Bug introduced by
The property customer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    /**
30
     * @Given /(\S+) (\S+) price is ([0-9.]+) (\w+) per ([0-9.]+) (\w+)/
31
     */
32
    public function priceIs($target, $type, $sum, $currency, $amount, $unit)
33
    {
34
        $type = new Type(Type::ANY, $type);
35
        $target = new Target(Target::ANY, $target);
36
        $quantity = Quantity::create($unit, $amount);
37
        $sum = new Money($sum*100, new Currency($currency));
38
        $this->price = new SinglePrice(null, $type, $target, null, $quantity, $sum);
0 ignored issues
show
Bug introduced by
The property price does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
    }
40
41
    /**
42
     * @Given /action is (\S+) (\w+) ([0-9.]+) (\S+)/
43
     */
44
    public function actionIs($target, $type, $amount, $unit)
45
    {
46
        $type = new Type(Type::ANY, $type);
47
        $target = new Target(Target::ANY, $target);
48
        $quantity = Quantity::create($unit, $amount);
49
        $time = new DateTimeImmutable();
50
        $this->action = new Action(null, $type, $target, $quantity, $this->customer, $time);
0 ignored issues
show
Bug introduced by
The property action does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
    }
52
53
    /**
54
     * @Given /formula is (.+)/
55
     */
56
    public function formulaIs($formula)
57
    {
58
        $this->formula = $formula;
0 ignored issues
show
Bug introduced by
The property formula does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
    }
60
61
    /**
62
     * @When /date is ([0-9.-]+)/
63
     */
64
    public function dateIs($date)
65
    {
66
        $this->date = $date;
0 ignored issues
show
Bug introduced by
The property date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
    }
68
69
    /**
70
     * @Then /(\w+) charge is (\S+)? ?([0-9.]+)? ?([A-Z]{3})?/
71
     */
72
    public function chargeIs($numeral, $type = null, $sum = null, $currency = null)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $sum 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...
Unused Code introduced by
The parameter $currency 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...
73
    {
74
        $no = $this->ensureNo($numeral);
0 ignored issues
show
Unused Code introduced by
$no 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...
75
        //var_dump($this->formula);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
        //var_dump($this->date);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        //var_dump("$no $type $sum $currency");
78
    }
79
80
    protected $numerals = [
81
        'first'     => 1,
82
        'second'    => 2,
83
        'third'     => 3,
84
        'fourth'    => 4,
85
        'fifth'     => 5,
86
    ];
87
88
    public function ensureNo($numeral)
89
    {
90
        if (empty($this->numerals[$numeral])) {
91
            throw new Exception("wrong numeral '$numeral'");
92
        }
93
94
        return $this->numerals[$numeral] - 1;
95
    }
96
}
97