diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 29bdc49fa0..b267f8c3b3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6,30 +6,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Calculation/LookupRef/Sort.php - - - message: '#^Cannot call method getAllSpContainers\(\) on mixed\.$#' - identifier: method.nonObject - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php - - - - message: '#^Cannot call method getBSECollection\(\) on mixed\.$#' - identifier: method.nonObject - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php - - - - message: '#^Cannot call method getBstoreContainer\(\) on mixed\.$#' - identifier: method.nonObject - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php - - - - message: '#^Cannot call method getSpgrContainer\(\) on mixed\.$#' - identifier: method.nonObject - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php - - message: '#^Cannot access offset 0 on mixed\.$#' identifier: offsetAccess.nonOffsetAccessible diff --git a/src/PhpSpreadsheet/Reader/Xls/Escher.php b/src/PhpSpreadsheet/Reader/Xls/Escher.php index 094c19e877..018c17189e 100644 --- a/src/PhpSpreadsheet/Reader/Xls/Escher.php +++ b/src/PhpSpreadsheet/Reader/Xls/Escher.php @@ -12,6 +12,9 @@ use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE; use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE\Blip; +/** + * @template T of BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer + */ class Escher { const DGGCONTAINER = 0xF000; @@ -50,11 +53,15 @@ class Escher /** * The object to be returned by the reader. Modified during load. + * + * @var T */ private BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer $object; /** * Create a new Escher instance. + * + * @param T $object */ public function __construct(BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer $object) { @@ -84,6 +91,8 @@ public function __construct(BSE|BstoreContainer|DgContainer|DggContainer|\PhpOff /** * Load Escher stream data. May be a partial Escher stream. + * + * @return T */ public function load(string $data): BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer { diff --git a/src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php b/src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php index 76e4235b8e..5c27b0cc3d 100644 --- a/src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php +++ b/src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php @@ -435,7 +435,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads // get all spContainers in one long array, so they can be mapped to OBJ records /** @var SpContainer[] $allSpContainers */ - $allSpContainers = method_exists($escherWorksheet, 'getDgContainer') ? $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers() : []; + $allSpContainers = $escherWorksheet->getDgContainerOrThrow()->getSpgrContainerOrThrow()->getAllSpContainers(); } // treat OBJ records @@ -497,7 +497,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads if ($escherWorkbook) { /** @var BSE[] */ - $BSECollection = method_exists($escherWorkbook, 'getDggContainer') ? $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection() : []; + $BSECollection = $escherWorkbook->getDggContainerOrThrow()->getBstoreContainerOrThrow()->getBSECollection(); $BSE = $BSECollection[$BSEindex - 1]; $blipType = $BSE->getBlipType(); diff --git a/src/PhpSpreadsheet/Shared/Escher.php b/src/PhpSpreadsheet/Shared/Escher.php index 9eb9956a2f..00180f6e25 100644 --- a/src/PhpSpreadsheet/Shared/Escher.php +++ b/src/PhpSpreadsheet/Shared/Escher.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheet\Shared; +use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; + class Escher { /** @@ -22,6 +24,14 @@ public function getDggContainer(): ?Escher\DggContainer return $this->dggContainer; } + /** + * Get Drawing Group Container. + */ + public function getDggContainerOrThrow(): Escher\DggContainer + { + return $this->dggContainer ?? throw new SpreadsheetException('dggContainer is unexpectedly null'); + } + /** * Set Drawing Group Container. */ @@ -38,6 +48,14 @@ public function getDgContainer(): ?Escher\DgContainer return $this->dgContainer; } + /** + * Get Drawing Container. + */ + public function getDgContainerOrThrow(): Escher\DgContainer + { + return $this->dgContainer ?? throw new SpreadsheetException('dgContainer is unexpectedly null'); + } + /** * Set Drawing Container. */ diff --git a/src/PhpSpreadsheet/Shared/Escher/DggContainer.php b/src/PhpSpreadsheet/Shared/Escher/DggContainer.php index eb8829c0cb..e5d831fa77 100644 --- a/src/PhpSpreadsheet/Shared/Escher/DggContainer.php +++ b/src/PhpSpreadsheet/Shared/Escher/DggContainer.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheet\Shared\Escher; +use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; + class DggContainer { /** @@ -94,6 +96,14 @@ public function getBstoreContainer(): ?DggContainer\BstoreContainer return $this->bstoreContainer; } + /** + * Get BLIP Store Container. + */ + public function getBstoreContainerOrThrow(): DggContainer\BstoreContainer + { + return $this->bstoreContainer ?? throw new SpreadsheetException('bstoreContainer is unexpectedly null'); + } + /** * Set BLIP Store Container. */