Phpstan and Xlsx Reader (#3043)

Eliminate most Phpstan messages in Xlsx Reader. In combination with similar changes to Xlsx Writer, baseline will shrink to just over 3,000 lines.
This commit is contained in:
oleibman 2022-09-04 10:43:31 -07:00 committed by GitHub
parent 57a72037b5
commit adbda63912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 146 additions and 131 deletions

View File

@ -1,6 +1,6 @@
<?php
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@ -121,12 +121,12 @@ $dataSeriesValues[2] // triangle border
$dataSeriesValues[2]->setScatterLines(false); // points not connected
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
$xAxis = new Axis();
$xAxis = new ChartAxis();
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
$xAxis->setAxisOption('textRotation', '45');
$yAxis = new Axis();
$yAxis = new ChartAxis();
$yAxis->setLineStyleProperties(
2.5, // width in points
Properties::LINE_STYLE_COMPOUND_SIMPLE,

View File

@ -1,6 +1,6 @@
<?php
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@ -121,13 +121,13 @@ $dataSeriesValues[2] // triangle border
$dataSeriesValues[2]->setScatterLines(false); // points not connected
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
$xAxis = new Axis();
$xAxis = new ChartAxis();
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
$xAxis->setAxisOption('textRotation', '45');
$xAxis->setAxisOption('hidden', '1');
$yAxis = new Axis();
$yAxis = new ChartAxis();
$yAxis->setLineStyleProperties(
2.5, // width in points
Properties::LINE_STYLE_COMPOUND_SIMPLE,

View File

@ -1,6 +1,6 @@
<?php
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@ -77,10 +77,10 @@ $dataSeriesValues = [
// marker details
$dataSeriesValues[0]
->getMarkerFillColor()
->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
$dataSeriesValues[0]
->getMarkerBorderColor()
->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
// line details - dashed, smooth line (Bezier) with arrows, 40% transparent
$dataSeriesValues[0]
@ -105,24 +105,24 @@ $dataSeriesValues[1] // square marker border color
->setColorProperties('accent6', 3, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
$dataSeriesValues[1] // square marker fill color
->getMarkerFillColor()
->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
$dataSeriesValues[1]
->setScatterLines(true)
->setSmoothLine(false)
->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_RGB);
$dataSeriesValues[1]->setLineWidth(2.0);
// series 3 - metric3, markers, no line
$dataSeriesValues[2] // triangle? fill
//->setPointMarker('triangle') // let Excel choose shape, which is predicted to be a triangle
->getMarkerFillColor()
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
$dataSeriesValues[2] // triangle border
->getMarkerBorderColor()
->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
$dataSeriesValues[2]->setScatterLines(false); // points not connected
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
$xAxis = new Axis();
$xAxis = new ChartAxis();
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
// Build the dataseries
@ -204,7 +204,7 @@ $dataSeriesValues[0]->setTrendLines($trendLines);
$dataSeriesValues[0]->setScatterLines(false); // points not connected
$dataSeriesValues[0]->getMarkerFillColor()
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
$dataSeriesValues[0]->getMarkerBorderColor()
->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
@ -218,7 +218,7 @@ $dataSeriesValues[0]->getTrendLines()[1]->setLineStyleProperties(1.25);
$dataSeriesValues[0]->getTrendLines()[2]->getLineColor()->setColorProperties('accent2', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
$dataSeriesValues[0]->getTrendLines()[2]->setLineStyleProperties(1.5, null, null, null, null, null, null, Properties::LINE_STYLE_ARROW_TYPE_OPEN, 8);
$xAxis = new Axis();
$xAxis = new ChartAxis();
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601); // m/d/yyyy
// Build the dataseries

View File

@ -324,13 +324,13 @@ class DataSeriesValues extends Properties
if (is_array($this->fillColor)) {
$array = [];
foreach ($this->fillColor as $chartColor) {
$array[] = self::chartColorToString($chartColor);
$array[] = $this->chartColorToString($chartColor);
}
return $array;
}
return self::chartColorToString($this->fillColor);
return $this->chartColorToString($this->fillColor);
}
/**
@ -348,13 +348,13 @@ class DataSeriesValues extends Properties
if ($fillString instanceof ChartColor) {
$this->fillColor[] = $fillString;
} else {
$this->fillColor[] = self::stringToChartColor($fillString);
$this->fillColor[] = $this->stringToChartColor($fillString);
}
}
} elseif ($color instanceof ChartColor) {
$this->fillColor = $color;
} elseif (is_string($color)) {
$this->fillColor = self::stringToChartColor($color);
} else {
$this->fillColor = $this->stringToChartColor($color);
}
return $this;

View File

@ -311,7 +311,7 @@ class Chart
break;
case 'stockChart':
$plotSeries[] = $this->chartDataSeries($chartDetail, $chartDetailKey);
$plotAttributes = $this->readChartAttributes($plotAreaLayout);
$plotAttributes = $this->readChartAttributes($chartDetail);
break;
}
@ -1068,7 +1068,7 @@ class Chart
}
/**
* @param null|Layout|SimpleXMLElement $chartDetail
* @param ?SimpleXMLElement $chartDetail
*/
private function readChartAttributes($chartDetail): array
{

View File

@ -349,7 +349,7 @@ class Font
// Special case if there are one or more newline characters ("\n")
$cellText = $cellText ?? '';
if (strpos($cellText, "\n") !== false) {
if (strpos(/** @scrutinizer ignore-type */ $cellText, "\n") !== false) {
$lineTexts = explode("\n", $cellText);
$lineWidths = [];
foreach ($lineTexts as $lineText) {

View File

@ -337,9 +337,19 @@ class StringHelper
mb_substitute_character(65533); // Unicode substitution character
// Phpstan does not think this can return false.
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
mb_substitute_character($subst);
mb_substitute_character(/** @scrutinizer ignore-type */ $subst);
return $returnValue;
return self::returnString($returnValue);
}
/**
* Strictly to satisfy Scrutinizer.
*
* @param mixed $value
*/
private static function returnString($value): string
{
return is_string($value) ? $value : '';
}
/**
@ -433,7 +443,7 @@ class StringHelper
}
}
return mb_convert_encoding($textValue, $to, $from);
return self::returnString(mb_convert_encoding($textValue, $to, $from));
}
/**

View File

@ -743,14 +743,14 @@ class Font extends Supervisor
private function hashChartColor(?ChartColor $underlineColor): string
{
if ($this->underlineColor === null) {
if ($underlineColor === null) {
return '';
}
return
$this->underlineColor->getValue()
. $this->underlineColor->getType()
. (string) $this->underlineColor->getAlpha();
$underlineColor->getValue()
. $underlineColor->getType()
. (string) $underlineColor->getAlpha();
}
/**

View File

@ -33,9 +33,6 @@ class Dompdf extends Pdf
{
$fileHandle = parent::prepareForSave($filename);
// Default PDF paper size
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
// Check for paper size and page orientation
$setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
$orientation = $this->getOrientation() ?? $setup->getOrientation();

View File

@ -233,8 +233,6 @@ class Chart extends WriterPart
if ($plotArea === null) {
return;
}
$majorGridlines = ($yAxis === null) ? null : $yAxis->getMajorGridlines();
$minorGridlines = ($yAxis === null) ? null : $yAxis->getMinorGridlines();
$id1 = $id2 = $id3 = '0';
$this->seriesIndex = 0;
@ -1163,7 +1161,6 @@ class Chart extends WriterPart
$intercept = $trendLine->getIntercept();
$name = $trendLine->getName();
$trendLineColor = $trendLine->getLineColor(); // ChartColor
$trendLineWidth = $trendLine->getLineStyleProperty('width');
$objWriter->startElement('c:trendline'); // N.B. lowercase 'ell'
if ($name !== '') {

View File

@ -12,7 +12,7 @@ class FormulaParserTest extends TestCase
{
$this->expectException(CalcException::class);
$this->expectExceptionMessage('Invalid parameter passed: formula');
$result = new FormulaParser(null);
new FormulaParser(null);
}
public function testInvalidTokenId(): void

View File

@ -3,11 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -107,7 +107,7 @@ class AxisGlowTest extends AbstractFunctional
$yAxis = $chart->getChartAxisY();
$xAxis = $chart->getChartAxisX();
$yGlowSize = 10.0;
$yAxis->setGlowProperties($yGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB);
$yAxis->setGlowProperties($yGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
$expectedGlowColor = [
'type' => 'srgbClr',
'value' => 'FFFF00',
@ -231,7 +231,7 @@ class AxisGlowTest extends AbstractFunctional
);
$yAxis = $chart->getChartAxisX(); // deliberate
$yGlowSize = 20.0;
$yAxis->setGlowProperties($yGlowSize, 'accent1', 20, Properties::EXCEL_COLOR_TYPE_SCHEME);
$yAxis->setGlowProperties($yGlowSize, 'accent1', 20, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
$expectedGlowColor = [
'type' => 'schemeClr',
'value' => 'accent1',

View File

@ -3,11 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -113,7 +113,7 @@ class AxisShadowTest extends AbstractFunctional
'distance' => 3,
'rotWithShape' => 0,
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],
@ -139,7 +139,7 @@ class AxisShadowTest extends AbstractFunctional
'ky' => null,
],
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_ARGB,
'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
'value' => 'FF0000',
'alpha' => 20,
],

View File

@ -4,12 +4,12 @@ namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -98,7 +98,7 @@ class GridlinesShadowGlowTest extends AbstractFunctional
$majorGridlines = new GridLines();
$yAxis->setMajorGridlines($majorGridlines);
$majorGlowSize = 10.0;
$majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB);
$majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
$softEdgeSize = 2.5;
$majorGridlines->setSoftEdges($softEdgeSize);
$expectedGlowColor = [
@ -122,7 +122,7 @@ class GridlinesShadowGlowTest extends AbstractFunctional
'distance' => 3,
'rotWithShape' => 0,
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],

View File

@ -23,7 +23,7 @@ class Issue2506Test extends AbstractFunctional
public function testDataSeriesValues(): void
{
$reader = new XlsxReader();
self::readCharts($reader);
$this->readCharts($reader);
$spreadsheet = $reader->load(self::DIRECTORY . 'issue.2506.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$charts = $worksheet->getChartCollection();

View File

@ -3,11 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -109,7 +109,7 @@ class MultiplierTest extends TestCase
'ky' => null,
],
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_ARGB,
'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
'value' => 'FF0000',
'alpha' => 20,
],

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PHPUnit\Framework\TestCase;
@ -131,7 +132,7 @@ class ShadowPresetsTest extends TestCase
'presets' => Properties::SHADOW_PRESETS_NOSHADOW,
'effect' => null,
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],
@ -160,7 +161,7 @@ class ShadowPresetsTest extends TestCase
'presets' => Properties::SHADOW_PRESETS_NOSHADOW,
'effect' => null,
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],
@ -189,7 +190,7 @@ class ShadowPresetsTest extends TestCase
'presets' => Properties::SHADOW_PRESETS_NOSHADOW,
'effect' => null,
'color' => [
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],

View File

@ -11,12 +11,14 @@ class AutoFilter2Test extends TestCase
{
private const TESTBOOK = 'tests/data/Reader/XLSX/autofilter2.xlsx';
public function getVisibleSheet(Worksheet $sheet, int $maxRow): array
public function getVisibleSheet(?Worksheet $sheet, int $maxRow): array
{
$actualVisible = [];
for ($row = 2; $row <= $maxRow; ++$row) {
if ($sheet->getRowDimension($row)->getVisible()) {
$actualVisible[] = $row;
if ($sheet !== null) {
for ($row = 2; $row <= $maxRow; ++$row) {
if ($sheet->getRowDimension($row)->getVisible()) {
$actualVisible[] = $row;
}
}
}
@ -35,13 +37,14 @@ class AutoFilter2Test extends TestCase
self::assertCount(1, $columns);
$column = $columns['A'] ?? null;
self::assertNotNull($column);
/** @scrutinizer ignore-call */
$ruleset = $column->getRules();
self::assertCount(1, $ruleset);
$rule = $ruleset[0];
self::assertSame(Rule::AUTOFILTER_RULETYPE_DATEGROUP, $rule->getRuleType());
$value = $rule->getValue();
self::assertIsArray($value);
self::assertCount(6, $value);
self::assertCount(6, /** @scrutinizer ignore-type */ $value);
self::assertSame('2002', $value['year']);
self::assertSame('', $value['month']);
self::assertSame('', $value['day']);

View File

@ -24,14 +24,14 @@ class RibbonTest extends AbstractFunctional
self::assertSame('customUI/customUI.xml', $target);
$data = $spreadsheet->getRibbonXMLData('data');
self::assertIsString($data);
self::assertSame(1522, strlen($data));
self::assertSame(1522, strlen(/** @scrutinizer ignore-type */ $data));
$vbaCode = (string) $spreadsheet->getMacrosCode();
self::assertSame(13312, strlen($vbaCode));
self::assertNull($spreadsheet->getRibbonBinObjects());
self::assertNull($spreadsheet->getRibbonBinObjects('names'));
self::assertNull($spreadsheet->getRibbonBinObjects('data'));
foreach (['names', 'data', 'xxxxx'] as $type) {
self::assertNull($spreadsheet->getRibbonBinObjects($type), "Expecting null when type is $type");
}
self::assertEmpty($spreadsheet->getRibbonBinObjects('types'));
self::assertNull($spreadsheet->getRibbonBinObjects('xxxxx'));
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
@ -58,7 +58,7 @@ class RibbonTest extends AbstractFunctional
self::assertSame('customUI/customUI.xml', $target);
$data = $spreadsheet->getRibbonXMLData('data');
self::assertIsString($data);
self::assertSame(1522, strlen($data));
self::assertSame(1522, strlen(/** @scrutinizer ignore-type */ $data));
$vbaCode = (string) $spreadsheet->getMacrosCode();
self::assertSame(13312, strlen($vbaCode));
$spreadsheet->discardMacros();

View File

@ -28,7 +28,9 @@ class RichTextTest extends TestCase
public function testTextElements(): void
{
$element1 = new TextElement('A');
self::assertNull($element1->getFont());
if ($element1->getFont() !== null) {
self::fail('Expected font to be null');
}
$element2 = new TextElement('B');
$element3 = new TextElement('C');
$richText = new RichText();

View File

@ -9,81 +9,93 @@ use PHPUnit\Framework\TestCase;
class SpreadsheetCoverageTest extends TestCase
{
/** @var ?Spreadsheet */
private $spreadsheet;
/** @var ?Spreadsheet */
private $spreadsheet2;
protected function tearDown(): void
{
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
if ($this->spreadsheet2 !== null) {
$this->spreadsheet2->disconnectWorksheets();
$this->spreadsheet2 = null;
}
}
public function testDocumentProperties(): void
{
$spreadsheet = new Spreadsheet();
$properties = $spreadsheet->getProperties();
$this->spreadsheet = new Spreadsheet();
$properties = $this->spreadsheet->getProperties();
$properties->setCreator('Anyone');
$properties->setTitle('Description');
$spreadsheet2 = new Spreadsheet();
self::assertNotEquals($properties, $spreadsheet2->getProperties());
$this->spreadsheet2 = new Spreadsheet();
self::assertNotEquals($properties, $this->spreadsheet2->getProperties());
$properties2 = clone $properties;
$spreadsheet2->setProperties($properties2);
self::assertEquals($properties, $spreadsheet2->getProperties());
$spreadsheet->disconnectWorksheets();
$spreadsheet2->disconnectWorksheets();
$this->spreadsheet2->setProperties($properties2);
self::assertEquals($properties, $this->spreadsheet2->getProperties());
}
public function testDocumentSecurity(): void
{
$spreadsheet = new Spreadsheet();
$security = $spreadsheet->getSecurity();
$this->spreadsheet = new Spreadsheet();
$security = $this->spreadsheet->getSecurity();
$security->setLockRevision(true);
$revisionsPassword = 'revpasswd';
$security->setRevisionsPassword($revisionsPassword);
$spreadsheet2 = new Spreadsheet();
self::assertNotEquals($security, $spreadsheet2->getSecurity());
$this->spreadsheet2 = new Spreadsheet();
self::assertNotEquals($security, $this->spreadsheet2->getSecurity());
$security2 = clone $security;
$spreadsheet2->setSecurity($security2);
self::assertEquals($security, $spreadsheet2->getSecurity());
$spreadsheet->disconnectWorksheets();
$spreadsheet2->disconnectWorksheets();
$this->spreadsheet2->setSecurity($security2);
self::assertEquals($security, $this->spreadsheet2->getSecurity());
}
public function testCellXfCollection(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getFont()->setName('font1');
$sheet->getStyle('A2')->getFont()->setName('font2');
$sheet->getStyle('A3')->getFont()->setName('font3');
$sheet->getStyle('B1')->getFont()->setName('font1');
$sheet->getStyle('B2')->getFont()->setName('font2');
$collection = $spreadsheet->getCellXfCollection();
$collection = $this->spreadsheet->getCellXfCollection();
self::assertCount(4, $collection);
$font1Style = $collection[1];
self::assertTrue($spreadsheet->cellXfExists($font1Style));
self::assertSame('font1', $spreadsheet->getCellXfCollection()[1]->getFont()->getName());
self::assertTrue($this->spreadsheet->cellXfExists($font1Style));
self::assertSame('font1', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
$spreadsheet->removeCellXfByIndex(1);
self::assertFalse($spreadsheet->cellXfExists($font1Style));
self::assertSame('font2', $spreadsheet->getCellXfCollection()[1]->getFont()->getName());
$this->spreadsheet->removeCellXfByIndex(1);
self::assertFalse($this->spreadsheet->cellXfExists($font1Style));
self::assertSame('font2', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
self::assertSame('Calibri', $sheet->getStyle('A1')->getFont()->getName());
self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
self::assertSame('Calibri', $sheet->getStyle('B1')->getFont()->getName());
self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
$spreadsheet->disconnectWorksheets();
}
public function testInvalidRemoveCellXfByIndex(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('CellXf index is out of bounds.');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getFont()->setName('font1');
$sheet->getStyle('A2')->getFont()->setName('font2');
$sheet->getStyle('A3')->getFont()->setName('font3');
$sheet->getStyle('B1')->getFont()->setName('font1');
$sheet->getStyle('B2')->getFont()->setName('font2');
$spreadsheet->removeCellXfByIndex(5);
$spreadsheet->disconnectWorksheets();
$this->spreadsheet->removeCellXfByIndex(5);
}
public function testInvalidRemoveDefaultStyle(): void
@ -91,71 +103,63 @@ class SpreadsheetCoverageTest extends TestCase
$this->expectException(SSException::class);
$this->expectExceptionMessage('No default style found for this workbook');
// Removing default style probably should be disallowed.
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->removeCellXfByIndex(0);
$style = $spreadsheet->getDefaultStyle();
$spreadsheet->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->removeCellXfByIndex(0);
$this->spreadsheet->getDefaultStyle();
}
public function testCellStyleXF(): void
{
$spreadsheet = new Spreadsheet();
$collection = $spreadsheet->getCellStyleXfCollection();
$this->spreadsheet = new Spreadsheet();
$collection = $this->spreadsheet->getCellStyleXfCollection();
self::assertCount(1, $collection);
$styleXf = $collection[0];
self::assertSame($styleXf, $spreadsheet->getCellStyleXfByIndex(0));
self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByIndex(0));
$hash = $styleXf->getHashCode();
self::assertSame($styleXf, $spreadsheet->getCellStyleXfByHashCode($hash));
self::assertFalse($spreadsheet->getCellStyleXfByHashCode($hash . 'x'));
$spreadsheet->disconnectWorksheets();
self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByHashCode($hash));
self::assertFalse($this->spreadsheet->getCellStyleXfByHashCode($hash . 'x'));
}
public function testInvalidRemoveCellStyleXfByIndex(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('CellStyleXf index is out of bounds.');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->removeCellStyleXfByIndex(5);
$spreadsheet->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->removeCellStyleXfByIndex(5);
}
public function testInvalidFirstSheetIndex(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('First sheet index must be a positive integer.');
$spreadsheet = new Spreadsheet();
$spreadsheet->setFirstSheetIndex(-1);
$spreadsheet->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->setFirstSheetIndex(-1);
}
public function testInvalidVisibility(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('Invalid visibility value.');
$spreadsheet = new Spreadsheet();
$spreadsheet->setVisibility(Spreadsheet::VISIBILITY_HIDDEN);
self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $spreadsheet->getVisibility());
$spreadsheet->setVisibility(null);
self::assertSame(Spreadsheet::VISIBILITY_VISIBLE, $spreadsheet->getVisibility());
$spreadsheet->setVisibility('badvalue');
$spreadsheet->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->setVisibility(Spreadsheet::VISIBILITY_HIDDEN);
self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $this->spreadsheet->getVisibility());
$this->spreadsheet->setVisibility(null);
self::assertSame(Spreadsheet::VISIBILITY_VISIBLE, $this->spreadsheet->getVisibility());
$this->spreadsheet->setVisibility('badvalue');
}
public function testInvalidTabRatio(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('Tab ratio must be between 0 and 1000.');
$spreadsheet = new Spreadsheet();
$spreadsheet->setTabRatio(2000);
$spreadsheet->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->setTabRatio(2000);
}
public function testCopy(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getFont()->setName('font1');
$sheet->getStyle('A2')->getFont()->setName('font2');
$sheet->getStyle('A3')->getFont()->setName('font3');
@ -166,8 +170,8 @@ class SpreadsheetCoverageTest extends TestCase
$sheet->getCell('A3')->setValue('this is a3');
$sheet->getCell('B1')->setValue('this is b1');
$sheet->getCell('B2')->setValue('this is b2');
$copied = $spreadsheet->copy();
$copysheet = $copied->getActiveSheet();
$this->spreadsheet2 = $this->spreadsheet->copy();
$copysheet = $this->spreadsheet2->getActiveSheet();
$copysheet->getStyle('A2')->getFont()->setName('font12');
$copysheet->getCell('A2')->setValue('this was a2');
@ -192,18 +196,13 @@ class SpreadsheetCoverageTest extends TestCase
self::assertSame('this is a3', $copysheet->getCell('A3')->getValue());
self::assertSame('this is b1', $copysheet->getCell('B1')->getValue());
self::assertSame('this is b2', $copysheet->getCell('B2')->getValue());
$spreadsheet->disconnectWorksheets();
$copied->disconnectWorksheets();
}
public function testClone(): void
{
$this->expectException(SSException::class);
$this->expectExceptionMessage('Do not use clone on spreadsheet. Use spreadsheet->copy() instead.');
$spreadsheet = new Spreadsheet();
$clone = clone $spreadsheet;
$spreadsheet->disconnectWorksheets();
$clone->disconnectWorksheets();
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet2 = clone $this->spreadsheet;
}
}

View File

@ -40,6 +40,7 @@ class ColumnCellIteratorTest extends TestCase
$values = [];
foreach ($iterator as $key => $ColumnCell) {
self::assertNotNull($ColumnCell);
/** @scrutinizer ignore-call */
$values[] = $ColumnCell->getValue();
self::assertEquals($ColumnCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $ColumnCell);
@ -60,6 +61,7 @@ class ColumnCellIteratorTest extends TestCase
$values = [];
foreach ($iterator as $key => $ColumnCell) {
self::assertNotNull($ColumnCell);
/** @scrutinizer ignore-call */
$values[] = $ColumnCell->getValue();
self::assertEquals($ColumnCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $ColumnCell);
@ -81,6 +83,7 @@ class ColumnCellIteratorTest extends TestCase
while ($iterator->valid()) {
$current = $iterator->current();
self::assertNotNull($current);
/** @scrutinizer ignore-call */
$cell = $current->getCoordinate();
$values[] = $sheet->getCell($cell)->getValue();
$iterator->prev();

View File

@ -40,6 +40,7 @@ class RowCellIteratorTest extends TestCase
$values = [];
foreach ($iterator as $key => $RowCell) {
self::assertNotNull($RowCell);
/** @scrutinizer ignore-call */
$values[] = $RowCell->getValue();
self::assertEquals($RowCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $RowCell);
@ -59,6 +60,7 @@ class RowCellIteratorTest extends TestCase
$values = [];
foreach ($iterator as $key => $RowCell) {
self::assertNotNull($RowCell);
/** @scrutinizer ignore-call */
$values[] = $RowCell->getValue();
self::assertEquals($RowCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $RowCell);
@ -80,6 +82,7 @@ class RowCellIteratorTest extends TestCase
while ($iterator->valid()) {
$current = $iterator->current();
self::assertNotNull($current);
/** @scrutinizer ignore-call */
$cell = $current->getCoordinate();
$values[] = $sheet->getCell($cell)->getValue();
$iterator->prev();