Completed
Push — master ( 33eefe...de01e5 )
by Mark
31s queued 27s
created

PPS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE;
4
5
// vim: set expandtab tabstop=4 shiftwidth=4:
6
// +----------------------------------------------------------------------+
7
// | PHP Version 4                                                        |
8
// +----------------------------------------------------------------------+
9
// | Copyright (c) 1997-2002 The PHP Group                                |
10
// +----------------------------------------------------------------------+
11
// | This source file is subject to version 2.02 of the PHP license,      |
12
// | that is bundled with this package in the file LICENSE, and is        |
13
// | available at through the world-wide-web at                           |
14
// | http://www.php.net/license/2_02.txt.                                 |
15
// | If you did not receive a copy of the PHP license and are unable to   |
16
// | obtain it through the world-wide-web, please send a note to          |
17
// | [email protected] so we can mail you a copy immediately.               |
18
// +----------------------------------------------------------------------+
19
// | Author: Xavier Noguer <[email protected]>                              |
20
// | Based on OLE::Storage_Lite by Kawai, Takanori                        |
21
// +----------------------------------------------------------------------+
22
//
23
use PhpOffice\PhpSpreadsheet\Shared\OLE;
24
25
/**
26
 * Class for creating PPS's for OLE containers.
27
 *
28
 * @author   Xavier Noguer <[email protected]>
29
 */
30
class PPS
31
{
32
    /**
33
     * The PPS index.
34
     *
35
     * @var int
36
     */
37
    public $No;
38
39
    /**
40
     * The PPS name (in Unicode).
41
     *
42
     * @var string
43
     */
44
    public $Name;
45
46
    /**
47
     * The PPS type. Dir, Root or File.
48
     *
49
     * @var int
50
     */
51
    public $Type;
52
53
    /**
54
     * The index of the previous PPS.
55
     *
56
     * @var int
57
     */
58
    public $PrevPps;
59
60
    /**
61
     * The index of the next PPS.
62
     *
63
     * @var int
64
     */
65
    public $NextPps;
66
67
    /**
68
     * The index of it's first child if this is a Dir or Root PPS.
69
     *
70
     * @var int
71
     */
72
    public $DirPps;
73
74
    /**
75
     * A timestamp.
76
     *
77
     * @var float|int
78
     */
79
    public $Time1st;
80
81
    /**
82
     * A timestamp.
83
     *
84
     * @var float|int
85
     */
86
    public $Time2nd;
87
88
    /**
89
     * Starting block (small or big) for this PPS's data  inside the container.
90
     *
91
     * @var ?int
92
     */
93
    public $startBlock;
94
95
    /**
96
     * The size of the PPS's data (in bytes).
97
     *
98
     * @var int
99
     */
100
    public $Size;
101
102
    /**
103
     * The PPS's data (only used if it's not using a temporary file).
104
     *
105
     * @var string
106
     */
107
    public $_data = '';
108
109
    /**
110
     * Array of child PPS's (only used by Root and Dir PPS's).
111
     *
112
     * @var array
113
     */
114
    public $children = [];
115
116
    /**
117
     * Pointer to OLE container.
118
     *
119
     * @var OLE
120
     */
121
    public $ole;
122
123
    /**
124
     * The constructor.
125
     *
126
     * @param ?int $No The PPS index
127
     * @param ?string $name The PPS name
128
     * @param ?int $type The PPS type. Dir, Root or File
129
     * @param ?int $prev The index of the previous PPS
130
     * @param ?int $next The index of the next PPS
131
     * @param ?int $dir The index of it's first child if this is a Dir or Root PPS
132
     * @param null|float|int $time_1st A timestamp
133
     * @param null|float|int $time_2nd A timestamp
134
     * @param ?string $data The (usually binary) source data of the PPS
135
     * @param array $children Array containing children PPS for this PPS
136
     */
137 90
    public function __construct($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children)
138
    {
139 90
        $this->No = (int) $No;
140 90
        $this->Name = (string) $name;
141 90
        $this->Type = (int) $type;
142 90
        $this->PrevPps = (int) $prev;
143 90
        $this->NextPps = (int) $next;
144 90
        $this->DirPps = (int) $dir;
145 90
        $this->Time1st = $time_1st ?? 0;
146 90
        $this->Time2nd = $time_2nd ?? 0;
147 90
        $this->_data = (string) $data;
148 90
        $this->children = $children;
149 90
        $this->Size = strlen((string) $data);
150
    }
151
152
    /**
153
     * Returns the amount of data saved for this PPS.
154
     *
155
     * @return int The amount of data (in bytes)
156
     */
157 89
    public function getDataLen()
158
    {
159
        //if (!isset($this->_data)) {
160
        //    return 0;
161
        //}
162
163 89
        return strlen($this->_data);
164
    }
165
166
    /**
167
     * Returns a string with the PPS's WK (What is a WK?).
168
     *
169
     * @return string The binary string
170
     */
171 89
    public function getPpsWk()
172
    {
173 89
        $ret = str_pad($this->Name, 64, "\x00");
174
175 89
        $ret .= pack('v', strlen($this->Name) + 2)  // 66
176 89
            . pack('c', $this->Type)              // 67
177 89
            . pack('c', 0x00) //UK                // 68
178 89
            . pack('V', $this->PrevPps) //Prev    // 72
179 89
            . pack('V', $this->NextPps) //Next    // 76
180 89
            . pack('V', $this->DirPps)  //Dir     // 80
181 89
            . "\x00\x09\x02\x00"                  // 84
182 89
            . "\x00\x00\x00\x00"                  // 88
183 89
            . "\xc0\x00\x00\x00"                  // 92
184 89
            . "\x00\x00\x00\x46"                  // 96 // Seems to be ok only for Root
185 89
            . "\x00\x00\x00\x00"                  // 100
186 89
            . OLE::localDateToOLE($this->Time1st)          // 108
187 89
            . OLE::localDateToOLE($this->Time2nd)          // 116
188 89
            . pack('V', $this->startBlock ?? 0)  // 120
189 89
            . pack('V', $this->Size)               // 124
190 89
            . pack('V', 0); // 128
191
192 89
        return $ret;
193
    }
194
195
    /**
196
     * Updates index and pointers to previous, next and children PPS's for this
197
     * PPS. I don't think it'll work with Dir PPS's.
198
     *
199
     * @param array $raList Reference to the array of PPS's for the whole OLE
200
     *                          container
201
     * @param mixed $to_save
202
     * @param mixed $depth
203
     *
204
     * @return int The index for this PPS
205
     */
206 89
    public static function savePpsSetPnt(&$raList, $to_save, $depth = 0)
207
    {
208 89
        if (!is_array($to_save) || (empty($to_save))) {
209 89
            return 0xFFFFFFFF;
210 89
        } elseif (count($to_save) == 1) {
211 89
            $cnt = count($raList);
212
            // If the first entry, it's the root... Don't clone it!
213 89
            $raList[$cnt] = ($depth == 0) ? $to_save[0] : clone $to_save[0];
214 89
            $raList[$cnt]->No = $cnt;
215 89
            $raList[$cnt]->PrevPps = 0xFFFFFFFF;
216 89
            $raList[$cnt]->NextPps = 0xFFFFFFFF;
217 89
            $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++);
218
        } else {
219 89
            $iPos = (int) floor(count($to_save) / 2);
220 89
            $aPrev = array_slice($to_save, 0, $iPos);
221 89
            $aNext = array_slice($to_save, $iPos + 1);
222 89
            $cnt = count($raList);
223
            // If the first entry, it's the root... Don't clone it!
224 89
            $raList[$cnt] = ($depth == 0) ? $to_save[$iPos] : clone $to_save[$iPos];
225 89
            $raList[$cnt]->No = $cnt;
226 89
            $raList[$cnt]->PrevPps = self::savePpsSetPnt($raList, $aPrev, $depth++);
227 89
            $raList[$cnt]->NextPps = self::savePpsSetPnt($raList, $aNext, $depth++);
228 89
            $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++);
229
        }
230
231 89
        return $cnt;
232
    }
233
}
234