Completed
Pull Request — master (#3)
by Christian
02:33
created

Attachment::fromModel()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.5806
cc 4
eloc 19
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace XApi\Repository\Doctrine\Mapping;
13
14
use Xabbuh\XApi\Model\Attachment as AttachmentModel;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\LanguageMap;
17
18
/**
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
class Attachment
22
{
23
    public $identifier;
24
25
    public $statement;
26
27
    /**
28
     * @var string
29
     */
30
    public $usageType;
31
32
    /**
33
     * @var string
34
     */
35
    public $contentType;
36
37
    /**
38
     * @var int
39
     */
40
    public $length;
41
42
    /**
43
     * @var string
44
     */
45
    public $sha2;
46
47
    /**
48
     * @var array
49
     */
50
    public $display;
51
52
    /**
53
     * @var bool
54
     */
55
    public $hasDescription;
56
57
    /**
58
     * @var array|null
59
     */
60
    public $description;
61
62
    /**
63
     * @var string|null
64
     */
65
    public $fileUrl;
66
67
    public static function fromModel(AttachmentModel $model)
68
    {
69
        $attachment = new self();
70
        $attachment->usageType = $model->getUsageType()->getValue();
71
        $attachment->contentType = $model->getContentType();
72
        $attachment->length = $model->getLength();
73
        $attachment->sha2 = $model->getSha2();
74
        $attachment->display = array();
75
        $attachment->fileUrl = $model->getFileUrl();
0 ignored issues
show
Documentation Bug introduced by
It seems like $model->getFileUrl() can also be of type object<Xabbuh\XApi\Model\IRL>. However, the property $fileUrl is declared as type string|null. 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...
76
77
        $display = $model->getDisplay();
78
79
        foreach ($display->languageTags() as $languageTag) {
80
            $attachment->display[$languageTag] = $display[$languageTag];
81
        }
82
83
        if (null !== $description = $model->getDescription()) {
84
            $attachment->hasDescription = true;
85
            $attachment->description = array();
86
87
            foreach ($description->languageTags() as $languageTag) {
88
                $attachment->description[$languageTag] = $description[$languageTag];
89
            }
90
        } else {
91
            $attachment->hasDescription = false;
92
        }
93
94
        return $attachment;
95
    }
96
97
    public function getModel()
98
    {
99
        $description = null;
100
101
        if ($this->hasDescription) {
102
            $description = LanguageMap::create($this->description);
0 ignored issues
show
Bug introduced by
It seems like $this->description can also be of type null; however, Xabbuh\XApi\Model\LanguageMap::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
        }
104
105
        return new AttachmentModel(IRI::fromString($this->usageType), $this->contentType, $this->length, $this->sha2, LanguageMap::create($this->display), $description, $this->fileUrl);
0 ignored issues
show
Bug introduced by
It seems like $this->fileUrl can also be of type string; however, Xabbuh\XApi\Model\Attachment::__construct() does only seem to accept null|object<Xabbuh\XApi\Model\IRL>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106
    }
107
}
108