Completed
Push — v3-ssp ( 4b57b0...23932e )
by Sam
01:47
created

AddonBuilder::build()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 66
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

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