Skip to content

Commit 069efdb

Browse files
authored
Merge pull request #3008 from PHPOffice/Expand-Return-Datatypes
More return type declarations, and some additional argument typehinting
2 parents bb072d1 + febd8d8 commit 069efdb

File tree

4 files changed

+52
-103
lines changed

4 files changed

+52
-103
lines changed

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class Cell
5050
private $dataType;
5151

5252
/**
53-
* Collection of cells.
53+
* The collection of cells that this cell belongs to (i.e. The Cell Collection for the parent Worksheet).
5454
*
5555
* @var Cells
5656
*/
5757
private $parent;
5858

5959
/**
60-
* Index to cellXf.
60+
* Index to the cellXf reference for the styling of this cell.
6161
*
6262
* @var int
6363
*/
@@ -95,9 +95,8 @@ public function attach(Cells $parent): void
9595
* Create a new Cell.
9696
*
9797
* @param mixed $value
98-
* @param string $dataType
9998
*/
100-
public function __construct($value, $dataType, Worksheet $worksheet)
99+
public function __construct($value, ?string $dataType, Worksheet $worksheet)
101100
{
102101
// Initialise cell value
103102
$this->value = $value;
@@ -111,7 +110,7 @@ public function __construct($value, $dataType, Worksheet $worksheet)
111110
$dataType = DataType::TYPE_STRING;
112111
}
113112
$this->dataType = $dataType;
114-
} elseif (!self::getValueBinder()->bindValue($this, $value)) {
113+
} elseif (self::getValueBinder()->bindValue($this, $value) === false) {
115114
throw new Exception('Value could not be bound to cell.');
116115
}
117116
}
@@ -167,10 +166,8 @@ public function getValue()
167166

168167
/**
169168
* Get cell value with formatting.
170-
*
171-
* @return string
172169
*/
173-
public function getFormattedValue()
170+
public function getFormattedValue(): string
174171
{
175172
return (string) NumberFormat::toFormattedString(
176173
$this->getCalculatedValue(),
@@ -188,7 +185,7 @@ public function getFormattedValue()
188185
*
189186
* @return $this
190187
*/
191-
public function setValue($value)
188+
public function setValue($value): self
192189
{
193190
if (!self::getValueBinder()->bindValue($this, $value)) {
194191
throw new Exception('Value could not be bound to cell.');
@@ -205,7 +202,7 @@ public function setValue($value)
205202
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this
206203
* method, then it is your responsibility as an end-user developer to validate that the value and
207204
* the datatype match.
208-
* If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype
205+
* If you do mismatch value and datatype, then the value you enter may be changed to match the datatype
209206
* that you specify.
210207
*
211208
* @return Cell
@@ -271,7 +268,7 @@ public function setValueExplicit($value, $dataType)
271268
*
272269
* @return mixed
273270
*/
274-
public function getCalculatedValue($resetLog = true)
271+
public function getCalculatedValue(bool $resetLog = true)
275272
{
276273
if ($this->dataType === DataType::TYPE_FORMULA) {
277274
try {
@@ -319,7 +316,7 @@ public function getCalculatedValue($resetLog = true)
319316
*
320317
* @return Cell
321318
*/
322-
public function setCalculatedValue($originalValue)
319+
public function setCalculatedValue($originalValue): self
323320
{
324321
if ($originalValue !== null) {
325322
$this->calculatedValue = (is_numeric($originalValue)) ? (float) $originalValue : $originalValue;
@@ -345,10 +342,8 @@ public function getOldCalculatedValue()
345342

346343
/**
347344
* Get cell data type.
348-
*
349-
* @return string
350345
*/
351-
public function getDataType()
346+
public function getDataType(): string
352347
{
353348
return $this->dataType;
354349
}
@@ -360,7 +355,7 @@ public function getDataType()
360355
*
361356
* @return Cell
362357
*/
363-
public function setDataType($dataType)
358+
public function setDataType($dataType): self
364359
{
365360
if ($dataType == DataType::TYPE_STRING2) {
366361
$dataType = DataType::TYPE_STRING;
@@ -392,10 +387,8 @@ public function hasDataValidation(): bool
392387

393388
/**
394389
* Get Data validation rules.
395-
*
396-
* @return DataValidation
397390
*/
398-
public function getDataValidation()
391+
public function getDataValidation(): DataValidation
399392
{
400393
if (!isset($this->parent)) {
401394
throw new Exception('Cannot get data validation for cell that is not bound to a worksheet');
@@ -420,10 +413,8 @@ public function setDataValidation(?DataValidation $dataValidation = null): self
420413

421414
/**
422415
* Does this cell contain valid value?
423-
*
424-
* @return bool
425416
*/
426-
public function hasValidValue()
417+
public function hasValidValue(): bool
427418
{
428419
$validator = new DataValidator();
429420

@@ -432,10 +423,8 @@ public function hasValidValue()
432423

433424
/**
434425
* Does this cell contain a Hyperlink?
435-
*
436-
* @return bool
437426
*/
438-
public function hasHyperlink()
427+
public function hasHyperlink(): bool
439428
{
440429
if (!isset($this->parent)) {
441430
throw new Exception('Cannot check for hyperlink when cell is not bound to a worksheet');
@@ -446,10 +435,8 @@ public function hasHyperlink()
446435

447436
/**
448437
* Get Hyperlink.
449-
*
450-
* @return Hyperlink
451438
*/
452-
public function getHyperlink()
439+
public function getHyperlink(): Hyperlink
453440
{
454441
if (!isset($this->parent)) {
455442
throw new Exception('Cannot get hyperlink for cell that is not bound to a worksheet');
@@ -463,7 +450,7 @@ public function getHyperlink()
463450
*
464451
* @return Cell
465452
*/
466-
public function setHyperlink(?Hyperlink $hyperlink = null)
453+
public function setHyperlink(?Hyperlink $hyperlink = null): self
467454
{
468455
if (!isset($this->parent)) {
469456
throw new Exception('Cannot set hyperlink for cell that is not bound to a worksheet');
@@ -486,10 +473,8 @@ public function getParent()
486473

487474
/**
488475
* Get parent worksheet.
489-
*
490-
* @return Worksheet
491476
*/
492-
public function getWorksheet()
477+
public function getWorksheet(): Worksheet
493478
{
494479
try {
495480
$worksheet = $this->parent->getParent();
@@ -506,27 +491,22 @@ public function getWorksheet()
506491

507492
/**
508493
* Is this cell in a merge range.
509-
*
510-
* @return bool
511494
*/
512-
public function isInMergeRange()
495+
public function isInMergeRange(): bool
513496
{
514497
return (bool) $this->getMergeRange();
515498
}
516499

517500
/**
518501
* Is this cell the master (top left cell) in a merge range (that holds the actual data value).
519-
*
520-
* @return bool
521502
*/
522-
public function isMergeRangeValueCell()
503+
public function isMergeRangeValueCell(): bool
523504
{
524505
if ($mergeRange = $this->getMergeRange()) {
525506
$mergeRange = Coordinate::splitRange($mergeRange);
526507
[$startCell] = $mergeRange[0];
527-
if ($this->getCoordinate() === $startCell) {
528-
return true;
529-
}
508+
509+
return $this->getCoordinate() === $startCell;
530510
}
531511

532512
return false;
@@ -579,7 +559,7 @@ public function getAppliedStyle(): Style
579559
*
580560
* @return Cell
581561
*/
582-
public function rebindParent(Worksheet $parent)
562+
public function rebindParent(Worksheet $parent): self
583563
{
584564
$this->parent = $parent->getCellCollection();
585565

@@ -590,10 +570,8 @@ public function rebindParent(Worksheet $parent)
590570
* Is cell in a specific range?
591571
*
592572
* @param string $range Cell range (e.g. A1:A1)
593-
*
594-
* @return bool
595573
*/
596-
public function isInRange($range)
574+
public function isInRange(string $range): bool
597575
{
598576
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range);
599577

@@ -614,7 +592,7 @@ public function isInRange($range)
614592
*
615593
* @return int Result of comparison (always -1 or 1, never zero!)
616594
*/
617-
public static function compareCells(self $a, self $b)
595+
public static function compareCells(self $a, self $b): int
618596
{
619597
if ($a->getRow() < $b->getRow()) {
620598
return -1;
@@ -629,10 +607,8 @@ public static function compareCells(self $a, self $b)
629607

630608
/**
631609
* Get value binder to use.
632-
*
633-
* @return IValueBinder
634610
*/
635-
public static function getValueBinder()
611+
public static function getValueBinder(): IValueBinder
636612
{
637613
if (self::$valueBinder === null) {
638614
self::$valueBinder = new DefaultValueBinder();
@@ -655,33 +631,29 @@ public static function setValueBinder(IValueBinder $binder): void
655631
public function __clone()
656632
{
657633
$vars = get_object_vars($this);
658-
foreach ($vars as $key => $value) {
659-
if ((is_object($value)) && ($key != 'parent')) {
660-
$this->$key = clone $value;
634+
foreach ($vars as $propertyName => $propertyValue) {
635+
if ((is_object($propertyValue)) && ($propertyName !== 'parent')) {
636+
$this->$propertyName = clone $propertyValue;
661637
} else {
662-
$this->$key = $value;
638+
$this->$propertyName = $propertyValue;
663639
}
664640
}
665641
}
666642

667643
/**
668644
* Get index to cellXf.
669-
*
670-
* @return int
671645
*/
672-
public function getXfIndex()
646+
public function getXfIndex(): int
673647
{
674648
return $this->xfIndex;
675649
}
676650

677651
/**
678652
* Set index to cellXf.
679653
*
680-
* @param int $indexValue
681-
*
682654
* @return Cell
683655
*/
684-
public function setXfIndex($indexValue)
656+
public function setXfIndex(int $indexValue): self
685657
{
686658
$this->xfIndex = $indexValue;
687659

@@ -695,7 +667,7 @@ public function setXfIndex($indexValue)
695667
*
696668
* @return $this
697669
*/
698-
public function setFormulaAttributes($attributes)
670+
public function setFormulaAttributes($attributes): self
699671
{
700672
$this->formulaAttributes = $attributes;
701673

src/PhpSpreadsheet/Collection/Cells.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ public function getParent()
9191
* Whether the collection holds a cell for the given coordinate.
9292
*
9393
* @param string $cellCoordinate Coordinate of the cell to check
94-
*
95-
* @return bool
9694
*/
97-
public function has($cellCoordinate)
95+
public function has($cellCoordinate): bool
9896
{
9997
return ($cellCoordinate === $this->currentCoordinate) || isset($this->index[$cellCoordinate]);
10098
}
@@ -103,10 +101,8 @@ public function has($cellCoordinate)
103101
* Add or update a cell in the collection.
104102
*
105103
* @param Cell $cell Cell to update
106-
*
107-
* @return Cell
108104
*/
109-
public function update(Cell $cell)
105+
public function update(Cell $cell): Cell
110106
{
111107
return $this->add($cell->getCoordinate(), $cell);
112108
}
@@ -165,10 +161,8 @@ public function getCurrentCoordinate()
165161

166162
/**
167163
* Return the column coordinate of the currently active cell object.
168-
*
169-
* @return string
170164
*/
171-
public function getCurrentColumn()
165+
public function getCurrentColumn(): string
172166
{
173167
sscanf($this->currentCoordinate ?? '', '%[A-Z]%d', $column, $row);
174168

@@ -177,10 +171,8 @@ public function getCurrentColumn()
177171

178172
/**
179173
* Return the row coordinate of the currently active cell object.
180-
*
181-
* @return int
182174
*/
183-
public function getCurrentRow()
175+
public function getCurrentRow(): int
184176
{
185177
sscanf($this->currentCoordinate ?? '', '%[A-Z]%d', $column, $row);
186178

src/PhpSpreadsheet/Collection/CellsFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ abstract class CellsFactory
1212
*
1313
* @param Worksheet $worksheet Enable cell caching for this worksheet
1414
*
15-
* @return Cells
1615
* */
17-
public static function getInstance(Worksheet $worksheet)
16+
public static function getInstance(Worksheet $worksheet): Cells
1817
{
1918
return new Cells($worksheet, Settings::getCache());
2019
}

0 commit comments

Comments
 (0)