Files
oleibman a3f3d2c9ad Clean Up Documentation for Worksheet (#3281)
* Clean Up Documentation for Worksheet

This PR was intended to clean up Phpstan/Scrutinizer messages regarding Worksheet. It is, for the most part, straightforward, but there is one problem which complicates things. The frequently-called public method `getParent` returns `Worksheet` or `null` but is documented to return only `Worksheet`. This can be addressed in either of two ways - change the code to match the documentation (smaller number of changes but a backwards compatibility break), or change the documentation to match the code (larger number of changes but no compatibility break). I have prepared a PR for each approach, but avoiding a compatibility break seems better, so I am pushing the latter. I can switch to the other if preferred. Most existing internal calls to `getParent` are changed to use a new method `getParentOrThrow`, which will throw an exception if parent is null. These calls would all have thrown a null pointer exception anyhow in that situation, so this should not cause any new breaks.

* Scrutinizer

One false positive, and one message leading to a minor code improvement.
2023-01-11 23:34:52 -08:00

34 lines
976 B
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Exception as SpException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class WorksheetParentTest extends TestCase
{
public function testNormal(): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
self::assertSame($spreadsheet, $worksheet->getParent());
self::assertSame($spreadsheet, $worksheet->getParentOrThrow());
}
public function testGetParent(): void
{
$worksheet = new Worksheet();
self::assertNull($worksheet->getParent());
}
public function testGetParentOrThrow(): void
{
$this->expectException(SpException::class);
$this->expectExceptionMessage('Sheet does not have a parent');
$worksheet = new Worksheet();
$worksheet->getParentOrThrow();
}
}