Passed
Push — master ( 2ddebf...84c74c )
by Petr
09:17 queued 01:35
created

XToLocalFile::getTempFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ChecksTests\TraitsTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mime\Check\Traits\TToLocalFile;
8
use kalanis\kw_mime\MimeException;
9
10
11
class ToLocalFileTest extends CommonTestClass
12
{
13
    /**
14
     * @throws MimeException
15
     */
16
    public function testNone1(): void
17
    {
18
        $lib = new XToLocalFile();
19
        $lib->initTempFile();
20
        @unlink($lib->getTempFile());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

20
        /** @scrutinizer ignore-unhandled */ @unlink($lib->getTempFile());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
21
        $this->expectException(MimeException::class);
22
        $lib->convert('file', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type resource|string expected by parameter $content of ChecksTests\TraitsTests\XToLocalFile::convert(). ( Ignorable by Annotation )

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

22
        $lib->convert('file', /** @scrutinizer ignore-type */ false);
Loading history...
23
    }
24
25
    /**
26
     * @throws MimeException
27
     */
28
    public function testNone2(): void
29
    {
30
        $lib = new XToLocalFile();
31
        $lib->initTempFile();
32
        @unlink($lib->getTempFile());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

32
        /** @scrutinizer ignore-unhandled */ @unlink($lib->getTempFile());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
        $this->expectException(MimeException::class);
34
        $lib->convert('file', null);
35
    }
36
37
    /**
38
     * @throws MimeException
39
     */
40
    public function testString(): void
41
    {
42
        $lib = new XToLocalFile();
43
        $lib->initTempFile();
44
        $pt1 = $lib->convert('data', 'test data');
45
        $this->assertEquals('test data', file_get_contents($pt1));
46
        @unlink($pt1);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-unhandled */ @unlink($pt1);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
47
        $lib->initTempFile();
48
        $pt2 = $lib->convert('nums', 123456);
49
        $this->assertEquals('123456', file_get_contents($pt2));
50
        @unlink($pt2);
51
    }
52
53
    /**
54
     * @throws MimeException
55
     */
56
    public function testResource(): void
57
    {
58
        $lib = new XToLocalFile();
59
        $res = @fopen('php://memory', 'r+');
60
        fwrite($res, 'okmijnuhb');
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $stream of fwrite() does only seem to accept resource, 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

60
        fwrite(/** @scrutinizer ignore-type */ $res, 'okmijnuhb');
Loading history...
61
        $lib->initTempFile();
62
        $pt1 = $lib->convert('res', $res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $content of ChecksTests\TraitsTests\XToLocalFile::convert() does only seem to accept resource|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

62
        $pt1 = $lib->convert('res', /** @scrutinizer ignore-type */ $res);
Loading history...
63
        $this->assertEquals('okmijnuhb', file_get_contents($pt1));
64
        @unlink($pt1);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

64
        /** @scrutinizer ignore-unhandled */ @unlink($pt1);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
    }
66
}
67
68
69
class XToLocalFile
70
{
71
    use TToLocalFile;
72
73
    /** @var string|null */
74
    protected $tempFile = null;
75
76
    /**
77
     * @param string $name
78
     * @param string|resource $content
79
     * @throws MimeException
80
     * @return string
81
     */
82
    public function convert(string $name, $content): string
83
    {
84
        $localPath = strval($this->getTempFile());
85
        $this->readSourceToLocalFile($name, $content, $localPath);
86
        return $localPath;
87
    }
88
89
    public function initTempFile(): void
90
    {
91
        // beware! it immediately creates that temporary file
92
        $this->tempFile = tempnam(sys_get_temp_dir(), 'MimeSrcTest');
93
    }
94
95
    public function getTempFile(): ?string
96
    {
97
        return $this->tempFile;
98
    }
99
}
100