GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GistBackupHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A backup() 0 14 2
B prepareFiles() 0 22 4
A zipFiles() 0 18 3
A removeFiles() 0 4 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Models\User;
6
7
class GistBackupHandler
8
{
9
    /** @var User $user */
10
    private $user;
11
12
    /** @var GistFinder $gistFinder */
13
    private $gistFinder;
14
15
    /** Max size in bytes of file that can be archived */
16
    const MAX_FILE_SIZE = 100000;
17
18
    function __construct(GistFinder $gistFinder)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        $this->user = auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
21
        $this->gistFinder = $gistFinder;
22
    }
23
24
    /**
25
     * Backup gists.
26
     *
27
     * @return string|integer A path to gist archive or 0 if there are no files to backup.
28
     */
29
    public function backup()
30
    {
31
        $files = $this->prepareFiles();
32
33
        if (!count($files)) {
34
            return 0;
35
        }
36
37
        $zipPath = $this->zipFiles($files);
38
39
        $this->removeFiles();
40
41
        return $zipPath;
42
    }
43
44
    /**
45
     * Get each gist file and save it in the user folder.
46
     *
47
     * Also checks the size of a file to determine if it should be ignored.
48
     *
49
     * @return array Paths to gist files.
50
     */
51
    private function prepareFiles()
52
    {
53
        $gists = $this->gistFinder->fetchRawGists();
54
55
        $files = [];
56
        foreach ($gists as $gist) {
57
            foreach ($gist['files'] as $file) {
58
                if ($file['size'] < self::MAX_FILE_SIZE) {
59
                    $filename = $file['filename'];
60
                    $contents = $this->gistFinder->getGistFileContents($file['raw_url']);
61
                    $fileDate = date('YmdHis', strtotime($gist['updated_at']));
62
                    $filePath = "backups/{$this->user->id}/tmp/{$fileDate}-{$filename}";
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Models\User>. 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...
63
64
                    \Storage::put($filePath, $contents);
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
                    $files[] = storage_path("app/$filePath");
67
                }
68
            }
69
        }
70
71
        return $files;
72
    }
73
74
    /**
75
     * Create a zip archive and add files to it.
76
     *
77
     * @param array $files
78
     *
79
     * @return string Path to zip archive.
80
     *
81
     * @throws \Exception If the archive cannot be created.
82
     */
83
    private function zipFiles(array $files)
84
    {
85
        $zipName = date("YmdHis") . '-gists-' . str_slug($this->user->nickname);
0 ignored issues
show
Documentation introduced by
The property nickname does not exist on object<App\Models\User>. 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...
86
        $zipPath = storage_path("app/backups/{$this->user->id}/{$zipName}.zip");
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Models\User>. 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...
87
88
        $zip = new \ZipArchive;
89
        if ($zip->open($zipPath, \ZipArchive::CREATE) === true) {
90
            foreach ($files as $file) {
91
                $zip->addFile($file, basename($file));
92
            }
93
94
            $zip->close();
95
96
            return $zipPath;
97
        } else {
98
            throw new \Exception("Cannot open $zipPath for writing.");
99
        }
100
    }
101
102
    /**
103
     * Delete the previously prepared files.
104
     */
105
    private function removeFiles()
106
    {
107
        \Storage::deleteDirectory("backups/{$this->user->id}/tmp");
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Models\User>. 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...
Bug introduced by
The method deleteDirectory() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
    }
109
}
110