FileFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 75
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extension() 0 3 1
A fileName() 0 3 1
A getTemporaryDirectory() 0 16 3
A createTemporary() 0 18 2
1
<?php
2
3
namespace Lincable\Http\File;
4
5
use Illuminate\Http\File;
6
7
class FileFactory
8
{
9
    /**
10
     * Create a temporary file from specified original content.
11
     *
12
     * @param  mixed  $data
13
     * @param  string  $originalFileName
14
     * @param  mixed|null  $flags
15
     * @return void
16
     */
17 4
    public static function createTemporary(
18
        $data,
19
        string $originalFileName = null, 
20
        $flags = null
21
    ) {
22
        // Generate a temp file from configuration and file name.
23 4
        $filename = static::getTemporaryDirectory(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $filename is correct as static::getTemporaryDire...ion($originalFileName)) targeting Lincable\Http\File\FileF...getTemporaryDirectory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24 4
            str_random().'.'.static::extension($originalFileName)
0 ignored issues
show
Bug introduced by
It seems like $originalFileName can also be of type null; however, parameter $filename of Lincable\Http\File\FileFactory::extension() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            str_random().'.'.static::extension(/** @scrutinizer ignore-type */ $originalFileName)
Loading history...
25
        );
26
27 4
        file_put_contents($filename, $data, $flags);
0 ignored issues
show
Bug introduced by
$filename of type void is incompatible with the type string expected by parameter $filename of file_put_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        file_put_contents(/** @scrutinizer ignore-type */ $filename, $data, $flags);
Loading history...
28
        
29
        // Handle stream resource.
30 4
        if (is_resource($data)) {
31 4
            fclose($data); 
32
        }
33
34 4
        return new File($filename);
0 ignored issues
show
Bug introduced by
$filename of type void is incompatible with the type string expected by parameter $path of Illuminate\Http\File::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return new File(/** @scrutinizer ignore-type */ $filename);
Loading history...
Bug Best Practice introduced by
The expression return new Illuminate\Http\File($filename) returns the type Illuminate\Http\File which is incompatible with the documented return type void.
Loading history...
35
    }
36
37
    /**
38
     * Return the extension from filename.
39
     *
40
     * @param  string  $filename
41
     * @return string
42
     */
43 5
    public static function extension(string $filename)
44
    {
45 5
        return pathinfo($filename, PATHINFO_EXTENSION);
46
    }
47
48
    /**
49
     * Return the file name from path.
50
     *
51
     * @param  string  $path
52
     * @return string
53
     */
54 5
    public static function fileName(string $path)
55
    {
56 5
        return pathinfo($path, PATHINFO_BASENAME);
57
    }
58
59
    /**
60
     * Return the configured temporary directory. You may pass
61
     * a file name to build some custom path.
62
     *
63
     * @param  string|null  $fileName
64
     * @return void
65
     */
66 9
    public static function getTemporaryDirectory(string $fileName = null)
67
    {
68 9
        $configuredTemp = config('lincable.temp_directory');
69
70 9
        $directory = str_finish(
71 9
            $configuredTemp === null 
72 9
                ? sys_get_temp_dir() 
73 9
                : (string) $configuredTemp, 
74 9
            DIRECTORY_SEPARATOR
75
        );
76
    
77 9
        if ($fileName) {
78 4
            return $directory.$fileName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $directory . $fileName returns the type string which is incompatible with the documented return type void.
Loading history...
79
        }
80
81 5
        return $directory;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $directory returns the type string which is incompatible with the documented return type void.
Loading history...
82
    }
83
}