Passed
Push — master ( 9e1378...d02904 )
by Mark
16:48 queued 06:34
created

XMLWriter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 14
eloc 26
c 0
b 0
f 0
dl 0
loc 96
ccs 24
cts 25
cp 0.96
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __wakeup() 0 5 1
A getData() 0 8 3
A __construct() 0 22 6
A __destruct() 0 7 2
A writeRawData() 0 7 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
6
7
class XMLWriter extends \XMLWriter
8
{
9
    /** @var bool */
10
    public static $debugEnabled = false;
11
12
    /** Temporary storage method */
13
    const STORAGE_MEMORY = 1;
14
    const STORAGE_DISK = 2;
15
16
    /**
17
     * Temporary filename.
18
     *
19
     * @var string
20
     */
21
    private $tempFileName = '';
22
23
    /**
24
     * Create a new XMLWriter instance.
25
     *
26
     * @param int $temporaryStorage Temporary storage location
27
     * @param string $temporaryStorageFolder Temporary storage folder
28
     */
29 333
    public function __construct($temporaryStorage = self::STORAGE_MEMORY, $temporaryStorageFolder = null)
30
    {
31
        // Open temporary storage
32 333
        if ($temporaryStorage == self::STORAGE_MEMORY) {
33 332
            $this->openMemory();
34
        } else {
35
            // Create temporary filename
36 1
            if ($temporaryStorageFolder === null) {
37 1
                $temporaryStorageFolder = File::sysGetTempDir();
38
            }
39 1
            $this->tempFileName = (string) @tempnam($temporaryStorageFolder, 'xml');
40
41
            // Open storage
42 1
            if (empty($this->tempFileName) || $this->openUri($this->tempFileName) === false) {
43
                // Fallback to memory...
44
                $this->openMemory();
45
            }
46
        }
47
48
        // Set default values
49 333
        if (self::$debugEnabled) {
50 1
            $this->setIndent(true);
51
        }
52
    }
53
54
    /**
55
     * Destructor.
56
     */
57 333
    public function __destruct()
58
    {
59
        // Unlink temporary files
60
        // There is nothing reasonable to do if unlink fails.
61 333
        if ($this->tempFileName != '') {
62
            /** @scrutinizer ignore-unhandled */
63 1
            @unlink($this->tempFileName);
64
        }
65
    }
66
67 1
    public function __wakeup(): void
68
    {
69 1
        $this->tempFileName = '';
70
71 1
        throw new SpreadsheetException('Unserialize not permitted');
72
    }
73
74
    /**
75
     * Get written data.
76
     *
77
     * @return string
78
     */
79 333
    public function getData()
80
    {
81 333
        if ($this->tempFileName == '') {
82 332
            return $this->outputMemory(true);
83
        }
84 1
        $this->flush();
85
86 1
        return file_get_contents($this->tempFileName) ?: '';
87
    }
88
89
    /**
90
     * Wrapper method for writeRaw.
91
     *
92
     * @param null|string|string[] $rawTextData
93
     *
94
     * @return bool
95
     */
96 277
    public function writeRawData($rawTextData)
97
    {
98 277
        if (is_array($rawTextData)) {
99 2
            $rawTextData = implode("\n", $rawTextData);
100
        }
101
102 277
        return $this->writeRaw(htmlspecialchars($rawTextData ?? ''));
103
    }
104
}
105