AddonToArray   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B convert() 0 67 8
1
<?php
2
3
/**
4
 * Service for converting Addon to a nested array suitable for json-encoding in an API
5
 */
6
class AddonToArray
7
{
8
9
    public function convert(Addon $package)
10
    {
11
        $data = $package->toMap();
12
        unset($data['LastEdited']);
13
        unset($data['Created']);
14
        unset($data['ClassName']);
15
        unset($data['RecordClassName']);
16
17
        unset($data['VendorID']);
18
19
        // Ensure consistent typing
20
        $data['Rating'] = (int)$data['Rating'];
21
22
        if ($package->RatingDetails) {
0 ignored issues
show
Documentation introduced by
The property RatingDetails does not exist on object<Addon>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
            $data['RatingDetails'] = [];
24
            foreach (json_decode($package->RatingDetails, true) as $k => $v) {
0 ignored issues
show
Documentation introduced by
The property RatingDetails does not exist on object<Addon>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
                $data['RatingDetails'][] = [
26
                    'Name' => $k,
27
                    'Value' => $v,
28
                ];
29
            }
30
        }
31
32
        $data['Versions'] = [];
33
        foreach ($package->Versions() as $version) {
0 ignored issues
show
Bug introduced by
The method Versions() does not exist on Addon. Did you maybe mean SortedVersions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
            $versionData = $version->toMap();
35
            unset($versionData['LastEdited']);
36
            unset($versionData['Created']);
37
            unset($versionData['ClassName']);
38
            unset($versionData['RecordClassName']);
39
            unset($versionData['AddonID']);
40
41
42
            unset($versionData['Name']);
43
            unset($versionData['Type']);
44
            unset($versionData['Homepage']);
45
46
            if (!empty($versionData['ExtraValue'])) {
47
                $versionData['ExtraValue'] = unserialize($versionData['ExtraValue']);
48
            }
49
            if (!empty($versionData['LicenseValue'])) {
50
                $versionData['LicenseValue'] = unserialize($versionData['LicenseValue']);
51
            }
52
53
            foreach ($version->CompatibleVersions() as $compatible) {
54
                $versionData['CompatibleVersions'][] = [
55
                    "Version" => $compatible->Name,
56
                    "Major" => $compatible->Major,
57
                    "Minor" => $compatible->Minor,
58
                ];
59
            }
60
61
            foreach ($version->Authors() as $author) {
62
                $authorData = $author->toMap();
63
                unset($authorData['ID']);
64
                unset($authorData['ClassName']);
65
                unset($authorData['RecordClassName']);
66
                unset($authorData['LastEdited']);
67
                unset($authorData['Created']);
68
                $versionData['Authors'][] = $authorData;
69
            }
70
71
            $data['Versions'][] = $versionData;
72
        }
73
74
        return $data;
75
    }
76
}
77