Completed
Push — master ( 7c71a4...34aff4 )
by Matt
05:48
created

Restore::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 12
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.2
cc 4
eloc 6
nc 3
nop 1
crap 4.0466
1
<?php
2
namespace Billow\Actions;
3
use InvalidArgumentException;
4
5
/**
6
 * @author Matt Frost<[email protected]>
7
 * @package Billow
8
 * @subpackage Actions
9
 * @license http://opensource.org/licenses/MIT MIT
10
 */
11 View Code Duplication
class Restore extends Action
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * Action parameter
15
     *
16
     * @const ACTION
17
     */
18
    const ACTION = 'enable_backups';
19
20
    /**
21
     * Action HTTP Method
22
     *
23
     * @const method
24
     */
25
    const METHOD = 'POST';
26
27
    /**
28
     * Image ID
29
     *
30
     * @var int $image
31
     */
32
    protected $image;
33
34
    /**
35
     * Constructor for Restore Action
36
     *
37
     * @param Array values 
38
     */
39 4
    public function __construct(Array $values)
40
    {
41 4
        if (!isset($values['image'])) {
42
            throw new InvalidArgumentException('Require value "image" is not present');
43
        }
44
45 4
        if (!is_numeric($values['image']) && !is_string($values['image'])) {
46 1
            throw new InvalidArgumentException('Image parameter must be an ID or a string');
47
        }
48
49 3
        $this->image = $values['image'];
0 ignored issues
show
Documentation Bug introduced by
It seems like $values['image'] can also be of type double or string. However, the property $image is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50 3
    }
51
52
    /**
53
     * Return the body of the request
54
     *
55
     * @return string json representation of the body
56
     */
57 2
    public function getBody()
58
    {
59 2
        return json_encode([
60 2
            'type' => self::ACTION,
61 2
            'image' => $this->image
62 2
        ]);
63
    }
64
}
65