Skip to content

Commit 6494ddb

Browse files
authored
Merge pull request #4690 from BackEndTea/fix-phpstan-issues
Fix phpstan issues by making Escher generic
2 parents 09fab17 + 1adad75 commit 6494ddb

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@ parameters:
66
count: 1
77
path: src/PhpSpreadsheet/Calculation/LookupRef/Sort.php
88

9-
-
10-
message: '#^Cannot call method getAllSpContainers\(\) on mixed\.$#'
11-
identifier: method.nonObject
12-
count: 1
13-
path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
14-
15-
-
16-
message: '#^Cannot call method getBSECollection\(\) on mixed\.$#'
17-
identifier: method.nonObject
18-
count: 1
19-
path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
20-
21-
-
22-
message: '#^Cannot call method getBstoreContainer\(\) on mixed\.$#'
23-
identifier: method.nonObject
24-
count: 1
25-
path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
26-
27-
-
28-
message: '#^Cannot call method getSpgrContainer\(\) on mixed\.$#'
29-
identifier: method.nonObject
30-
count: 1
31-
path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
32-
339
-
3410
message: '#^Cannot access offset 0 on mixed\.$#'
3511
identifier: offsetAccess.nonOffsetAccessible

src/PhpSpreadsheet/Reader/Xls/Escher.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE;
1313
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE\Blip;
1414

15+
/**
16+
* @template T of BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer
17+
*/
1518
class Escher
1619
{
1720
const DGGCONTAINER = 0xF000;
@@ -50,11 +53,15 @@ class Escher
5053

5154
/**
5255
* The object to be returned by the reader. Modified during load.
56+
*
57+
* @var T
5358
*/
5459
private BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer $object;
5560

5661
/**
5762
* Create a new Escher instance.
63+
*
64+
* @param T $object
5865
*/
5966
public function __construct(BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer $object)
6067
{
@@ -84,6 +91,8 @@ public function __construct(BSE|BstoreContainer|DgContainer|DggContainer|\PhpOff
8491

8592
/**
8693
* Load Escher stream data. May be a partial Escher stream.
94+
*
95+
* @return T
8796
*/
8897
public function load(string $data): BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer
8998
{

src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads
435435

436436
// get all spContainers in one long array, so they can be mapped to OBJ records
437437
/** @var SpContainer[] $allSpContainers */
438-
$allSpContainers = method_exists($escherWorksheet, 'getDgContainer') ? $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers() : [];
438+
$allSpContainers = $escherWorksheet->getDgContainerOrThrow()->getSpgrContainerOrThrow()->getAllSpContainers();
439439
}
440440

441441
// treat OBJ records
@@ -497,7 +497,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads
497497

498498
if ($escherWorkbook) {
499499
/** @var BSE[] */
500-
$BSECollection = method_exists($escherWorkbook, 'getDggContainer') ? $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection() : [];
500+
$BSECollection = $escherWorkbook->getDggContainerOrThrow()->getBstoreContainerOrThrow()->getBSECollection();
501501
$BSE = $BSECollection[$BSEindex - 1];
502502
$blipType = $BSE->getBlipType();
503503

src/PhpSpreadsheet/Shared/Escher.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Shared;
44

5+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
6+
57
class Escher
68
{
79
/**
@@ -22,6 +24,14 @@ public function getDggContainer(): ?Escher\DggContainer
2224
return $this->dggContainer;
2325
}
2426

27+
/**
28+
* Get Drawing Group Container.
29+
*/
30+
public function getDggContainerOrThrow(): Escher\DggContainer
31+
{
32+
return $this->dggContainer ?? throw new SpreadsheetException('dggContainer is unexpectedly null');
33+
}
34+
2535
/**
2636
* Set Drawing Group Container.
2737
*/
@@ -38,6 +48,14 @@ public function getDgContainer(): ?Escher\DgContainer
3848
return $this->dgContainer;
3949
}
4050

51+
/**
52+
* Get Drawing Container.
53+
*/
54+
public function getDgContainerOrThrow(): Escher\DgContainer
55+
{
56+
return $this->dgContainer ?? throw new SpreadsheetException('dgContainer is unexpectedly null');
57+
}
58+
4159
/**
4260
* Set Drawing Container.
4361
*/

src/PhpSpreadsheet/Shared/Escher/DggContainer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Shared\Escher;
44

5+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
6+
57
class DggContainer
68
{
79
/**
@@ -94,6 +96,14 @@ public function getBstoreContainer(): ?DggContainer\BstoreContainer
9496
return $this->bstoreContainer;
9597
}
9698

99+
/**
100+
* Get BLIP Store Container.
101+
*/
102+
public function getBstoreContainerOrThrow(): DggContainer\BstoreContainer
103+
{
104+
return $this->bstoreContainer ?? throw new SpreadsheetException('bstoreContainer is unexpectedly null');
105+
}
106+
97107
/**
98108
* Set BLIP Store Container.
99109
*/

0 commit comments

Comments
 (0)