CouponSheet::getValidUntil()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
namespace Germania\Coupons;
3
4
class CouponSheet extends CouponSheetAbstract implements CouponSheetInterface
5
{
6
7
    /**
8
     * @param  string $id
9
     * @return self Fluid Interface
10
     */
11 5
    public function setId( $id ) {
12 5
        $this->id = $id;
13 5
        return $this;
14
    }
15
16
17
    /**
18
     * @param  int $quantity
19
     * @return self Fluid Interface
20
     */
21
    public function setQuantity( $quantity ) {
22
        $this->quantity = $quantity;
23
        return $this;
24
    }
25
26
27
    /**
28
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
29
     * @return self Fluid Interface
30
     */
31 5
    public function setSlug( $slug ) {
32 5
        $this->slug = $slug;
33 5
        return $this;
34
    }
35
36
37
    /**
38
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
39
     * @return self Fluid Interface
40
     */
41 5
    public function setName( $name ) {
42 5
        $this->name = $name;
43 5
        return $this;
44
    }
45
46
47
48
49
50
    /**
51
     * @param DateTimeInterface $valid_from
52
     * @return self Fluid Interface
53
     */
54 10
    public function setValidFrom( \DateTimeInterface $valid_from ) {
55 10
        $this->valid_from = $valid_from;
0 ignored issues
show
Documentation Bug introduced by
It seems like $valid_from of type object<DateTimeInterface> is incompatible with the declared type object<Germania\Coupons\DateTime>|null of property $valid_from.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56 10
        return $this;
57
    }
58
59
60
    /**
61
     * @inheritdoc
62
     *
63
     * If the valid_from value is a datetime string (like "Y-m-d H:i:s"),
64
     * the value will be converted to a DateTime object.
65
     *
66
     * @return DateTime
67
     * @implements CouponSheetInterface
68
     */
69 10
    public function getValidFrom() {
70 10
        if ($this->valid_from and is_string($this->valid_from)):
71 5
            $this->setValidFrom( \DateTime::createFromFormat("Y-m-d H:i:s", $this->valid_from) );
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...:s', $this->valid_from) targeting DateTime::createFromFormat() can also be of type false; however, Germania\Coupons\CouponSheet::setValidFrom() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
72 1
        endif;
73 10
        return $this->valid_from;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->valid_from; of type null|Germania\Coupons\DateTime|string adds the type string to the return on line 73 which is incompatible with the return type declared by the interface Germania\Coupons\CouponS...Interface::getValidFrom of type Germania\Coupons\DateTime.
Loading history...
74
    }
75
76
77
78
    /**
79
     * @param DateTimeInterface $valid_until
80
     * @return self Fluid Interface
81
     */
82 10
    public function setValidUntil( \DateTimeInterface $valid_until ) {
83 10
        $this->valid_until = $valid_until;
0 ignored issues
show
Documentation Bug introduced by
It seems like $valid_until of type object<DateTimeInterface> is incompatible with the declared type object<Germania\Coupons\DateTime>|null of property $valid_until.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84 10
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     *
90
     * If the valid_until value is a datetime string (like "Y-m-d H:i:s"),
91
     * the value will be converted to a DateTime object.
92
     *
93
     * @return DateTime
94
     * @implements CouponSheetInterface
95
     */
96 10
    public function getValidUntil() {
97 10
        if ($this->valid_until and is_string($this->valid_until)):
98 5
            $this->setValidUntil( \DateTime::createFromFormat("Y-m-d H:i:s", $this->valid_until) );
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...s', $this->valid_until) targeting DateTime::createFromFormat() can also be of type false; however, Germania\Coupons\CouponSheet::setValidUntil() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
99 1
        endif;
100
101 10
        return $this->valid_until;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->valid_until; of type null|Germania\Coupons\DateTime|string adds the type string to the return on line 101 which is incompatible with the return type declared by the interface Germania\Coupons\CouponS...nterface::getValidUntil of type Germania\Coupons\DateTime.
Loading history...
102
    }
103
104
105
106
    /**
107
     * @return CouponInterface[]
108
     * @implements CouponSheetInterface
109
     */
110 10
    public function getCoupons() {
111 10
        if ($this->coupons and is_string( $this->coupons ))
112 6
            $this->setCoupons( $this->coupons );
0 ignored issues
show
Documentation introduced by
$this->coupons is of type string, but the function expects a array|object<Traversable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
114 10
        return $this->coupons;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->coupons; (string) is incompatible with the return type declared by the interface Germania\Coupons\CouponSheetInterface::getCoupons of type Germania\Coupons\CouponInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
115
    }
116
117
118
    /**
119
     * @param array|\Traversable $coupons
120
     * @return self Fluid Interface
121
     */
122 10
    public function setCoupons( $coupons )
123
    {
124 10
        if ($coupons instanceOf \Traversable ):
125 5
            $coupons = iterator_to_array( $coupons, "use_keys");
126
127 10
        elseif (is_string( $coupons )):
128 10
            $instances = array();
129 10
            foreach(explode(",", $coupons) as $raw_coupon)
130 10
                array_push($instances, Coupon::fromArray([
131 10
                    'code' => $raw_coupon,
132 8
                    'coupon_sheet' => $this
133 2
                ]));
134 10
            $coupons = $instances;
135
136 6
        elseif (!is_array( $coupons )):
137 5
            throw new \InvalidArgumentException("Array or Traversable expected");
138
139
        endif;
140
141 10
        $this->coupons = $coupons;
0 ignored issues
show
Documentation Bug introduced by
It seems like $coupons of type array is incompatible with the declared type string of property $coupons.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
142 10
        return $this;
143
    }
144
145
146
}
147