AddonToArray::convert()   B
last analyzed

Complexity

Conditions 8
Paths 34

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 7.4755
c 0
b 0
f 0
cc 8
nc 34
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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