Failed Conditions
Pull Request — master (#4165)
by Owen
15:55
created

StreamTest::testAllWritersCanWriteToStream()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 8.6666
c 0
b 0
f 0
cc 7
nc 10
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Functional;
6
7
use PhpOffice\PhpSpreadsheet\IOFactory;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Not clear that Dompdf will be Php8.4 compatible in time.
13
 * Run in separate process and add version test till it is ready.
14
 *
15
 * @runTestsInSeparateProcesses
16
 */
17
class StreamTest extends TestCase
18
{
19
    public static function providerFormats(): array
20
    {
21
        $providerFormats = [
22
            ['Xls'],
23
            ['Xlsx'],
24
            ['Ods'],
25
            ['Csv'],
26
            ['Html'],
27
            ['Mpdf'],
28
            ['Dompdf'],
29
            ['Tcpdf'],
30
        ];
31
32
        return $providerFormats;
33
    }
34
35
    /**
36
     * @dataProvider providerFormats
37
     */
38
    public function testAllWritersCanWriteToStream(string $format): void
39
    {
40
        $spreadsheet = new Spreadsheet();
41
        $spreadsheet->getActiveSheet()->setCellValue('A1', 'foo');
42
        $writer = IOFactory::createWriter($spreadsheet, $format);
43
44
        $stream = fopen('php://memory', 'wb+');
45
        $stat = ($stream === false) ? false : fstat($stream);
46
        if ($stream === false || $stat === false) {
47
            self::fail('fopen or fstat failed');
48
        } else {
49
            self::assertSame(0, $stat['size']);
50
51
            if ($format === 'Dompdf' && PHP_VERSION_ID >= 80400) {
52
                @$writer->save($stream);
2 ignored issues
show
Bug introduced by
Are you sure the usage of $writer->save($stream) targeting PhpOffice\PhpSpreadsheet\Writer\IWriter::save() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for save(). 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

52
                /** @scrutinizer ignore-unhandled */ @$writer->save($stream);

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...
53
            } else {
54
                $writer->save($stream);
55
            }
56
57
            self::assertIsResource($stream, 'should not close the stream for further usage out of PhpSpreadsheet');
58
            $stat = fstat($stream);
59
            if ($stat === false) {
60
                self::fail('fstat failed');
61
            } else {
62
                self::assertGreaterThan(0, $stat['size'], 'something should have been written to the stream');
63
            }
64
            self::assertGreaterThan(0, ftell($stream), 'should not be rewinded, because not all streams support it');
65
        }
66
    }
67
}
68