Skip to content

Commit

Permalink
Remove all mixed in return type where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Jan 22, 2024
1 parent 936805a commit 013e5cc
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 38 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ parameters:
parallel:
processTimeout: 300.0
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
# Accept a bit anything for assert methods
- '~^Parameter \#2 .* of static method PHPUnit\\Framework\\Assert\:\:assert\w+\(\) expects .*, .* given\.$~'
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static function getLocaleBoolean(string $index): string
* Excel constant string translations to their PHP equivalents
* Constant conversion from text name/value to actual (datatyped) value.
*
* @var array<string, mixed>
* @var array<string, null|bool>
*/
private static array $excelConstants = [
'TRUE' => true,
Expand All @@ -208,7 +208,7 @@ public static function keyInExcelConstants(string $key): bool
return array_key_exists($key, self::$excelConstants);
}

public static function getExcelConstants(string $key): mixed
public static function getExcelConstants(string $key): bool|null
{
return self::$excelConstants[$key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static function executeQuery(array $database, string $query, array $crit
return $database;
}

private static function processCondition(string $criterion, array $fields, array $dataValues, string $conditions): mixed
private static function processCondition(string $criterion, array $fields, array $dataValues, string $conditions): string
{
$key = array_search($criterion, $fields, true);

Expand Down
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use DateTime;
use DateTimeImmutable;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;

Expand All @@ -21,10 +22,10 @@ class Current
* Excel Function:
* TODAY()
*
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
*/
public static function today(): mixed
public static function today(): DateTime|float|int|string
{
$dti = new DateTimeImmutable();
$dateArray = Helpers::dateParse($dti->format('c'));
Expand All @@ -46,10 +47,10 @@ public static function today(): mixed
* Excel Function:
* NOW()
*
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
*/
public static function now(): mixed
public static function now(): DateTime|float|int|string
{
$dti = new DateTimeImmutable();
$dateArray = Helpers::dateParse($dti->format('c'));
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
Expand Down Expand Up @@ -58,12 +59,12 @@ class Date
* example, DATE(2008,1,-15) returns the serial number representing
* December 16, 2007.
*
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return array|DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
* If an array of numbers is passed as the argument, then the returned result will also be an array
* with the same dimensions
*/
public static function fromYMD(array|float|int|string $year, array|float|int|string $month, array|float|int|string $day): mixed
public static function fromYMD(array|float|int|string $year, array|float|int|string $month, array|float|int|string $day): float|int|DateTime|string|array
{
if (is_array($year) || is_array($month) || is_array($day)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $year, $month, $day);
Expand Down
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use DateTime;
use DateTimeImmutable;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
Expand Down Expand Up @@ -34,12 +35,12 @@ class DateValue
* #VALUE! error value if date_text is out of this range.
* Or can be an array of date values
*
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return array|DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
* If an array of numbers is passed as the argument, then the returned result will also be an array
* with the same dimensions
*/
public static function fromString(null|array|string|int|bool|float $dateValue): mixed
public static function fromString(null|array|string|int|bool|float $dateValue): array|string|float|int|DateTime
{
if (is_array($dateValue)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
Expand Down Expand Up @@ -131,10 +132,10 @@ private static function setUpArray(string $dateValue, DateTimeImmutable $dti): a
/**
* Final results.
*
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
*/
private static function finalResults(array $PHPDateArray, DateTimeImmutable $dti, int $baseYear): mixed
private static function finalResults(array $PHPDateArray, DateTimeImmutable $dti, int $baseYear): string|float|int|DateTime
{
$retValue = ExcelError::Value();
if (Helpers::dateParseSucceeded($PHPDateArray)) {
Expand Down
7 changes: 5 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public static function getDateValue(mixed $dateValue, bool $allowBool = true): f
/**
* getTimeValue.
*
* @return mixed Excel date/time serial value, or string if error
* @return float|string Excel date/time serial value, or string if error
*/
public static function getTimeValue(string $timeValue): mixed
public static function getTimeValue(string $timeValue): string|float
{
$saveReturnDateType = Functions::getReturnDateType();
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
/** @var float|string $timeValue */
$timeValue = TimeValue::fromString($timeValue);
Functions::setReturnDateType($saveReturnDateType);

Expand Down Expand Up @@ -238,6 +239,8 @@ public static function validateNumericNull(mixed $number): int|float

/**
* Many functions accept null/false/true argument treated as 0/0/1.
*
* @phpstan-assert float $number
*/
public static function validateNotNegative(mixed $number): float
{
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public static function adjust(mixed $dateValue, array|string|bool|float|int $adj
* a negative value yields a past date.
* Or can be an array of adjustment values
*
* @return array|mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return array|DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
* If an array of values is passed as the argument, then the returned result will also be an array
* with the same dimensions
*/
public static function lastDay(mixed $dateValue, array|float|int|bool|string $adjustmentMonths): mixed
public static function lastDay(mixed $dateValue, array|float|int|bool|string $adjustmentMonths): array|string|DateTime|float|int
{
if (is_array($dateValue) || is_array($adjustmentMonths)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $dateValue, $adjustmentMonths);
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/WorkDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class WorkDay
* Or can be an array of int values
* @param null|mixed $dateArgs An array of dates (such as holidays) to exclude from the calculation
*
* @return array|mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* @return array|DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function date(mixed $startDate, array|int|string $endDays, mixed ...$dateArgs): mixed
public static function date(mixed $startDate, array|int|string $endDays, mixed ...$dateArgs): array|float|int|DateTime|string
{
if (is_array($startDate) || is_array($endDays)) {
return self::evaluateArrayArgumentsSubset(
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function flattenArrayIndexed($array): array
*
* @param mixed $value Array or scalar value
*/
public static function flattenSingleValue(mixed $value = ''): mixed
public static function flattenSingleValue(mixed $value): mixed
{
while (is_array($value)) {
$value = array_shift($value);
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/LookupRef/Hyperlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Hyperlink
* @param mixed $displayName Expect string. Value to return when testValue is an error condition
* @param ?Cell $cell The cell to set the hyperlink in
*
* @return mixed The value of $displayName (or $linkURL if $displayName was blank)
* @return string The value of $displayName (or $linkURL if $displayName was blank)
*/
public static function set(mixed $linkURL = '', mixed $displayName = null, ?Cell $cell = null): mixed
public static function set(mixed $linkURL = '', mixed $displayName = null, ?Cell $cell = null): string
{
$linkURL = ($linkURL === null) ? '' : Functions::flattenSingleValue($linkURL);
$displayName = ($displayName === null) ? '' : Functions::flattenSingleValue($displayName);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Statistical/MaxMinBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

abstract class MaxMinBase
{
protected static function datatypeAdjustmentAllowStrings(mixed $value): mixed
protected static function datatypeAdjustmentAllowStrings(int|float|string|bool $value): int|float
{
if (is_bool($value)) {
return (int) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

abstract class VarianceBase
{
protected static function datatypeAdjustmentAllowStrings(mixed $value): mixed
protected static function datatypeAdjustmentAllowStrings(int|float|string|bool $value): int|float
{
if (is_bool($value)) {
return (int) $value;
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Calculation/TextData/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static function TEXTFORMAT(mixed $value, mixed $format): array|string
$format = Helpers::extractString($format);

if (!is_numeric($value) && Date::isDateTimeFormatCode($format)) {
// @phpstan-ignore-next-line
$value = DateTimeExcel\DateValue::fromString($value) + DateTimeExcel\TimeValue::fromString($value);
}

Expand Down
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Cell/AddressRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PhpOffice\PhpSpreadsheet\Cell;

/**
* @template T
*/
interface AddressRange
{
public const MAX_ROW = 1048576;
Expand All @@ -10,8 +13,14 @@ interface AddressRange

public const MAX_COLUMN_INT = 16384;

/**
* @return T
*/
public function from(): mixed;

/**
* @return T
*/
public function to(): mixed;

public function __toString(): string;
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Cell/CellRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Stringable;

/**
* @implements AddressRange<CellAddress>
*/
class CellRange implements AddressRange, Stringable
{
protected CellAddress $from;
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Cell/ColumnRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Stringable;

/**
* @implements AddressRange<string>
*/
class ColumnRange implements AddressRange, Stringable
{
protected ?Worksheet $worksheet;
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Cell/RowRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Stringable;

/**
* @implements AddressRange<int>
*/
class RowRange implements AddressRange, Stringable
{
protected ?Worksheet $worksheet;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Shared/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,9 @@ public static function SYLKtoUTF8(string $textValue): string
* Retrieve any leading numeric part of a string, or return the full string if no leading numeric
* (handles basic integer or float, but not exponent or non decimal).
*
* @return mixed string or only the leading numeric part of the string
* @return float|string string or only the leading numeric part of the string
*/
public static function testStringAsNumeric(string $textValue): mixed
public static function testStringAsNumeric(string $textValue): float|string
{
if (is_numeric($textValue)) {
return $textValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ConditionalFormatValueObject

private null|float|int|string $value;

private mixed $cellFormula;
private ?string $cellFormula;

public function __construct(string $type, null|float|int|string $value = null, mixed $cellFormula = null)
public function __construct(string $type, null|float|int|string $value = null, ?string $cellFormula = null)
{
$this->type = $type;
$this->value = $value;
Expand Down Expand Up @@ -41,12 +41,12 @@ public function setValue(null|float|int|string $value): self
return $this;
}

public function getCellFormula(): mixed
public function getCellFormula(): ?string
{
return $this->cellFormula;
}

public function setCellFormula(mixed $cellFormula): self
public function setCellFormula(?string $cellFormula): self
{
$this->cellFormula = $cellFormula;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBar
}
}

public function getId(): mixed
public function getId(): string
{
return $this->id;
}

public function setId(mixed $id): self
public function setId(string $id): self
{
$this->id = $id;

Expand Down
12 changes: 6 additions & 6 deletions src/PhpSpreadsheet/Writer/Xls/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1246,9 +1246,9 @@ private function parenthesizedExpression(): array
* It parses a term. It assumes the following rule:
* Term -> Fact [("*" | "/") Fact].
*
* @return mixed The parsed ptg'd tree on success
* @return array The parsed ptg'd tree on success
*/
private function term(): mixed
private function term(): array
{
$result = $this->fact();
while (
Expand Down Expand Up @@ -1277,9 +1277,9 @@ private function term(): mixed
* | Number
* | Function.
*
* @return mixed The parsed ptg'd tree on success
* @return array The parsed ptg'd tree on success
*/
private function fact(): mixed
private function fact(): array
{
$currentToken = $this->currentToken;
if ($currentToken === '(') {
Expand Down Expand Up @@ -1376,9 +1376,9 @@ private function fact(): mixed
* It parses a function call. It assumes the following rule:
* Func -> ( Expr [,Expr]* ).
*
* @return mixed The parsed ptg'd tree on success
* @return array The parsed ptg'd tree on success
*/
private function func(): mixed
private function func(): array
{
$num_args = 0; // number of arguments received
$function = strtoupper($this->currentToken);
Expand Down

0 comments on commit 013e5cc

Please sign in to comment.