Completed
Push — master ( 1d790a...bb9b7d )
by Seth
02:11
created

AbstractCanvasSearchDomain::setId()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace smtech\StMarksSearch\Canvas;
4
5
use Exception;
6
use smtech\CanvasPest\CanvasPest;
7
use smtech\StMarksSearch\AbstractSearchDomain;
8
9
/**
10
 * Parent class for all Canvas search domains
11
 *
12
 * @author Seth Battis <[email protected]>
13
 */
14
abstract class AbstractCanvasSearchDomain extends AbstractSearchDomain
15
{
16
    use RequireCanvasPestParameter;
17
18
    /**
19
     * Canvas ID or SIS ID of the Canvas object
20
     * @var string|integer
21
     */
22
    protected $id;
23
24
    /**
25
     * Construct a CanvasSearchDomain from `$params`, requires `id` and `api`
26
     * params, will extract `url` param from `api`, if necessary.
27
     *
28
     * @inheritdoc
29
     *
30
     * @param mixed[string] $params
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
     */
32
    public function __construct($params)
33
    {
34
        $this->requireCanvasPestParameter($params);
35
36 View Code Duplication
        if (empty($params['url'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
            $params['url'] = preg_replace('%^(.*)/api/v\d+$%', '$1', $this->getApi()->base_url);
38
        }
39
40
        parent::__construct($params);
41
42
        assert(isset($params['id']), new Exception('`id` parameter required'));
43
        $this->setId($params['id']);
44
    }
45
46
    /**
47
     * Update the ID of the Canvas object
48
     *
49
     * @used-by AbstractCanvasSearchDomain::__construct()
50
     * @param string|integer $id Canvas ID or SIS ID formatted as `sis_*_id:*`
51
     */
52
    protected function setId($id)
53
    {
54
        assert(
55
            is_numeric($id) ||
56
                preg_match('/^sis_[a-z]+_id:\S+$/i', $id),
57
            new Exception('ID must be a Canvas ID or SIS ID, received:' . PHP_EOL . print_r($id, true))
58
        );
59
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type double. However, the property $id is declared as type string|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...
60
    }
61
62
    /**
63
     * Get the Canvas object ID
64
     *
65
     * @return string|integer
66
     */
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
}
72