Completed
Pull Request — master (#176)
by Robbie
02:59 queued 01:28
created

AddonBuilder::rateModule()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 3
nop 2
1
<?php
2
3
use Composer\Package\Package;
4
use Composer\Package\PackageInterface;
5
use SilverStripe\ModuleRatings\CheckSuite;
6
7
/**
8
 * Downloads an add-on and builds more details information about it.
9
 */
10
class AddonBuilder
11
{
12
13
    const ADDONS_DIR = 'addon-downloads';
14
15
    const SCREENSHOTS_DIR = 'screenshots';
16
17
    private $packagist;
18
19
    public function __construct(PackagistService $packagist)
20
    {
21
        $this->packagist = $packagist;
22
    }
23
24
    public function build(Addon $addon)
25
    {
26
        putenv("GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no\"");
27
28
        $composer = $this->packagist->getComposer();
29
        $downloader = $composer->getDownloadManager();
0 ignored issues
show
Unused Code introduced by
$downloader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
        $packageVersions = $this->packagist->getPackageVersions($addon->Name);
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...
31
        $time = time();
32
33
        if (!$packageVersions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $packageVersions of type Composer\Package\PackageInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
            throw new Exception('Could not find corresponding Packagist versions');
35
        }
36
37
        // Get the latest local and packagist version pair.
38
        $version = $addon->Versions()->filter('Development', true)->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...
39
        if (!$version) {
40
            echo "No versions found for " . $addon->Name . "; deleting orphan record.\n";
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...
41
            $addon->delete();
42
            return;
43
        }
44
45
        foreach ($packageVersions as $packageVersion) {
46
            if ($packageVersion->getVersionNormalized() != $version->Version) {
0 ignored issues
show
Bug introduced by
The method getVersionNormalized() does not exist on Composer\Package\PackageInterface. Did you maybe mean getVersion()?

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...
47
                continue;
48
            }
49
50
            if (defined('SS_ADDONS_DOWNLOAD_PATH')) {
51
                $path = SS_ADDONS_DOWNLOAD_PATH . '/' . $addon->Name;
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...
52
            } else {
53
                $path = implode('/', array(
54
                    TEMP_FOLDER, self::ADDONS_DIR, $addon->Name
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...
55
                ));
56
            }
57
58
            // Convert PackagistAPI result into class compatible with Composer logic
59
            $package = new Package(
60
                $addon->Name,
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...
61
                $packageVersion->getVersionNormalized(),
0 ignored issues
show
Bug introduced by
The method getVersionNormalized() does not exist on Composer\Package\PackageInterface. Did you maybe mean getVersion()?

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...
62
                $packageVersion->getVersion()
63
            );
64
65
            if ($extra = $packageVersion->getExtra()) {
66
                $package->setExtra((array) $extra);
67
            }
68
            if ($source = $packageVersion->getSource()) {
0 ignored issues
show
Bug introduced by
The method getSource() does not exist on Composer\Package\PackageInterface. Did you maybe mean getSourceMirrors()?

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...
69
                $package->setSourceUrl($source->getUrl());
70
                $package->setSourceType($source->getType());
71
                $package->setSourceReference($source->getReference());
72
            }
73
            if ($dist = $packageVersion->getDist()) {
0 ignored issues
show
Bug introduced by
The method getDist() does not exist on Composer\Package\PackageInterface. Did you maybe mean getDistMirrors()?

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...
74
                $package->setDistUrl($dist->getUrl());
75
                $package->setDistType($dist->getType());
76
                $package->setDistReference($dist->getReference());
77
            }
78
79
            try {
80
                $this->download($package, $path);
81
82
            // If there's an error, mark this version as bad
83
            } catch (RuntimeException $e) {
84
                echo "Add-on " . $addon->Name . " couldn't be downloaded; deleting from database.\n";
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...
85
                echo "Error message: " . $e->getMessage() . "\n";
86
                $addon->delete();
87
                return;
88
            }
89
90
            $this->buildReadme($addon, $path);
91
            $this->buildScreenshots($addon, $package, $path);
92
            $this->rateModule($addon, $path);
93
        }
94
95
        $addon->LastBuilt = $time;
0 ignored issues
show
Documentation introduced by
The property LastBuilt 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...
96
        $addon->write();
97
    }
98
99
    protected function download(PackageInterface $package, $path)
100
    {
101
        $this->packagist
102
            ->getComposer()
103
            ->getDownloadManager()
104
            ->download($package, $path);
105
    }
106
107
    /**
108
     * Parses a readme file from markdown to HTML, then purifies it
109
     * @param Addon  $addon
110
     * @param string $path
111
     */
112
    protected function buildReadme(Addon $addon, $path)
113
    {
114
        $candidates = array(
115
            'README.md',
116
            'README.markdown',
117
            'README.mdown',
118
            'docs/en/index.md'
119
        );
120
121
        foreach ($candidates as $candidate) {
122
            $lower = strtolower($candidate);
123
            $paths = array("$path/$candidate", "$path/$lower");
124
125
            foreach ($paths as $path) {
126
                if (!file_exists($path)) {
127
                    return;
128
                }
129
130
                $parser = GitHubMarkdownService::create();
131
                if ($context = $this->getGitHubContext($addon)) {
132
                    $parser->setContext($context);
133
                }
134
                $readme = $parser->toHtml(file_get_contents($path));
135
136
                if (empty($readme)) {
137
                    return;
138
                }
139
140
                $readme = $parser->toHtml(file_get_contents($path));
141
142
                $purifier = new HTMLPurifier();
143
                $readme = $purifier->purify($readme, array(
144
                    'Cache.SerializerPath' => TEMP_FOLDER
145
                ));
146
147
                $readme = $this->replaceRelativeLinks($addon, $readme);
148
                $addon->Readme = $readme;
0 ignored issues
show
Documentation introduced by
The property Readme 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
                return;
150
            }
151
        }
152
    }
153
154
    /**
155
     * Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path
156
     *
157
     * @param  Addon $addon
158
     * @return string|false
159
     */
160
    public function getGitHubContext(Addon $addon)
161
    {
162
        $repository = $addon->Repository;
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...
163
        if (stripos($repository, '://github.com/') === false) {
164
            return false;
165
        }
166
167
        preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches);
168
169
        if (isset($matches['module'])) {
170
            return $matches['module'];
171
        }
172
173
        return false;
174
    }
175
176
    /**
177
     * Given an addon and a parsed HTML readme string, find and replace relative links with absolute
178
     * repository path links. This method applies to GitHub repositories only.
179
     *
180
     * @param  Addon  $addon
181
     * @param  string $readme
182
     * @return string
183
     */
184
    public function replaceRelativeLinks(Addon $addon, $readme)
185
    {
186
        if (!$this->hasGitHubRepository($addon)) {
187
            return $readme;
188
        }
189
190
        $dom = new DOMDocument;
191
        // LibXML needs a wrapper element...
192
        $dom->loadHTML('<div>' . $readme . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
193
        $xpath = new DOMXPath($dom);
194
195
        // Select all anchors and images in the readme document
196
        $query = $xpath->query('//*[self::a or self::img]');
197
198
        foreach ($query as $element) { /** @var DOMElement $element */
199
            $attribute = ($element->nodeName === 'a') ? 'href' : 'src';
200
            $path = $element->getAttribute($attribute);
201
            if (!$this->isRelativeUri($path)) {
202
                continue;
203
            }
204
205
            // See GitHub readmes for example
206
            $folder = ($attribute === 'href') ? 'blob' : 'raw';
207
            $defaultBranch = 'master'; // Is this safe to assume?
208
209
            $element->setAttribute(
210
                $attribute,
211
                implode('/', array($addon->Repository, $folder, $defaultBranch, $path))
0 ignored issues
show
Documentation introduced by
The property Repository 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...
212
            );
213
        }
214
215
        // Return the inner HTML of the wrapper div... Reference: stackoverflow.com/a/39193507/2812842
216
        $node = $dom->getElementsByTagName('div')->item(0);
217
        return implode(array_map([$node->ownerDocument, 'saveHTML'], iterator_to_array($node->childNodes)));
218
    }
219
220
    /**
221
     * Decide whether a URI path is relative or not. The regex pattern matches prefixes that start with
222
     * a protocol, a slash or a hash. If they don't start with those things, then they are deemed to be
223
     * relative paths.
224
     *
225
     * @param  string $path
226
     * @return bool
227
     */
228
    public function isRelativeUri($path)
229
    {
230
        return !preg_match('/(^(?:https?:\/\/|\/|#).*$)/', $path);
231
    }
232
233
    /**
234
     * Determine whether an addon is hosted on GitHub
235
     *
236
     * @param  Addon $addon
237
     * @return bool
238
     */
239
    public function hasGitHubRepository(Addon $addon)
240
    {
241
        return (strpos($addon->Repository, 'github.com') !== false);
0 ignored issues
show
Documentation introduced by
The property Repository 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...
242
    }
243
244
    private function buildScreenshots(Addon $addon, PackageInterface $package, $path)
245
    {
246
        $extra = $package->getExtra();
247
        $screenshots = array();
248
        $target = self::SCREENSHOTS_DIR . '/' . $addon->Name;
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...
249
250
        if (isset($extra['screenshots'])) {
251
            $screenshots = (array) $extra['screenshots'];
252
        } elseif (isset($extra['screenshot'])) {
253
            $screenshots = (array) $extra['screenshot'];
254
        }
255
256
        // Delete existing screenshots.
257
        foreach ($addon->Screenshots() as $screenshot) {
0 ignored issues
show
Documentation Bug introduced by
The method Screenshots 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...
258
            $screenshot->delete();
259
        }
260
261
        $addon->Screenshots()->removeAll();
0 ignored issues
show
Documentation Bug introduced by
The method Screenshots 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...
262
263
        foreach ($screenshots as $screenshot) {
264
            if (!is_string($screenshot)) {
265
                continue;
266
            }
267
268
            $scheme = parse_url($screenshot, PHP_URL_SCHEME);
269
270
            // Handle absolute image URLs.
271
            if ($scheme == 'http' || $scheme == 'https') {
272
                $temp = TEMP_FOLDER . '/' . md5($screenshot);
273
274
                if (!copy($screenshot, $temp)) {
275
                    continue;
276
                }
277
278
                $data = array(
279
                    'name' => basename($screenshot),
280
                    'size' => filesize($temp),
281
                    'tmp_name' => $temp,
282
                    'error' => 0
283
                );
284
            } // Handle images that are included in the repository.
285
            else {
286
                $source = $path . '/' . ltrim($screenshot, '/');
287
288
                // Prevent directory traversal.
289
                if ($source != realpath($source)) {
290
                    continue;
291
                }
292
293
                if (!file_exists($source)) {
294
                    continue;
295
                }
296
297
                $data = array(
298
                    'name' => basename($source),
299
                    'size' => filesize($source),
300
                    'tmp_name' => $source,
301
                    'error' => 0
302
                );
303
            }
304
305
            $upload = new Upload();
306
            $upload->setValidator(new AddonBuilderScreenshotValidator());
307
            $upload->load($data, $target);
0 ignored issues
show
Documentation introduced by
$target is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
308
309
            if ($file = $upload->getFile()) {
310
                $addon->Screenshots()->add($file);
0 ignored issues
show
Documentation Bug introduced by
The method Screenshots 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...
311
            }
312
        }
313
    }
314
315
    /**
316
     * Use the Rating check runner to generate an automated rating for this module
317
     *
318
     * @param Addon $addon
319
     * @param string $modulePath
320
     */
321
    protected function rateModule(Addon $addon, $modulePath)
322
    {
323
        $suite = new CheckSuite();
324
        $suite->setModuleRoot($modulePath);
325
        $repositoryUrl = $addon->Repository;
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...
326
        // Get repository slug from URL
327
        preg_match('~.*\/(.+\/.+)(?:\.git)?$~', $repositoryUrl, $matches);
328
        $suite->setRepositorySlug(!empty($matches[1]) ? $matches[1] : '');
329
330
        try {
331
            $suite->run();
332
        } catch (\Exception $e) {
333
            echo $e->getMessage();
334
            return;
335
        }
336
        /** @var array $checkDetails */
337
        $checkDetails = $suite->getCheckDetails();
338
339
        // Pull part of the check details out that we want (code and points)
340
        $details = array();
341
        foreach ($checkDetails as $checkCode => $metrics) {
342
            $details[$checkCode] = $metrics['points'];
343
        }
344
345
        // Total score for the check suite
346
        $addon->Rating = $suite->getScore();
0 ignored issues
show
Documentation introduced by
The property Rating 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...
347
        // Individual checks and scores for each
348
        $addon->RatingDetails = Convert::raw2json($details);
0 ignored issues
show
Documentation introduced by
The property RatingDetails 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...
349
    }
350
}
351