Skip to content

Commit b0b7c9a

Browse files
authored
Revert "Start work on array-enabling the Lookup and Reference functions (#2602)"
This reverts commit c10d86e.
1 parent c10d86e commit b0b7c9a

File tree

20 files changed

+32
-305
lines changed

20 files changed

+32
-305
lines changed

src/PhpSpreadsheet/Calculation/ArrayEnabled.php

-21
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,4 @@ protected static function evaluateArrayArgumentsSubset(callable $method, int $li
5353

5454
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
5555
}
56-
57-
/**
58-
* @param mixed ...$arguments
59-
*/
60-
protected static function evaluateArrayArgumentsSubsetFrom(callable $method, int $start, ...$arguments): array
61-
{
62-
$arrayArgumentsSubset = array_combine(
63-
range($start, count($arguments) - $start),
64-
array_slice($arguments, $start)
65-
);
66-
if ($arrayArgumentsSubset === false) {
67-
return ['#VALUE!'];
68-
}
69-
70-
self::initialiseHelper($arrayArgumentsSubset);
71-
$leadingArguments = array_slice($arguments, 0, $start);
72-
$arguments = self::$arrayArgumentHelper->arguments();
73-
$arguments = array_merge($leadingArguments, $arguments);
74-
75-
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
76-
}
7756
}

src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
class ArrayArgumentHelper
88
{
9-
/**
10-
* @var int
11-
*/
12-
protected $indexStart = 0;
13-
149
/**
1510
* @var array
1611
*/
@@ -33,8 +28,6 @@ class ArrayArgumentHelper
3328

3429
public function initialise(array $arguments): void
3530
{
36-
$keys = array_keys($arguments);
37-
$this->indexStart = (int) array_shift($keys);
3831
$this->rows = $this->rows($arguments);
3932
$this->columns = $this->columns($arguments);
4033

@@ -64,7 +57,7 @@ public function getFirstArrayArgumentNumber(): int
6457
$rowArrays = $this->filterArray($this->rows);
6558
$columnArrays = $this->filterArray($this->columns);
6659

67-
for ($index = $this->indexStart; $index < $this->argumentCount; ++$index) {
60+
for ($index = 0; $index < $this->argumentCount; ++$index) {
6861
if (isset($rowArrays[$index]) || isset($columnArrays[$index])) {
6962
return ++$index;
7063
}
@@ -83,7 +76,7 @@ public function getSingleRowVector(): ?int
8376
private function getRowVectors(): array
8477
{
8578
$rowVectors = [];
86-
for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) {
79+
for ($index = 0; $index < $this->argumentCount; ++$index) {
8780
if ($this->rows[$index] === 1 && $this->columns[$index] > 1) {
8881
$rowVectors[] = $index;
8982
}
@@ -102,7 +95,7 @@ public function getSingleColumnVector(): ?int
10295
private function getColumnVectors(): array
10396
{
10497
$columnVectors = [];
105-
for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) {
98+
for ($index = 0; $index < $this->argumentCount; ++$index) {
10699
if ($this->rows[$index] > 1 && $this->columns[$index] === 1) {
107100
$columnVectors[] = $index;
108101
}
@@ -113,7 +106,7 @@ private function getColumnVectors(): array
113106

114107
public function getMatrixPair(): array
115108
{
116-
for ($i = $this->indexStart; $i < ($this->indexStart + $this->argumentCount - 1); ++$i) {
109+
for ($i = 0; $i < ($this->argumentCount - 1); ++$i) {
117110
for ($j = $i + 1; $j < $this->argumentCount; ++$j) {
118111
if (isset($this->rows[$i], $this->rows[$j])) {
119112
return [$i, $j];

src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentProcessor.php

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static function processArguments(
3333

3434
$singleRowVectorIndex = self::$arrayArgumentHelper->getSingleRowVector();
3535
$singleColumnVectorIndex = self::$arrayArgumentHelper->getSingleColumnVector();
36-
3736
if ($singleRowVectorIndex !== null && $singleColumnVectorIndex !== null) {
3837
// Basic logic for a single row vector and a single column vector
3938
return self::evaluateVectorPair($method, $singleRowVectorIndex, $singleColumnVectorIndex, ...$arguments);

src/PhpSpreadsheet/Calculation/Functions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public static function isNonText($value = null)
518518
*
519519
* @param null|mixed $value The value you want converted
520520
*
521-
* @return number|string N converts values listed in the following table
521+
* @return number N converts values listed in the following table
522522
* If value is or refers to N returns
523523
* A number That number
524524
* A date The serial number of that date

src/PhpSpreadsheet/Calculation/LookupRef.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class LookupRef
4141
* @param bool $referenceStyle A logical value that specifies the A1 or R1C1 reference style.
4242
* TRUE or omitted CELL_ADDRESS returns an A1-style reference
4343
* FALSE CELL_ADDRESS returns an R1C1-style reference
44-
* @param array|string $sheetText Optional Name of worksheet to use
44+
* @param string $sheetText Optional Name of worksheet to use
4545
*
46-
* @return array|string
46+
* @return string
4747
*/
4848
public static function cellAddress($row, $column, $relativity = 1, $referenceStyle = true, $sheetText = '')
4949
{
@@ -277,7 +277,7 @@ public static function CHOOSE(...$chooseArgs)
277277
* @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below.
278278
* If match_type is 1 or -1, the list has to be ordered.
279279
*
280-
* @return array|int|string The relative position of the found item
280+
* @return int|string The relative position of the found item
281281
*/
282282
public static function MATCH($lookupValue, $lookupArray, $matchType = 1)
283283
{

src/PhpSpreadsheet/Calculation/LookupRef/Address.php

+7-27
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
66
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
77
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
88

99
class Address
1010
{
11-
use ArrayEnabled;
12-
1311
public const ADDRESS_ABSOLUTE = 1;
1412
public const ADDRESS_COLUMN_RELATIVE = 2;
1513
public const ADDRESS_ROW_RELATIVE = 3;
@@ -27,44 +25,26 @@ class Address
2725
* =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText])
2826
*
2927
* @param mixed $row Row number (integer) to use in the cell reference
30-
* Or can be an array of values
3128
* @param mixed $column Column number (integer) to use in the cell reference
32-
* Or can be an array of values
3329
* @param mixed $relativity Integer flag indicating the type of reference to return
3430
* 1 or omitted Absolute
3531
* 2 Absolute row; relative column
3632
* 3 Relative row; absolute column
3733
* 4 Relative
38-
* Or can be an array of values
3934
* @param mixed $referenceStyle A logical (boolean) value that specifies the A1 or R1C1 reference style.
4035
* TRUE or omitted ADDRESS returns an A1-style reference
4136
* FALSE ADDRESS returns an R1C1-style reference
42-
* Or can be an array of values
4337
* @param mixed $sheetName Optional Name of worksheet to use
44-
* Or can be an array of values
4538
*
46-
* @return array|string
47-
* If an array of values is passed as the $testValue argument, then the returned result will also be
48-
* an array with the same dimensions
39+
* @return string
4940
*/
5041
public static function cell($row, $column, $relativity = 1, $referenceStyle = true, $sheetName = '')
5142
{
52-
if (
53-
is_array($row) || is_array($column) ||
54-
is_array($relativity) || is_array($referenceStyle) || is_array($sheetName)
55-
) {
56-
return self::evaluateArrayArguments(
57-
[self::class, __FUNCTION__],
58-
$row,
59-
$column,
60-
$relativity,
61-
$referenceStyle,
62-
$sheetName
63-
);
64-
}
65-
66-
$relativity = $relativity ?? 1;
67-
$referenceStyle = $referenceStyle ?? true;
43+
$row = Functions::flattenSingleValue($row);
44+
$column = Functions::flattenSingleValue($column);
45+
$relativity = ($relativity === null) ? 1 : Functions::flattenSingleValue($relativity);
46+
$referenceStyle = ($referenceStyle === null) ? true : Functions::flattenSingleValue($referenceStyle);
47+
$sheetName = Functions::flattenSingleValue($sheetName);
6848

6949
if (($row < 1) || ($column < 1)) {
7050
return ExcelError::VALUE();

src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
65
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
76
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
@@ -11,8 +10,6 @@
1110

1211
class ExcelMatch
1312
{
14-
use ArrayEnabled;
15-
1613
public const MATCHTYPE_SMALLEST_VALUE = -1;
1714
public const MATCHTYPE_FIRST_VALUE = 0;
1815
public const MATCHTYPE_LARGEST_VALUE = 1;
@@ -30,16 +27,15 @@ class ExcelMatch
3027
* @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below.
3128
* If match_type is 1 or -1, the list has to be ordered.
3229
*
33-
* @return array|int|string The relative position of the found item
30+
* @return int|string The relative position of the found item
3431
*/
3532
public static function MATCH($lookupValue, $lookupArray, $matchType = self::MATCHTYPE_LARGEST_VALUE)
3633
{
37-
if (is_array($lookupValue)) {
38-
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $matchType);
39-
}
40-
4134
$lookupArray = Functions::flattenArray($lookupArray);
42-
$matchType = (int) ($matchType ?? self::MATCHTYPE_LARGEST_VALUE);
35+
$lookupValue = Functions::flattenSingleValue($lookupValue);
36+
$matchType = ($matchType === null)
37+
? self::MATCHTYPE_LARGEST_VALUE
38+
: (int) Functions::flattenSingleValue($matchType);
4339

4440
try {
4541
// Input validation

src/PhpSpreadsheet/Calculation/LookupRef/Lookup.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
66
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
77
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
88

99
class Lookup
1010
{
11-
use ArrayEnabled;
12-
1311
/**
1412
* LOOKUP
1513
* The LOOKUP function searches for value either from a one-row or one-column range or from an array.
@@ -22,9 +20,7 @@ class Lookup
2220
*/
2321
public static function lookup($lookupValue, $lookupVector, $resultVector = null)
2422
{
25-
if (is_array($lookupValue)) {
26-
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $lookupValue, $lookupVector, $resultVector);
27-
}
23+
$lookupValue = Functions::flattenSingleValue($lookupValue);
2824

2925
if (!is_array($lookupVector)) {
3026
return ExcelError::NA();

src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
65
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
77
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
88

99
class Matrix
1010
{
11-
use ArrayEnabled;
12-
1311
/**
1412
* TRANSPOSE.
1513
*
@@ -48,25 +46,17 @@ public static function transpose($matrixData)
4846
* @param mixed $matrix A range of cells or an array constant
4947
* @param mixed $rowNum The row in the array or range from which to return a value.
5048
* If row_num is omitted, column_num is required.
51-
* Or can be an array of values
5249
* @param mixed $columnNum The column in the array or range from which to return a value.
5350
* If column_num is omitted, row_num is required.
54-
* Or can be an array of values
5551
*
5652
* TODO Provide support for area_num, currently not supported
5753
*
5854
* @return mixed the value of a specified cell or array of cells
59-
* If an array of values is passed as the $rowNum and/or $columnNum arguments, then the returned result
60-
* will also be an array with the same dimensions
6155
*/
6256
public static function index($matrix, $rowNum = 0, $columnNum = 0)
6357
{
64-
if (is_array($rowNum) || is_array($columnNum)) {
65-
return self::evaluateArrayArgumentsSubsetFrom([self::class, __FUNCTION__], 1, $matrix, $rowNum, $columnNum);
66-
}
67-
68-
$rowNum = $rowNum ?? 0;
69-
$columnNum = $columnNum ?? 0;
58+
$rowNum = ($rowNum === null) ? 0 : Functions::flattenSingleValue($rowNum);
59+
$columnNum = ($columnNum === null) ? 0 : Functions::flattenSingleValue($columnNum);
7060

7161
try {
7262
$rowNum = LookupRefValidations::validatePositiveInt($rowNum);

src/PhpSpreadsheet/Calculation/LookupRef/Selection.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
65
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
87

98
class Selection
109
{
11-
use ArrayEnabled;
12-
1310
/**
1411
* CHOOSE.
1512
*
@@ -19,19 +16,18 @@ class Selection
1916
* Excel Function:
2017
* =CHOOSE(index_num, value1, [value2], ...)
2118
*
22-
* @param mixed $chosenEntry The entry to select from the list (indexed from 1)
2319
* @param mixed ...$chooseArgs Data values
2420
*
2521
* @return mixed The selected value
2622
*/
27-
public static function choose($chosenEntry, ...$chooseArgs)
23+
public static function choose(...$chooseArgs)
2824
{
29-
if (is_array($chosenEntry)) {
30-
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $chosenEntry, ...$chooseArgs);
31-
}
32-
25+
$chosenEntry = Functions::flattenArray(array_shift($chooseArgs));
3326
$entryCount = count($chooseArgs) - 1;
3427

28+
if (is_array($chosenEntry)) {
29+
$chosenEntry = array_shift($chosenEntry);
30+
}
3531
if (is_numeric($chosenEntry)) {
3632
--$chosenEntry;
3733
} else {

src/PhpSpreadsheet/Cell/DataValidator.php

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ private function isValueInList(Cell $cell)
6464

6565
try {
6666
$result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell);
67-
while (is_array($result)) {
68-
$result = array_pop($result);
69-
}
7067

7168
return $result !== ExcelError::NA();
7269
} catch (Exception $ex) {

src/PhpSpreadsheet/Worksheet/AutoFilter.php

-3
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,6 @@ public function showHideRows()
931931
$averageFormula = '=AVERAGE(' . $columnID . ($rangeStart[1] + 1) . ':' . $columnID . $rangeEnd[1] . ')';
932932
$spreadsheet = ($this->workSheet === null) ? null : $this->workSheet->getParent();
933933
$average = Calculation::getInstance($spreadsheet)->calculateFormula($averageFormula, null, $this->workSheet->getCell('A1'));
934-
while (is_array($average)) {
935-
$average = array_pop($average);
936-
}
937934
// Set above/below rule based on greaterThan or LessTan
938935
$operator = ($dynamicRuleType === Rule::AUTOFILTER_RULETYPE_DYNAMIC_ABOVEAVERAGE)
939936
? Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/AddressTest.php

-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
65
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
87
use PHPUnit\Framework\TestCase;
@@ -29,27 +28,4 @@ public function providerADDRESS(): array
2928
{
3029
return require 'tests/data/Calculation/LookupRef/ADDRESS.php';
3130
}
32-
33-
/**
34-
* @dataProvider providerAddressArray
35-
*/
36-
public function testAddressArray(array $expectedResult, string $argument1, string $argument2): void
37-
{
38-
$calculation = Calculation::getInstance();
39-
40-
$formula = "=ADDRESS({$argument1}, {$argument2}, 4)";
41-
$result = $calculation->_calculateFormulaValue($formula);
42-
self::assertEquals($expectedResult, $result);
43-
}
44-
45-
public function providerAddressArray(): array
46-
{
47-
return [
48-
'row/column vectors' => [
49-
[['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']],
50-
'{1; 2; 3}',
51-
'{1, 2, 3}',
52-
],
53-
];
54-
}
5531
}

0 commit comments

Comments
 (0)