validation-bugsnag-email

This commit is contained in:
RafficMohammed
2023-01-31 13:17:59 +05:30
parent 2ec836b447
commit 9dd3f53910
769 changed files with 20242 additions and 14060 deletions

View File

@@ -17,6 +17,10 @@ abstract class CellIterator implements NativeIterator
public const TREAT_EMPTY_STRING_AS_EMPTY_CELL = 2;
public const IF_NOT_EXISTS_RETURN_NULL = false;
public const IF_NOT_EXISTS_CREATE_NEW = true;
/**
* Worksheet to iterate.
*
@@ -38,6 +42,14 @@ abstract class CellIterator implements NativeIterator
*/
protected $onlyExistingCells = false;
/**
* If iterating all cells, and a cell doesn't exist, identifies whether a new cell should be created,
* or if the iterator should return a null value.
*
* @var bool
*/
protected $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW;
/**
* Destructor.
*/
@@ -47,6 +59,16 @@ abstract class CellIterator implements NativeIterator
$this->worksheet = $this->cellCollection = null;
}
public function getIfNotExists(): bool
{
return $this->ifNotExists;
}
public function setIfNotExists(bool $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW): void
{
$this->ifNotExists = $ifNotExists;
}
/**
* Get loop only existing cells.
*/
@@ -56,9 +78,9 @@ abstract class CellIterator implements NativeIterator
}
/**
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
* Validate start/end values for 'IterateOnlyExistingCells' mode, and adjust if necessary.
*/
abstract protected function adjustForExistingOnlyRange();
abstract protected function adjustForExistingOnlyRange(): void;
/**
* Set the iterator to loop only existing cells.

View File

@@ -52,14 +52,23 @@ class Column
*
* @param int $startRow The row number at which to start iterating
* @param int $endRow Optionally, the row number at which to stop iterating
*
* @return ColumnCellIterator
*/
public function getCellIterator($startRow = 1, $endRow = null)
public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterator
{
return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow);
}
/**
* Get row iterator. Synonym for getCellIterator().
*
* @param int $startRow The row number at which to start iterating
* @param int $endRow Optionally, the row number at which to stop iterating
*/
public function getRowIterator($startRow = 1, $endRow = null): ColumnCellIterator
{
return $this->getCellIterator($startRow, $endRow);
}
/**
* Returns a boolean true if the column contains no cells. By default, this means that no cell records exist in the
* collection for this column. false will be returned otherwise.
@@ -76,13 +85,15 @@ class Column
* Possible Flag Values are:
* CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
* CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
* @param int $startRow The row number at which to start iterating
* @param int $endRow Optionally, the row number at which to stop iterating
*/
public function isEmpty(int $definitionOfEmptyFlags = 0): bool
public function isEmpty(int $definitionOfEmptyFlags = 0, $startRow = 1, $endRow = null): bool
{
$nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
$emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
$cellIterator = $this->getCellIterator();
$cellIterator = $this->getCellIterator($startRow, $endRow);
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
/** @scrutinizer ignore-call */

View File

@@ -128,7 +128,11 @@ class ColumnCellIterator extends CellIterator
return $this->cellCollection->has($cellAddress)
? $this->cellCollection->get($cellAddress)
: $this->worksheet->createNewCell($cellAddress);
: (
$this->ifNotExists === self::IF_NOT_EXISTS_CREATE_NEW
? $this->worksheet->createNewCell($cellAddress)
: null
);
}
/**

View File

@@ -10,7 +10,7 @@ class ColumnDimension extends Dimension
/**
* Column index.
*
* @var string
* @var ?string
*/
private $columnIndex;
@@ -33,7 +33,7 @@ class ColumnDimension extends Dimension
/**
* Create a new ColumnDimension.
*
* @param string $index Character column index
* @param ?string $index Character column index
*/
public function __construct($index = 'A')
{
@@ -47,7 +47,7 @@ class ColumnDimension extends Dimension
/**
* Get column index as string eg: 'A'.
*/
public function getColumnIndex(): string
public function getColumnIndex(): ?string
{
return $this->columnIndex;
}
@@ -67,7 +67,7 @@ class ColumnDimension extends Dimension
*/
public function getColumnNumeric(): int
{
return Coordinate::columnIndexFromString($this->columnIndex);
return Coordinate::columnIndexFromString($this->columnIndex ?? '');
}
/**

View File

@@ -220,7 +220,7 @@ class Shadow implements IComparable
*
* @return $this
*/
public function setColor(?Color $color = null)
public function setColor(Color $color)
{
$this->color = $color;

View File

@@ -639,7 +639,7 @@ class PageSetup
public function getPrintArea($index = 0)
{
if ($index == 0) {
return $this->printArea;
return (string) $this->printArea;
}
$printAreas = explode(',', (string) $this->printArea);
if (isset($printAreas[$index - 1])) {

View File

@@ -51,14 +51,23 @@ class Row
*
* @param string $startColumn The column address at which to start iterating
* @param string $endColumn Optionally, the column address at which to stop iterating
*
* @return RowCellIterator
*/
public function getCellIterator($startColumn = 'A', $endColumn = null)
public function getCellIterator($startColumn = 'A', $endColumn = null): RowCellIterator
{
return new RowCellIterator($this->worksheet, $this->rowIndex, $startColumn, $endColumn);
}
/**
* Get column iterator. Synonym for getCellIterator().
*
* @param string $startColumn The column address at which to start iterating
* @param string $endColumn Optionally, the column address at which to stop iterating
*/
public function getColumnIterator($startColumn = 'A', $endColumn = null): RowCellIterator
{
return $this->getCellIterator($startColumn, $endColumn);
}
/**
* Returns a boolean true if the row contains no cells. By default, this means that no cell records exist in the
* collection for this row. false will be returned otherwise.
@@ -75,13 +84,15 @@ class Row
* Possible Flag Values are:
* CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
* CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
* @param string $startColumn The column address at which to start iterating
* @param string $endColumn Optionally, the column address at which to stop iterating
*/
public function isEmpty(int $definitionOfEmptyFlags = 0): bool
public function isEmpty(int $definitionOfEmptyFlags = 0, $startColumn = 'A', $endColumn = null): bool
{
$nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
$emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
$cellIterator = $this->getCellIterator();
$cellIterator = $this->getCellIterator($startColumn, $endColumn);
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
/** @scrutinizer ignore-call */

View File

@@ -127,7 +127,11 @@ class RowCellIterator extends CellIterator
return $this->cellCollection->has($cellAddress)
? $this->cellCollection->get($cellAddress)
: $this->worksheet->createNewCell($cellAddress);
: (
$this->ifNotExists === self::IF_NOT_EXISTS_CREATE_NEW
? $this->worksheet->createNewCell($cellAddress)
: null
);
}
/**

View File

@@ -9,7 +9,7 @@ class RowDimension extends Dimension
/**
* Row index.
*
* @var int
* @var ?int
*/
private $rowIndex;
@@ -32,7 +32,7 @@ class RowDimension extends Dimension
/**
* Create a new RowDimension.
*
* @param int $index Numeric row index
* @param ?int $index Numeric row index
*/
public function __construct($index = 0)
{
@@ -46,7 +46,7 @@ class RowDimension extends Dimension
/**
* Get Row Index.
*/
public function getRowIndex(): int
public function getRowIndex(): ?int
{
return $this->rowIndex;
}

View File

@@ -53,6 +53,11 @@ class RowIterator implements NativeIterator
$this->resetStart($startRow);
}
public function __destruct()
{
$this->subject = null; // @phpstan-ignore-line
}
/**
* (Re)Set the start row and the current row pointer.
*

View File

@@ -148,7 +148,7 @@ class Table
$tableName = StringHelper::strToLower($name);
if ($worksheet !== null && StringHelper::strToLower($this->name) !== $name) {
$spreadsheet = $worksheet->getParent();
$spreadsheet = $worksheet->getParentOrThrow();
foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
foreach ($sheet->getTableCollection() as $table) {
@@ -170,7 +170,7 @@ class Table
if (StringHelper::strToLower($this->name) !== StringHelper::strToLower($name)) {
// We need to check all formula cells that might contain fully-qualified Structured References
// that refer to this table, and update those formulae to reference the new table name
$spreadsheet = $this->workSheet->getParent();
$spreadsheet = $this->workSheet->getParentOrThrow();
foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
$this->updateStructuredReferencesInCells($sheet, $name);
}
@@ -298,8 +298,8 @@ class Table
}
[$width, $height] = Coordinate::rangeDimension($range);
if ($width < 1 || $height < 2) {
throw new PhpSpreadsheetException('The table range must be at least 1 column and 2 rows');
if ($width < 1 || $height < 1) {
throw new PhpSpreadsheetException('The table range must be at least 1 column and row');
}
$this->range = $range;
@@ -347,7 +347,7 @@ class Table
public function setWorksheet(?Worksheet $worksheet = null): self
{
if ($this->name !== '' && $worksheet !== null) {
$spreadsheet = $worksheet->getParent();
$spreadsheet = $worksheet->getParentOrThrow();
$tableName = StringHelper::strToUpper($this->name);
foreach ($spreadsheet->getWorksheetIterator() as $sheet) {

View File

@@ -205,9 +205,9 @@ class Column
return $this;
}
public static function updateStructuredReferences(?Worksheet $workSheet, ?string $oldTitle, string $newTitle): void
public static function updateStructuredReferences(?Worksheet $workSheet, ?string $oldTitle, ?string $newTitle): void
{
if ($workSheet === null || $oldTitle === null || $oldTitle === '') {
if ($workSheet === null || $oldTitle === null || $oldTitle === '' || $newTitle === null) {
return;
}
@@ -215,7 +215,7 @@ class Column
if (StringHelper::strToLower($oldTitle) !== StringHelper::strToLower($newTitle)) {
// We need to check all formula cells that might contain Structured References that refer
// to this column, and update those formulae to reference the new column text
$spreadsheet = $workSheet->getParent();
$spreadsheet = $workSheet->getParentOrThrow();
foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
self::updateStructuredReferencesInCells($sheet, $oldTitle, $newTitle);
}

View File

@@ -103,7 +103,7 @@ class Validations
$coordinate = strtoupper($coordinate);
// Eliminate leading equal sign
$testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
$defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);
$defined = $worksheet->getParentOrThrow()->getDefinedName($testCoordinate, $worksheet);
if ($defined !== null) {
if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
$coordinate = (string) preg_replace('/^=/', '', $defined->getValue());

View File

@@ -64,7 +64,7 @@ class Worksheet implements IComparable
/**
* Parent spreadsheet.
*
* @var Spreadsheet
* @var ?Spreadsheet
*/
private $parent;
@@ -399,7 +399,6 @@ class Worksheet implements IComparable
$this->cellCollection = null;
}
// detach ourself from the workbook, so that it can then delete this worksheet successfully
// @phpstan-ignore-next-line
$this->parent = null;
}
@@ -644,13 +643,9 @@ class Worksheet implements IComparable
*/
public function getChartByName($chartName)
{
$chartCount = count($this->chartCollection);
if ($chartCount == 0) {
return false;
}
foreach ($this->chartCollection as $index => $chart) {
if ($chart->getName() == $chartName) {
return $this->chartCollection[$index];
return $chart;
}
}
@@ -760,7 +755,7 @@ class Worksheet implements IComparable
//The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide)
if ($isMerged && $cell->isMergeRangeValueCell()) {
$range = $cell->getMergeRange();
$range = (string) $cell->getMergeRange();
$rangeBoundaries = Coordinate::rangeDimension($range);
if ($rangeBoundaries[0] === 1) {
$isMergedButProceed = true;
@@ -782,7 +777,7 @@ class Worksheet implements IComparable
// To formatted string
$cellValue = NumberFormat::toFormattedString(
$cell->getCalculatedValue(),
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
(string) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())
->getNumberFormat()->getFormatCode()
);
@@ -790,11 +785,11 @@ class Worksheet implements IComparable
$autoSizes[$this->cellCollection->getCurrentColumn()] = max(
(float) $autoSizes[$this->cellCollection->getCurrentColumn()],
(float) Shared\Font::calculateColumnWidth(
$this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$cellValue,
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
(int) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())
->getAlignment()->getTextRotation(),
$this->getParent()->getDefaultStyle()->getFont(),
$this->getParentOrThrow()->getDefaultStyle()->getFont(),
$filterAdjustment,
$indentAdjustment
)
@@ -817,15 +812,25 @@ class Worksheet implements IComparable
}
/**
* Get parent.
*
* @return Spreadsheet
* Get parent or null.
*/
public function getParent()
public function getParent(): ?Spreadsheet
{
return $this->parent;
}
/**
* Get parent, throw exception if null.
*/
public function getParentOrThrow(): Spreadsheet
{
if ($this->parent !== null) {
return $this->parent;
}
throw new Exception('Sheet does not have a parent.');
}
/**
* Re-bind parent.
*
@@ -1274,7 +1279,7 @@ class Worksheet implements IComparable
if (strpos($coordinate, '!') !== false) {
$worksheetReference = self::extractSheetTitle($coordinate, true);
$sheet = $this->parent->getSheetByName($worksheetReference[0]);
$sheet = $this->getParentOrThrow()->getSheetByName($worksheetReference[0]);
$finalCoordinate = strtoupper($worksheetReference[1]);
if ($sheet === null) {
@@ -1509,12 +1514,12 @@ class Worksheet implements IComparable
$cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate);
// set this sheet as active
$this->parent->setActiveSheetIndex($this->parent->getIndex($this));
$this->getParentOrThrow()->setActiveSheetIndex($this->getParentOrThrow()->getIndex($this));
// set cell coordinate as active
$this->setSelectedCells($cellCoordinate);
return $this->parent->getCellXfSupervisor();
return $this->getParentOrThrow()->getCellXfSupervisor();
}
/**
@@ -1666,8 +1671,8 @@ class Worksheet implements IComparable
public function duplicateStyle(Style $style, $range)
{
// Add the style to the workbook if necessary
$workbook = $this->parent;
if ($existingStyle = $this->parent->getCellXfByHashCode($style->getHashCode())) {
$workbook = $this->getParentOrThrow();
if ($existingStyle = $workbook->getCellXfByHashCode($style->getHashCode())) {
// there is already such cell Xf in our collection
$xfIndex = $existingStyle->getIndex();
} else {
@@ -1899,7 +1904,7 @@ class Worksheet implements IComparable
public function mergeCellBehaviour(Cell $cell, string $upperLeft, string $behaviour, array $leftCellValue): array
{
if ($cell->getCoordinate() !== $upperLeft) {
Calculation::getInstance($cell->getWorksheet()->getParent())->flushInstance();
Calculation::getInstance($cell->getWorksheet()->getParentOrThrow())->flushInstance();
if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) {
$cellValue = $cell->getFormattedValue();
if ($cellValue !== '') {
@@ -2236,6 +2241,12 @@ class Worksheet implements IComparable
return $tableNames;
}
/** @var null|Table */
private static $scrutinizerNullTable;
/** @var null|int */
private static $scrutinizerNullInt;
/**
* @param string $name the table name to search
*
@@ -2245,7 +2256,7 @@ class Worksheet implements IComparable
{
$tableIndex = $this->getTableIndexByName($name);
return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex];
return ($tableIndex === null) ? self::$scrutinizerNullTable : $this->tableCollection[$tableIndex];
}
/**
@@ -2263,7 +2274,7 @@ class Worksheet implements IComparable
}
}
return null;
return self::$scrutinizerNullInt;
}
/**
@@ -2495,7 +2506,7 @@ class Worksheet implements IComparable
} elseif ($num > $highRow) {
$num -= $numberOfRows;
$cloneDimension = clone $rowDimension;
$cloneDimension->setRowIndex($num);
$cloneDimension->setRowIndex(/** @scrutinizer ignore-type */ $num);
$holdRowDimensions[$num] = $cloneDimension;
}
}
@@ -2967,9 +2978,10 @@ class Worksheet implements IComparable
$cRef = $returnCellRef ? $col : ++$c;
// Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
// so we test and retrieve directly against cellCollection
if ($this->cellCollection->has($col . $row)) {
$cell = $this->cellCollection->get($col . $row);
//if ($this->cellCollection->has($col . $row)) {
if ($cell !== null) {
// Cell exists
$cell = $this->cellCollection->get($col . $row);
if ($cell->getValue() !== null) {
if ($cell->getValue() instanceof RichText) {
$returnValue[$rRef][$cRef] = $cell->getValue()->getPlainText();
@@ -2982,10 +2994,10 @@ class Worksheet implements IComparable
}
if ($formatData) {
$style = $this->parent->getCellXfByIndex($cell->getXfIndex());
$style = $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex());
$returnValue[$rRef][$cRef] = NumberFormat::toFormattedString(
$returnValue[$rRef][$cRef],
($style && $style->getNumberFormat()) ? $style->getNumberFormat()->getFormatCode() : NumberFormat::FORMAT_GENERAL
$style->getNumberFormat()->getFormatCode() ?? NumberFormat::FORMAT_GENERAL
);
}
} else {
@@ -3022,14 +3034,17 @@ class Worksheet implements IComparable
throw new Exception('Defined Named ' . $definedName . ' is a formula, not a range or cell.');
}
if ($namedRange->getLocalOnly() && $this->getHashCode() !== $namedRange->getWorksheet()->getHashCode()) {
if ($returnNullIfInvalid) {
return null;
}
if ($namedRange->getLocalOnly()) {
$worksheet = $namedRange->getWorksheet();
if ($worksheet === null || $this->getHashCode() !== $worksheet->getHashCode()) {
if ($returnNullIfInvalid) {
return null;
}
throw new Exception(
'Named range ' . $definedName . ' is not accessible from within sheet ' . $this->getTitle()
);
throw new Exception(
'Named range ' . $definedName . ' is not accessible from within sheet ' . $this->getTitle()
);
}
}
return $namedRange;
@@ -3049,13 +3064,18 @@ class Worksheet implements IComparable
*/
public function namedRangeToArray(string $definedName, $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false)
{
$retVal = [];
$namedRange = $this->validateNamedRange($definedName);
$workSheet = $namedRange->getWorksheet();
/** @phpstan-ignore-next-line */
$cellRange = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$cellRange = str_replace('$', '', $cellRange);
if ($namedRange !== null) {
$cellRange = ltrim(substr($namedRange->getValue(), (int) strrpos($namedRange->getValue(), '!')), '!');
$cellRange = str_replace('$', '', $cellRange);
$workSheet = $namedRange->getWorksheet();
if ($workSheet !== null) {
$retVal = $workSheet->rangeToArray($cellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef);
}
}
return $workSheet->rangeToArray($cellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef);
return $retVal;
}
/**
@@ -3516,16 +3536,16 @@ class Worksheet implements IComparable
// We use the same code that setTitle to find a valid codeName else not using a space (Excel don't like) but a '_'
if ($this->getParent()) {
if ($this->parent !== null) {
// Is there already such sheet name?
if ($this->getParent()->sheetCodeNameExists($codeName)) {
if ($this->parent->sheetCodeNameExists($codeName)) {
// Use name, but append with lowest possible integer
if (Shared\StringHelper::countCharacters($codeName) > 29) {
$codeName = Shared\StringHelper::substring($codeName, 0, 29);
}
$i = 1;
while ($this->getParent()->sheetCodeNameExists($codeName . '_' . $i)) {
while ($this->getParentOrThrow()->sheetCodeNameExists($codeName . '_' . $i)) {
++$i;
if ($i == 10) {
if (Shared\StringHelper::countCharacters($codeName) > 28) {