Completed
Pull Request — master (#164)
by Sam
06:46
created

AddonUpdater::updateAuthors()   D

Complexity

Conditions 14
Paths 131

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 4.8094
c 0
b 0
f 0
cc 14
eloc 27
nc 131
nop 2

How to fix   Complexity   

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
use Composer\Package\AliasPackage;
4
use Composer\Package\CompletePackage;
5
use Guzzle\Http\Exception\ClientErrorResponseException;
6
use SilverStripe\Elastica\ElasticaService;
7
use Packagist\Api\Result\Package;
8
use Composer\Package\Version\VersionParser;
9
use Packagist\Api\Result\Package\Version;
10
11
/**
12
 * Updates all add-ons from Packagist.
13
 */
14
class AddonUpdater
15
{
16
17
    /**
18
     * @var PackagistService
19
     */
20
    private $packagist;
21
22
    /**
23
     * @var SilverStripe\Elastica\ElasticaService
24
     */
25
    private $elastica;
26
27
    /**
28
     * @var ResqueService
29
     */
30
    private $resque;
31
32
    /**
33
     * @var SilverStripeVersion[]
34
     */
35
    private $silverstripes;
36
37
    public function __construct(
38
        PackagistService $packagist,
39
        ElasticaService $elastica,
40
        ResqueService $resque
41
    ) {
42
        $this->packagist = $packagist;
43
        $this->elastica = $elastica;
44
        $this->resque = $resque;
45
46
        $this->setSilverStripeVersions(SilverStripeVersion::get());
47
    }
48
49
    /**
50
     * Updates all add-ons.
51
     *
52
     * @param Boolean Clear existing addons before updating them.
53
     * Will also clear their search index, and cascade the delete for associated data.
54
     * @param Array Limit to specific addons, using their name incl. vendor prefix.
55
     */
56
    public function update($clear = false, $limitAddons = null)
57
    {
58
        if ($clear && !$limitAddons) {
59
            Addon::get()->removeAll();
60
            AddonAuthor::get()->removeAll();
61
            AddonKeyword::get()->removeAll();
62
            AddonLink::get()->removeAll();
63
            AddonVendor::get()->removeAll();
64
            AddonVersion::get()->removeAll();
65
        }
66
67
        // This call to packagist can be expensive. Requests are served from a cache if usePackagistCache() returns true
68
        $cache = SS_Cache::factory('addons');
69
        if ($this->usePackagistCache() && $packages = $cache->load('packagist')) {
70
            $packages = unserialize($packages);
71
        } else {
72
            $packages = $this->packagist->getPackages();
73
            $cache->save(serialize($packages), 'packagist');
74
        }
75
        $this->elastica->startBulkIndex();
76
77
        foreach ($packages as $package) {
78
            /** @var Packagist\Api\Result\Package $package */
79
80
            $isAbandoned = (method_exists($package, 'isAbandoned') && $package->isAbandoned());
81
            $name = $package->getName();
82
            $versions = $package->getVersions();
83
84
            if ($limitAddons && !in_array($name, $limitAddons)) {
85
                continue;
86
            }
87
88
            $addon = Addon::get()->filter('Name', $name)->first();
89
90
            if (!$addon) {
91
                if ($isAbandoned) {
92
                    echo ' - Skipping abandoned package: ' . $name, PHP_EOL;
93
                    continue;
94
                }
95
96
                $addon = new Addon();
97
                $addon->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
98
                $addon->write();
99
            }
100
101
            if ($isAbandoned) {
102
                echo ' - Removing abandoned package: ' . $name, PHP_EOL;
103
                $addon->delete();
104
                continue;
105
            }
106
107
            usort($versions, function ($a, $b) {
108
                return version_compare($a->getVersionNormalized(), $b->getVersionNormalized());
109
            });
110
111
            $this->updateAddon($addon, $package, $versions);
0 ignored issues
show
Compatibility introduced by
$addon of type object<DataObject> is not a sub-type of object<Addon>. It seems like you assume a child class of the class DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
        }
113
114
        $this->elastica->endBulkIndex();
115
    }
116
117
118
119
    /**
120
     * Check whether or not we should contact packagist or use a cached version. This allows to speed up the task
121
     * during development.
122
     *
123
     * @return bool
124
     */
125
    protected function usePackagistCache()
126
    {
127
        return Director::isDev();
128
    }
129
130
    private function updateAddon(Addon $addon, Package $package, array $versions)
131
    {
132
        if (!$addon->VendorID) {
0 ignored issues
show
Documentation introduced by
The property VendorID 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...
133
            $vendor = AddonVendor::get()->filter('Name', $addon->VendorName())->first();
134
135
            if (!$vendor) {
136
                $vendor = new AddonVendor();
137
                $vendor->Name = $addon->VendorName();
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<AddonVendor>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
138
                $vendor->write();
139
            }
140
141
            $addon->VendorID = $vendor->ID;
0 ignored issues
show
Documentation introduced by
The property VendorID does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
142
        }
143
144
        $addon->Type = preg_replace('/^silverstripe-(vendor)?/', '', $package->getType());
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
145
        $addon->Description = $package->getDescription();
0 ignored issues
show
Documentation introduced by
The property Description does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
146
        $addon->Released = strtotime($package->getTime());
0 ignored issues
show
Documentation introduced by
The property Released does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
147
        $addon->Repository = $package->getRepository();
0 ignored issues
show
Documentation introduced by
The property Repository does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
148
        $addon->Downloads = $package->getDownloads()->getTotal();
0 ignored issues
show
Documentation introduced by
The property Downloads does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
149
        $addon->DownloadsMonthly = $package->getDownloads()->getMonthly();
0 ignored issues
show
Documentation introduced by
The property DownloadsMonthly does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
150
        $addon->Favers = $package->getFavers();
0 ignored issues
show
Documentation introduced by
The property Favers does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
151
152
        foreach ($versions as $version) {
153
            $this->updateVersion($addon, $version);
154
        }
155
156
        // If there is no build, then queue one up if the add-on requires
157
        // one.
158
        if (!$addon->BuildQueued) {
0 ignored issues
show
Documentation introduced by
The property BuildQueued 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...
159
            if (!$addon->BuiltAt) {
0 ignored issues
show
Documentation introduced by
The property BuiltAt 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...
160
                $this->resque->queue('first_build', 'BuildAddonJob', array('id' => $addon->ID));
161
                $addon->BuildQueued = true;
0 ignored issues
show
Documentation introduced by
The property BuildQueued does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
162
            } else {
163
                $built = (int) $addon->obj('BuiltAt')->format('U');
164
165
                foreach ($versions as $version) {
166
                    if (strtotime($version->getTime()) > $built) {
167
                        $this->resque->queue('update', 'BuildAddonJob', array('id' => $addon->ID));
168
                        $addon->BuildQueued = true;
0 ignored issues
show
Documentation introduced by
The property BuildQueued does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
169
170
                        break;
171
                    }
172
                }
173
            }
174
        }
175
176
        $addon->LastUpdated = time();
0 ignored issues
show
Documentation introduced by
The property LastUpdated does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
177
        $addon->write();
178
    }
179
180
    private function updateVersion(Addon $addon, Version $package)
181
    {
182
        $version = null;
183
184
        if ($addon->isInDB()) {
185
            $version = $addon->Versions()->filter('Version', $package->getVersionNormalized())->first();
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...
186
        }
187
188
        if (!$version) {
189
            $version = new AddonVersion();
190
        }
191
192
        $version->Name = $package->getName();
193
        $version->Type = preg_replace('/^silverstripe-(vendor)?/', '', $package->getType());
194
        $version->Description = $package->getDescription();
195
        $version->Released = strtotime($package->getTime());
196
        $keywords = $package->getKeywords();
197
198
        if ($keywords) {
199
            foreach ($keywords as $keyword) {
0 ignored issues
show
Bug introduced by
The expression $keywords of type string is not traversable.
Loading history...
200
                $keyword = AddonKeyword::get_by_name($keyword);
201
202
                $addon->Keywords()->add($keyword);
0 ignored issues
show
Documentation Bug introduced by
The method Keywords does not exist on object<Addon>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
203
                $version->Keywords()->add($keyword);
204
            }
205
        }
206
207
        $version->Version = $package->getVersionNormalized();
208
        $version->PrettyVersion = $package->getVersion();
209
210
        $stability = VersionParser::parseStability($package->getVersion());
211
        $isDev = $stability === 'dev';
212
        $version->Development = $isDev;
213
214
        $version->SourceType = $package->getSource()->getType();
215
        $version->SourceUrl = $package->getSource()->getUrl();
216
        $version->SourceReference = $package->getSource()->getReference();
217
218
        if ($package->getDist()) {
219
            $version->DistType = $package->getDist()->getType();
220
            $version->DistUrl = $package->getDist()->getUrl();
221
            $version->DistReference = $package->getDist()->getReference();
222
            $version->DistChecksum = $package->getDist()->getShasum();
223
        }
224
225
        $version->Extra = $package->getExtra();
226
        $version->Homepage = $package->getHomepage();
227
        $version->License = $package->getLicense();
228
        // $version->Support = $package->getSupport();
229
230
        $addon->Versions()->add($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...
231
232
        $this->updateLinks($version, $package);
233
        $this->updateCompatibility($addon, $version, $package);
234
        $this->updateAuthors($version, $package);
235
    }
236
237
    private function updateLinks(AddonVersion $version, Version $package)
238
    {
239
        $getLink = function ($name, $type) use ($version) {
240
            $link = null;
241
242
            if ($version->isInDB()) {
243
                $link = $version->Links()->filter('Name', $name)->filter('Type', $type)->first();
0 ignored issues
show
Documentation Bug introduced by
The method Links does not exist on object<AddonVersion>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
244
            }
245
246
            if (!$link) {
247
                $link = new AddonLink();
248
                $link->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<AddonLink>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
249
                $link->Type = $type;
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<AddonLink>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
250
            }
251
252
            return $link;
253
        };
254
255
        $types = array(
256
            'require' => 'getRequire',
257
            'require-dev' => 'getRequireDev',
258
            'provide' => 'getProvide',
259
            'conflict' => 'getConflict',
260
            'replace' => 'getReplace'
261
        );
262
263
        foreach ($types as $type => $method) {
264
            if ($linked = $package->$method()) {
265
                foreach ($linked as $link => $constraint) {
266
                    $name = $link;
267
                    $addon = Addon::get()->filter('Name', $name)->first();
268
269
                    $local = $getLink($name, $type);
270
                    $local->Constraint = $constraint;
271
272
                    if ($addon) {
273
                        $local->TargetID = $addon->ID;
274
                    }
275
276
                    $version->Links()->add($local);
0 ignored issues
show
Documentation Bug introduced by
The method Links does not exist on object<AddonVersion>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
277
                }
278
            }
279
        }
280
281
        //to-do api have no method to get this.
282
        /*$suggested = $package->getSuggests();
283
284
		if ($suggested) foreach ($suggested as $package => $description) {
285
			$link = $getLink($package, 'suggest');
286
			$link->Description = $description;
287
288
			$version->Links()->add($link);
289
		}*/
290
    }
291
292
    private function updateCompatibility(Addon $addon, AddonVersion $version, Version $package)
293
    {
294
        $require = null;
295
296
        if ($package->getRequire()) {
297
            foreach ($package->getRequire() as $name => $link) {
298
                if ((string)$link == 'self.version') {
299
                    continue;
300
                }
301
302
                if ($name == 'silverstripe/framework') {
303
                    $require = $link;
304
                    break;
305
                }
306
307
                if ($name == 'silverstripe/cms') {
308
                    $require = $link;
309
                }
310
            }
311
        }
312
313
        if (!$require) {
314
            return;
315
        }
316
317
        //  >= interpreted as ^, see https://github.com/silverstripe/addons.silverstripe.org/issues/160
318
        $require = preg_replace('/^>=/', '^', $require);
319
320
        $addon->CompatibleVersions()->removeAll();
0 ignored issues
show
Documentation Bug introduced by
The method CompatibleVersions does not exist on object<Addon>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
321
        $version->CompatibleVersions()->removeAll();
0 ignored issues
show
Documentation Bug introduced by
The method CompatibleVersions does not exist on object<AddonVersion>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
322
323
        foreach ($this->getSilverStripeVersions() as $silverStripeVersion) {
324
            /** @var SilverStripeVersion $silverStripeVersion */
325
            try {
326
                if ($silverStripeVersion->getConstraintValidity($require)) {
327
                    $addon->CompatibleVersions()->add($silverStripeVersion);
0 ignored issues
show
Documentation Bug introduced by
The method CompatibleVersions does not exist on object<Addon>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
328
                    $version->CompatibleVersions()->add($silverStripeVersion);
0 ignored issues
show
Documentation Bug introduced by
The method CompatibleVersions does not exist on object<AddonVersion>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
329
                }
330
            } catch (Exception $e) {
331
                // An exception here shouldn't prevent further updates.
332
                Debug::log($addon->Name . "\t" . $addon->ID . "\t" . $e->getMessage());
0 ignored issues
show
Documentation introduced by
The property Name 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...
333
            }
334
        }
335
    }
336
337
    private function updateAuthors(AddonVersion $version, Version $package)
338
    {
339
        if ($package->getAuthors()) {
340
            foreach ($package->getAuthors() as $details) {
341
                $author = null;
342
343
                if (!$details->getName() && !$details->getEmail()) {
344
                    continue;
345
                }
346
347
                if ($details->getEmail()) {
348
                    $author = AddonAuthor::get()->filter('Email', $details->getEmail())->first();
349
                }
350
351
                if (!$author && $details->getHomepage()) {
352
                    $author = AddonAuthor::get()
353
                    ->filter('Name', $details->getName())
354
                    ->filter('Homepage', $details->getHomepage())
355
                    ->first();
356
                }
357
358
                if (!$author && $details->getName()) {
359
                    $author = AddonAuthor::get()
360
                    ->filter('Name', $details->getName())
361
                    ->filter('Versions.Addon.Name', $package->getName())
362
                    ->first();
363
                }
364
365
                if (!$author) {
366
                    $author = new AddonAuthor();
367
                }
368
369
                if ($details->getName()) {
370
                    $author->Name = $details->getName();
371
                }
372
                if ($details->getEmail()) {
373
                    $author->Email = $details->getEmail();
374
                }
375
                if ($details->getHomepage()) {
376
                    $author->Homepage = $details->getHomepage();
377
                }
378
379
                        //to-do not supported by API
380
                        //if(isset($details['role'])) $author->Role = $details['role'];
381
382
                $version->Authors()->add($author->write());
0 ignored issues
show
Documentation Bug introduced by
The method Authors does not exist on object<AddonVersion>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
383
            }
384
        }
385
    }
386
387
    /**
388
     * Get the list of SilverStripe versions
389
     *
390
     * @return DataList
391
     */
392
    public function getSilverStripeVersions()
393
    {
394
        return $this->silverstripes;
395
    }
396
397
    /**
398
     * Set the list of SilverStripeVersions
399
     *
400
     * @param  DataList $versions
401
     * @return $this
402
     */
403
    public function setSilverStripeVersions(DataList $versions)
404
    {
405
        $this->silverstripes = $versions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $versions of type object<DataList> is incompatible with the declared type array<integer,object<SilverStripeVersion>> of property $silverstripes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
406
        return $this;
407
    }
408
}
409