Completed
Pull Request — master (#176)
by Robbie
08:50 queued 07:19
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
    /**
20
     * @var CheckSuite
21
     */
22
    protected $checkSuite;
23
24
    public function __construct(PackagistService $packagist)
25
    {
26
        $this->packagist = $packagist;
27
    }
28
29
    public function build(Addon $addon)
30
    {
31
        putenv("GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no\"");
32
        $this->setCheckSuite(new CheckSuite());
33
34
        $composer = $this->packagist->getComposer();
35
        $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...
36
        $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...
37
        $time = time();
38
39
        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...
40
            throw new Exception('Could not find corresponding Packagist versions');
41
        }
42
43
        // Get the latest local and packagist version pair.
44
        $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...
45
        if (!$version) {
46
            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...
47
            $addon->delete();
48
            return;
49
        }
50
51
        foreach ($packageVersions as $packageVersion) {
52
            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...
53
                continue;
54
            }
55
56
            if (defined('SS_ADDONS_DOWNLOAD_PATH')) {
57
                $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...
58
            } else {
59
                $path = implode('/', array(
60
                    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...
61
                ));
62
            }
63
64
            // Convert PackagistAPI result into class compatible with Composer logic
65
            $package = new Package(
66
                $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...
67
                $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...
68
                $packageVersion->getVersion()
69
            );
70
71
            if ($extra = $packageVersion->getExtra()) {
72
                $package->setExtra((array) $extra);
73
            }
74
            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 getSourceType()?

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...
75
                $package->setSourceUrl($source->getUrl());
76
                $package->setSourceType($source->getType());
77
                $package->setSourceReference($source->getReference());
78
            }
79
            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 getDistType()?

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...
80
                $package->setDistUrl($dist->getUrl());
81
                $package->setDistType($dist->getType());
82
                $package->setDistReference($dist->getReference());
83
            }
84
85
            try {
86
                $this->download($package, $path);
87
88
            // If there's an error, mark this version as bad
89
            } catch (RuntimeException $e) {
90
                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...
91
                echo "Error message: " . $e->getMessage() . "\n";
92
                $addon->delete();
93
                return;
94
            }
95
96
            $this->buildReadme($addon, $path);
97
            $this->buildScreenshots($addon, $package, $path);
98
            $this->rateModule($addon, $path);
99
        }
100
101
        $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...
102
        $addon->write();
103
    }
104
105
    protected function download(PackageInterface $package, $path)
106
    {
107
        $this->packagist
108
            ->getComposer()
109
            ->getDownloadManager()
110
            ->download($package, $path);
111
    }
112
113
    /**
114
     * Parses a readme file from markdown to HTML, then purifies it
115
     * @param Addon  $addon
116
     * @param string $path
117
     */
118
    protected function buildReadme(Addon $addon, $path)
119
    {
120
        $candidates = array(
121
            'README.md',
122
            'README.markdown',
123
            'README.mdown',
124
            'docs/en/index.md'
125
        );
126
127
        foreach ($candidates as $candidate) {
128
            $lower = strtolower($candidate);
129
            $paths = array("$path/$candidate", "$path/$lower");
130
131
            foreach ($paths as $path) {
132
                if (!file_exists($path)) {
133
                    return;
134
                }
135
136
                $parser = GitHubMarkdownService::create();
137
                if ($context = $this->getGitHubContext($addon)) {
138
                    $parser->setContext($context);
139
                }
140
                $readme = $parser->toHtml(file_get_contents($path));
141
142
                if (empty($readme)) {
143
                    return;
144
                }
145
146
                $readme = $parser->toHtml(file_get_contents($path));
147
148
                $purifier = new HTMLPurifier();
149
                $readme = $purifier->purify($readme, array(
150
                    'Cache.SerializerPath' => TEMP_FOLDER
151
                ));
152
153
                $readme = $this->replaceRelativeLinks($addon, $readme);
154
                $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...
155
                return;
156
            }
157
        }
158
    }
159
160
    /**
161
     * Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path
162
     *
163
     * @param  Addon $addon
164
     * @return string|false
165
     */
166
    public function getGitHubContext(Addon $addon)
167
    {
168
        $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...
169
        if (stripos($repository, '://github.com/') === false) {
170
            return false;
171
        }
172
173
        preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches);
174
175
        if (isset($matches['module'])) {
176
            return $matches['module'];
177
        }
178
179
        return false;
180
    }
181
182
    /**
183
     * Given an addon and a parsed HTML readme string, find and replace relative links with absolute
184
     * repository path links. This method applies to GitHub repositories only.
185
     *
186
     * @param  Addon  $addon
187
     * @param  string $readme
188
     * @return string
189
     */
190
    public function replaceRelativeLinks(Addon $addon, $readme)
191
    {
192
        if (!$this->hasGitHubRepository($addon)) {
193
            return $readme;
194
        }
195
196
        $dom = new DOMDocument;
197
        // LibXML needs a wrapper element...
198
        $dom->loadHTML('<div>' . $readme . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
199
        $xpath = new DOMXPath($dom);
200
201
        // Select all anchors and images in the readme document
202
        $query = $xpath->query('//*[self::a or self::img]');
203
204
        foreach ($query as $element) { /** @var DOMElement $element */
205
            $attribute = ($element->nodeName === 'a') ? 'href' : 'src';
206
            $path = $element->getAttribute($attribute);
207
            if (!$this->isRelativeUri($path)) {
208
                continue;
209
            }
210
211
            // See GitHub readmes for example
212
            $folder = ($attribute === 'href') ? 'blob' : 'raw';
213
            $defaultBranch = 'master'; // Is this safe to assume?
214
215
            $element->setAttribute(
216
                $attribute,
217
                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...
218
            );
219
        }
220
221
        // Return the inner HTML of the wrapper div... Reference: stackoverflow.com/a/39193507/2812842
222
        $node = $dom->getElementsByTagName('div')->item(0);
223
        return implode(array_map([$node->ownerDocument, 'saveHTML'], iterator_to_array($node->childNodes)));
224
    }
225
226
    /**
227
     * Decide whether a URI path is relative or not. The regex pattern matches prefixes that start with
228
     * a protocol, a slash or a hash. If they don't start with those things, then they are deemed to be
229
     * relative paths.
230
     *
231
     * @param  string $path
232
     * @return bool
233
     */
234
    public function isRelativeUri($path)
235
    {
236
        return !preg_match('/(^(?:https?:\/\/|\/|#).*$)/', $path);
237
    }
238
239
    /**
240
     * Determine whether an addon is hosted on GitHub
241
     *
242
     * @param  Addon $addon
243
     * @return bool
244
     */
245
    public function hasGitHubRepository(Addon $addon)
246
    {
247
        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...
248
    }
249
250
    private function buildScreenshots(Addon $addon, PackageInterface $package, $path)
251
    {
252
        $extra = $package->getExtra();
253
        $screenshots = array();
254
        $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...
255
256
        if (isset($extra['screenshots'])) {
257
            $screenshots = (array) $extra['screenshots'];
258
        } elseif (isset($extra['screenshot'])) {
259
            $screenshots = (array) $extra['screenshot'];
260
        }
261
262
        // Delete existing screenshots.
263
        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...
264
            $screenshot->delete();
265
        }
266
267
        $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...
268
269
        foreach ($screenshots as $screenshot) {
270
            if (!is_string($screenshot)) {
271
                continue;
272
            }
273
274
            $scheme = parse_url($screenshot, PHP_URL_SCHEME);
275
276
            // Handle absolute image URLs.
277
            if ($scheme == 'http' || $scheme == 'https') {
278
                $temp = TEMP_FOLDER . '/' . md5($screenshot);
279
280
                if (!copy($screenshot, $temp)) {
281
                    continue;
282
                }
283
284
                $data = array(
285
                    'name' => basename($screenshot),
286
                    'size' => filesize($temp),
287
                    'tmp_name' => $temp,
288
                    'error' => 0
289
                );
290
            } // Handle images that are included in the repository.
291
            else {
292
                $source = $path . '/' . ltrim($screenshot, '/');
293
294
                // Prevent directory traversal.
295
                if ($source != realpath($source)) {
296
                    continue;
297
                }
298
299
                if (!file_exists($source)) {
300
                    continue;
301
                }
302
303
                $data = array(
304
                    'name' => basename($source),
305
                    'size' => filesize($source),
306
                    'tmp_name' => $source,
307
                    'error' => 0
308
                );
309
            }
310
311
            $upload = new Upload();
312
            $upload->setValidator(new AddonBuilderScreenshotValidator());
313
            $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...
314
315
            if ($file = $upload->getFile()) {
316
                $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...
317
            }
318
        }
319
    }
320
321
    /**
322
     * Use the Rating check runner to generate an automated rating for this module
323
     *
324
     * @param Addon $addon
325
     * @param string $modulePath
326
     */
327
    protected function rateModule(Addon $addon, $modulePath)
328
    {
329
        $suite = $this->getCheckSuite();
330
        $suite->setModuleRoot($modulePath);
331
        $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...
332
        // Get repository slug from URL
333
        preg_match('~.*\/(.+\/.+)(?:\.git)?$~', $repositoryUrl, $matches);
334
        $suite->setRepositorySlug(!empty($matches[1]) ? $matches[1] : '');
335
336
        try {
337
            $suite->run();
338
        } catch (\Exception $e) {
339
            echo $e->getMessage();
340
            return;
341
        }
342
        /** @var array $checkDetails */
343
        $checkDetails = $suite->getCheckDetails();
344
345
        // Pull part of the check details out that we want (code and points)
346
        $details = array();
347
        foreach ($checkDetails as $checkCode => $metrics) {
348
            $details[$checkCode] = $metrics['points'];
349
        }
350
351
        // Total score for the check suite
352
        $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...
353
        // Individual checks and scores for each
354
        $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...
355
    }
356
357
    /**
358
     * @return CheckSuite
359
     */
360
    public function getCheckSuite()
361
    {
362
        return $this->checkSuite;
363
    }
364
365
    /**
366
     * @param CheckSuite $checkSuite
367
     * @return $this
368
     */
369
    public function setCheckSuite($checkSuite)
370
    {
371
        $this->checkSuite = $checkSuite;
372
        return $this;
373
    }
374
}
375