AddonBuilder::build()   C
last analyzed

Complexity

Conditions 9
Paths 20

Size

Total Lines 70
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 6.1585
c 0
b 0
f 0
cc 9
eloc 45
nc 20
nop 1

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
use Composer\Package\Package;
4
use Composer\Package\PackageInterface;
5
6
/**
7
 * Downloads an add-on and builds more details information about it.
8
 */
9
class AddonBuilder
10
{
11
12
    const ADDONS_DIR = 'addon-downloads';
13
14
    const SCREENSHOTS_DIR = 'screenshots';
15
16
    private $packagist;
17
18
    public function __construct(PackagistService $packagist)
19
    {
20
        $this->packagist = $packagist;
21
    }
22
23
    public function build(Addon $addon)
24
    {
25
        putenv("GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no\"");
26
27
        $composer = $this->packagist->getComposer();
28
        $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...
29
        $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...
30
        $time = time();
31
32
        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...
33
            throw new Exception('Could not find corresponding Packagist versions');
34
        }
35
36
        // Get the latest local and packagist version pair.
37
        $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...
38
        if (!$version) {
39
            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...
40
            $addon->delete();
41
            return;
42
        }
43
44
        foreach ($packageVersions as $packageVersion) {
45
            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...
46
                continue;
47
            }
48
49
            if (defined('SS_ADDONS_DOWNLOAD_PATH')) {
50
                $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...
51
            } else {
52
                $path = implode('/', array(
53
                    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...
54
                ));
55
            }
56
57
            // Convert PackagistAPI result into class compatible with Composer logic
58
            $package = new Package(
59
                $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...
60
                $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...
61
                $packageVersion->getVersion()
62
            );
63
            $package->setExtra((array)$packageVersion->getExtra());
64
            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...
65
                $package->setSourceUrl($source->getUrl());
66
                $package->setSourceType($source->getType());
67
                $package->setSourceReference($source->getReference());
68
            }
69
            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...
70
                $package->setDistUrl($dist->getUrl());
71
                $package->setDistType($dist->getType());
72
                $package->setDistReference($dist->getReference());
73
            }
74
75
            try {
76
                $this->download($package, $path);
77
78
            // If there's an error, mark this version as bad
79
            } catch (RuntimeException $e) {
80
                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...
81
                echo "Error message: " . $e->getMessage() . "\n";
82
                $addon->delete();
83
                return;
84
            }
85
86
            $this->buildReadme($addon, $path);
87
            $this->buildScreenshots($addon, $package, $path);
88
        }
89
90
        $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...
91
        $addon->write();
92
    }
93
94
    protected function download(PackageInterface $package, $path)
95
    {
96
        $this->packagist
97
            ->getComposer()
98
            ->getDownloadManager()
99
            ->download($package, $path);
100
    }
101
102
    /**
103
     * Parses a readme file from markdown to HTML, then purifies it
104
     * @param Addon  $addon
105
     * @param string $path
106
     */
107
    protected function buildReadme(Addon $addon, $path)
108
    {
109
        $candidates = array(
110
            'README.md',
111
            'README.markdown',
112
            'README.mdown',
113
            'docs/en/index.md'
114
        );
115
116
        foreach ($candidates as $candidate) {
117
            $lower = strtolower($candidate);
118
            $paths = array("$path/$candidate", "$path/$lower");
119
120
            foreach ($paths as $path) {
121
                if (!file_exists($path)) {
122
                    return;
123
                }
124
125
                $parser = GitHubMarkdownService::create();
126
                if ($context = $this->getGitHubContext($addon)) {
127
                    $parser->setContext($context);
128
                }
129
                $readme = $parser->toHtml(file_get_contents($path));
130
131
                if (empty($readme)) {
132
                    return;
133
                }
134
135
                $readme = $parser->toHtml(file_get_contents($path));
136
137
                $purifier = new HTMLPurifier();
138
                $readme = $purifier->purify($readme, array(
139
                    'Cache.SerializerPath' => TEMP_FOLDER
140
                ));
141
142
                $readme = $this->replaceRelativeLinks($addon, $readme);
143
                $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...
144
                return;
145
            }
146
        }
147
    }
148
149
    /**
150
     * Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path
151
     *
152
     * @param  Addon $addon
153
     * @return string|false
154
     */
155
    public function getGitHubContext(Addon $addon)
156
    {
157
        $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...
158
        if (stripos($repository, '://github.com/') === false) {
159
            return false;
160
        }
161
162
        preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches);
163
164
        if (isset($matches['module'])) {
165
            return $matches['module'];
166
        }
167
168
        return false;
169
    }
170
171
    /**
172
     * Given an addon and a parsed HTML readme string, find and replace relative links with absolute
173
     * repository path links. This method applies to GitHub repositories only.
174
     *
175
     * @param  Addon  $addon
176
     * @param  string $readme
177
     * @return string
178
     */
179
    public function replaceRelativeLinks(Addon $addon, $readme)
180
    {
181
        if (!$this->hasGitHubRepository($addon)) {
182
            return $readme;
183
        }
184
185
        $dom = new DOMDocument;
186
        // LibXML needs a wrapper element...
187
        $dom->loadHTML('<div>' . $readme . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
188
        $xpath = new DOMXPath($dom);
189
190
        // Select all anchors and images in the readme document
191
        $query = $xpath->query('//*[self::a or self::img]');
192
193
        foreach ($query as $element) { /** @var DOMElement $element */
194
            $attribute = ($element->nodeName === 'a') ? 'href' : 'src';
195
            $path = $element->getAttribute($attribute);
196
            if (!$this->isRelativeUri($path)) {
197
                continue;
198
            }
199
200
            // See GitHub readmes for example
201
            $folder = ($attribute === 'href') ? 'blob' : 'raw';
202
            $defaultBranch = 'master'; // Is this safe to assume?
203
204
            $element->setAttribute(
205
                $attribute,
206
                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...
207
            );
208
        }
209
210
        // Return the inner HTML of the wrapper div... Reference: stackoverflow.com/a/39193507/2812842
211
        $node = $dom->getElementsByTagName('div')->item(0);
212
        return implode(array_map([$node->ownerDocument, 'saveHTML'], iterator_to_array($node->childNodes)));
213
    }
214
215
    /**
216
     * Decide whether a URI path is relative or not. The regex pattern matches prefixes that start with
217
     * a protocol, a slash or a hash. If they don't start with those things, then they are deemed to be
218
     * relative paths.
219
     *
220
     * @param  string $path
221
     * @return bool
222
     */
223
    public function isRelativeUri($path)
224
    {
225
        return !preg_match('/(^(?:https?:\/\/|\/|#).*$)/', $path);
226
    }
227
228
    /**
229
     * Determine whether an addon is hosted on GitHub
230
     *
231
     * @param  Addon $addon
232
     * @return bool
233
     */
234
    public function hasGitHubRepository(Addon $addon)
235
    {
236
        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...
237
    }
238
239
    private function buildScreenshots(Addon $addon, PackageInterface $package, $path)
240
    {
241
        $extra = $package->getExtra();
242
        $screenshots = array();
243
        $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...
244
245
        if (isset($extra['screenshots'])) {
246
            $screenshots = (array) $extra['screenshots'];
247
        } elseif (isset($extra['screenshot'])) {
248
            $screenshots = (array) $extra['screenshot'];
249
        }
250
251
        // Delete existing screenshots.
252
        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...
253
            $screenshot->delete();
254
        }
255
256
        $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...
257
258
        foreach ($screenshots as $screenshot) {
259
            if (!is_string($screenshot)) {
260
                continue;
261
            }
262
263
            $scheme = parse_url($screenshot, PHP_URL_SCHEME);
264
265
            // Handle absolute image URLs.
266
            if ($scheme == 'http' || $scheme == 'https') {
267
                $temp = TEMP_FOLDER . '/' . md5($screenshot);
268
269
                if (!copy($screenshot, $temp)) {
270
                    continue;
271
                }
272
273
                $data = array(
274
                    'name' => basename($screenshot),
275
                    'size' => filesize($temp),
276
                    'tmp_name' => $temp,
277
                    'error' => 0
278
                );
279
            } // Handle images that are included in the repository.
280
            else {
281
                $source = $path . '/' . ltrim($screenshot, '/');
282
283
                // Prevent directory traversal.
284
                if ($source != realpath($source)) {
285
                    continue;
286
                }
287
288
                if (!file_exists($source)) {
289
                    continue;
290
                }
291
292
                $data = array(
293
                    'name' => basename($source),
294
                    'size' => filesize($source),
295
                    'tmp_name' => $source,
296
                    'error' => 0
297
                );
298
            }
299
300
            $upload = new Upload();
301
            $upload->setValidator(new AddonBuilderScreenshotValidator());
302
            $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...
303
304
            if ($file = $upload->getFile()) {
305
                $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...
306
            }
307
        }
308
    }
309
}
310