Skip to content

Commit

Permalink
Merge branch 'master' into issue4108
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman committed Aug 1, 2024
2 parents a157e3f + fcca8ac commit 3e5f1fe
Show file tree
Hide file tree
Showing 64 changed files with 422 additions and 251 deletions.
204 changes: 106 additions & 98 deletions composer.lock

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class Calculation

/**
* Instance of this class.
*
* @var ?Calculation
*/
private static ?Calculation $instance = null;

Expand Down Expand Up @@ -3272,10 +3270,8 @@ private static function translateFormula(array $from, array $to, string $formula
return $formula;
}

/** @var ?array */
private static ?array $functionReplaceFromExcel;

/** @var ?array */
private static ?array $functionReplaceToLocale;

/**
Expand Down Expand Up @@ -3321,10 +3317,8 @@ public function translateFormulaToLocale(string $formula): string
);
}

/** @var ?array */
private static ?array $functionReplaceFromLocale;

/** @var ?array */
private static ?array $functionReplaceToExcel;

/**
Expand Down Expand Up @@ -5087,9 +5081,10 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
if ($cell === null || $pCellWorksheet === null) {
return $this->raiseFormulaError("undefined name '$token'");
}
$specifiedWorksheet = trim($matches[2], "'");

$this->debugLog->writeDebugLog('Evaluating Defined Name %s', $definedName);
$namedRange = DefinedName::resolveName($definedName, $pCellWorksheet);
$namedRange = DefinedName::resolveName($definedName, $pCellWorksheet, $specifiedWorksheet);
// If not Defined Name, try as Table.
if ($namedRange === null && $this->spreadsheet !== null) {
$table = $this->spreadsheet->getTableByName($definedName);
Expand All @@ -5114,7 +5109,7 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
return $this->raiseFormulaError("undefined name '$definedName'");
}

$result = $this->evaluateDefinedName($cell, $namedRange, $pCellWorksheet, $stack);
$result = $this->evaluateDefinedName($cell, $namedRange, $pCellWorksheet, $stack, $specifiedWorksheet !== '');
if (isset($storeKey)) {
$branchStore[$storeKey] = $result;
}
Expand Down Expand Up @@ -5593,10 +5588,10 @@ private function addCellReference(array $args, bool $passCellReference, array|st
return $args;
}

private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksheet $cellWorksheet, Stack $stack): mixed
private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksheet $cellWorksheet, Stack $stack, bool $ignoreScope = false): mixed
{
$definedNameScope = $namedRange->getScope();
if ($definedNameScope !== null && $definedNameScope !== $cellWorksheet) {
if ($definedNameScope !== null && $definedNameScope !== $cellWorksheet && !$ignoreScope) {
// The defined name isn't in our current scope, so #REF
$result = ExcelError::REF();
$stack->push('Error', $result, $namedRange->getName());
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Helpers
*/
public static function isLeapYear(int|string $year): bool
{
$year = (int) $year;

return (($year % 4) === 0) && (($year % 100) !== 0) || (($year % 400) === 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function fromString(null|array|string|int|bool|float $timeValue):

$arraySplit = preg_split('/[\/:\-\s]/', $timeValue) ?: [];
if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) {
$arraySplit[0] = ($arraySplit[0] % 24);
$arraySplit[0] = ((int) $arraySplit[0] % 24);
$timeValue = implode(':', $arraySplit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function rate(
$guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess);

try {
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
$numberOfPeriods = CashFlowValidations::validateFloat($numberOfPeriods);
$payment = CashFlowValidations::validateFloat($payment);
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
Expand Down Expand Up @@ -193,7 +193,7 @@ public static function rate(
return $close ? $rate : ExcelError::NAN();
}

private static function rateNextGuess(float $rate, int $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float
private static function rateNextGuess(float $rate, float $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float
{
if ($rate == 0.0) {
return ExcelError::NAN();
Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class Cell implements Stringable

/**
* The collection of cells that this cell belongs to (i.e. The Cell Collection for the parent Worksheet).
*
* @var ?Cells
*/
private ?Cells $parent;

Expand Down
1 change: 0 additions & 1 deletion src/PhpSpreadsheet/Chart/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ abstract class Properties

protected bool $objectState = false; // used only for minor gridlines

/** @var ?float */
protected ?float $glowSize = null;

protected ChartColor $glowColor;
Expand Down
3 changes: 0 additions & 3 deletions src/PhpSpreadsheet/Helper/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,10 @@ class Html
'yellowgreen' => '9acd32',
];

/** @var ?string */
private ?string $face = null;

/** @var ?string */
private ?string $size = null;

/** @var ?string */
private ?string $color = null;

private bool $bold = false;
Expand Down
1 change: 0 additions & 1 deletion src/PhpSpreadsheet/Reader/Csv/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Delimiter

protected int $numberLines = 0;

/** @var ?string */
protected ?string $delimiter = null;

/**
Expand Down
5 changes: 4 additions & 1 deletion src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,10 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$r
{
foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) {
$domText = (string) preg_replace('/\s+/u', ' ', trim($child->nodeValue ?? ''));
$domText = (string) preg_replace('/\s+/', ' ', trim($child->nodeValue ?? ''));
if ($domText === "\u{a0}") {
$domText = '';
}
if (is_string($cellContent)) {
// simply append the text if the cell content is a plain text string
$cellContent .= $domText;
Expand Down
32 changes: 16 additions & 16 deletions src/PhpSpreadsheet/Reader/Ods/PageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ private function readPageSettingStyles(DOMDocument $styleDom): void

foreach ($styles as $styleSet) {
$styleName = $styleSet->getAttributeNS($this->stylesNs, 'name');
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0];
$styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation');
$styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to');
$stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order');
$centered = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'table-centering');

$marginLeft = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-left');
$marginRight = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-right');
$marginTop = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-top');
$marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0];
$headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0];
$footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0);
$styleOrientation = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-orientation');
$styleScale = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'scale-to');
$stylePrintOrder = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-page-order');
$centered = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'table-centering');

$marginLeft = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-left');
$marginRight = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-right');
$marginTop = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-top');
$marginBottom = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0);
$headerProperties = $header?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginHeader = $headerProperties?->getAttributeNS($this->stylesFo, 'min-height');
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0);
$footerProperties = $footer?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginFooter = $footerProperties?->getAttributeNS($this->stylesFo, 'min-height');

$this->pageLayoutStyles[$styleName] = (object) [
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT,
Expand Down
24 changes: 18 additions & 6 deletions src/PhpSpreadsheet/Reader/Security/XmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ private static function forceString(mixed $arg): string

private function toUtf8(string $xml): string
{
$pattern = '/encoding="(.*?)"/';
$result = preg_match($pattern, $xml, $matches);
$charset = strtoupper($result ? $matches[1] : 'UTF-8');

$charset = $this->findCharSet($xml);
if ($charset !== 'UTF-8') {
$xml = self::forceString(mb_convert_encoding($xml, 'UTF-8', $charset));

$result = preg_match($pattern, $xml, $matches);
$charset = strtoupper($result ? $matches[1] : 'UTF-8');
$charset = $this->findCharSet($xml);
if ($charset !== 'UTF-8') {
throw new Reader\Exception('Suspicious Double-encoded XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
}
Expand All @@ -52,6 +48,22 @@ private function toUtf8(string $xml): string
return $xml;
}

private function findCharSet(string $xml): string
{
$patterns = [
'/encoding="([^"]*]?)"/',
"/encoding='([^']*?)'/",
];

foreach ($patterns as $pattern) {
if (preg_match($pattern, $xml, $matches)) {
return strtoupper($matches[1]);
}
}

return 'UTF-8';
}

/**
* Scan the XML for use of <!ENTITY to prevent XXE/XEE attacks.
*
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Slk.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private function processPRecord(array $rowData, Spreadsheet &$spreadsheet): void
private function processPColors(string $rowDatum, array &$formatArray): void
{
if (preg_match('/L([1-9]\\d*)/', $rowDatum, $matches)) {
$fontColor = $matches[1] % 8;
$fontColor = ((int) $matches[1]) % 8;
$formatArray['font']['color']['argb'] = self::COLOR_ARRAY[$fontColor];
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Reader/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ class Xls extends BaseReader

/**
* The current RC4 decryption object.
*
* @var ?Xls\RC4
*/
private ?Xls\RC4 $rc4Key = null;

Expand Down
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Reader/Xlsx/SheetViewOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ private function sheetFormat(SimpleXMLElement $sheetFormatPrx): void
private function printOptions(SimpleXMLElement $printOptionsx): void
{
$printOptions = $printOptionsx->attributes() ?? [];
if (isset($printOptions['gridLinesSet']) && self::boolean((string) $printOptions['gridLinesSet'])) {
$this->worksheet->setShowGridlines(true);
}
// Spec is weird. gridLines (default false)
// and gridLinesSet (default true) must both be true.
if (isset($printOptions['gridLines']) && self::boolean((string) $printOptions['gridLines'])) {
$this->worksheet->setPrintGridlines(true);
if (!isset($printOptions['gridLinesSet']) || self::boolean((string) $printOptions['gridLinesSet'])) {
$this->worksheet->setPrintGridlines(true);
}
}
if (isset($printOptions['horizontalCentered']) && self::boolean((string) $printOptions['horizontalCentered'])) {
$this->worksheet->getPageSetup()->setHorizontalCentered(true);
Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Styles extends BaseParserClass
{
/**
* Theme instance.
*
* @var ?Theme
*/
private ?Theme $theme = null;

Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class ReferenceHelper

/**
* Instance of this class.
*
* @var ?ReferenceHelper
*/
private static ?ReferenceHelper $instance = null;

Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/RichText/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class Run extends TextElement implements ITextElement
{
/**
* Font.
*
* @var ?Font
*/
private ?Font $font;

Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Settings

/**
* The cache implementation to be used for cell collection.
*
* @var ?CacheInterface
*/
private static ?CacheInterface $cache = null;

Expand Down
4 changes: 0 additions & 4 deletions src/PhpSpreadsheet/Shared/Escher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ class Escher
{
/**
* Drawing Group Container.
*
* @var ?Escher\DggContainer
*/
private ?Escher\DggContainer $dggContainer = null;

/**
* Drawing Container.
*
* @var ?Escher\DgContainer
*/
private ?Escher\DgContainer $dgContainer = null;

Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Shared/Escher/DggContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class DggContainer

/**
* BLIP Store Container.
*
* @var ?DggContainer\BstoreContainer
*/
private ?DggContainer\BstoreContainer $bstoreContainer = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class BSE

/**
* The BLIP (Big Large Image or Picture).
*
* @var ?BSE\Blip
*/
private ?BSE\Blip $blip = null;

Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null): Workshe
if ($this->activeSheetIndex >= $sheetIndex) {
++$this->activeSheetIndex;
}
if ($this->activeSheetIndex < 0) {
$this->activeSheetIndex = 0;
}
}

if ($worksheet->getParent() === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

abstract class WizardAbstract
{
/**
* @var ?Style
*/
protected ?Style $style = null;

protected string $expression;
Expand Down
2 changes: 0 additions & 2 deletions src/PhpSpreadsheet/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ class Font extends Supervisor

private string $strikeType = '';

/** @var ?ChartColor */
private ?ChartColor $underlineColor = null;

/** @var ?ChartColor */
private ?ChartColor $chartColor = null;
// end of chart title items

Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3684,7 +3684,9 @@ public function applyStylesFromArray(string $coordinate, array $styleArray): boo
$originalSelected = $this->selectedCells;
$this->getStyle($coordinate)->applyFromArray($styleArray);
$this->selectedCells = $originalSelected;
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
if ($activeSheetIndex >= 0) {
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ class SetupTeardownDatabases extends TestCase
{
protected const RESULT_CELL = 'Z1';

/**
* @var ?Spreadsheet
*/
private ?Spreadsheet $spreadsheet = null;

/**
* @var ?Worksheet
*/
private ?Worksheet $sheet = null;

protected function setUp(): void
Expand Down
Loading

0 comments on commit 3e5f1fe

Please sign in to comment.