diff --git a/src/DataFrame.php b/src/DataFrame.php index 4840c00..414bcbf 100644 --- a/src/DataFrame.php +++ b/src/DataFrame.php @@ -38,7 +38,7 @@ final class DataFrame extends DataFrameCore * @return DataFrame * @since 0.1.0 */ - public static function fromCSV($fileName, $options = []): self + public static function fromCSV(string $fileName, array $options = []): self { $csv = new CSV($fileName); $data = $csv->loadFile($options); @@ -54,7 +54,7 @@ public static function fromCSV($fileName, $options = []): self * @throws \CondorcetPHP\Oliphant\Exceptions\FileExistsException * @since 0.1.0 */ - public function toCSV($fileName, $options = []): self + public function toCSV(string $fileName, array $options = []): self { $csv = new CSV($fileName); $csv->saveFile($this->data, $options); @@ -70,7 +70,7 @@ public function toCSV($fileName, $options = []): self * @return DataFrame * @since 0.1.0 */ - public static function fromFWF($fileName, array $colSpecs, array $options = []): self + public static function fromFWF(string $fileName, array $colSpecs, array $options = []): self { $fwf = new FWF($fileName); $data = $fwf->loadFile($colSpecs, $options); @@ -98,7 +98,7 @@ public static function fromXLSX($fileName, array $options = []): self * @param $worksheetTitle * @since 0.3.0 */ - public function toXLSXWorksheet(Spreadsheet &$excel, $worksheetTitle): Worksheet + public function toXLSXWorksheet(Spreadsheet &$excel, string $worksheetTitle): Worksheet { return XLSX::saveToWorksheet($excel, $worksheetTitle, $this->data, $this->columns); } @@ -110,7 +110,7 @@ public function toXLSXWorksheet(Spreadsheet &$excel, $worksheetTitle): Worksheet * @return DataFrame * @since 0.3.0 */ - public static function fromSQL($sqlQuery, PDO $pdo): self + public static function fromSQL(string $sqlQuery, PDO $pdo): self { $sql = new SQL($pdo); $data = $sql->select($sqlQuery); diff --git a/src/DataFrameCore.php b/src/DataFrameCore.php index 58e9815..ad39205 100644 --- a/src/DataFrameCore.php +++ b/src/DataFrameCore.php @@ -120,7 +120,7 @@ public function apply(Closure $f): self * @return DataFrameCore * @since 0.1.0 */ - public function applyIndexMap(array $map, mixed $column = null) + public function applyIndexMap(array $map, ?string $column = null) { return $this->apply(static function (&$row, $i) use ($map, $column) { if (isset($map[$i])) { @@ -214,7 +214,7 @@ public function query(string $sql, ?PDO $pdo = null): self * @throws InvalidColumnException * @since 0.1.0 */ - public function mustHaveColumn(mixed $columnName): void + public function mustHaveColumn(string $columnName): void { if ($this->hasColumn($columnName) === false) { throw new InvalidColumnException("{$columnName} doesn't exist in DataFrame"); @@ -228,7 +228,7 @@ public function mustHaveColumn(mixed $columnName): void * @return bool * @since 0.1.0 */ - public function hasColumn(mixed $columnName): bool + public function hasColumn(string $columnName): bool { if (array_search($columnName, $this->columns, true) === false) { return false; @@ -244,7 +244,7 @@ public function hasColumn(mixed $columnName): bool * @param $columnName * @since 0.1.0 */ - private function addColumn($columnName): void + private function addColumn(string $columnName): void { if (!$this->hasColumn($columnName)) { $this->columns[] = $columnName; @@ -274,7 +274,7 @@ private function addColumns(array $columnNames): void * @param $from * @param $to */ - public function renameColumn($from, $to): void + public function renameColumn(string $from, string $to): void { $this->mustHaveColumn($from); @@ -298,7 +298,7 @@ public function renameColumn($from, $to): void * @param $columnName * @since 0.1.0 */ - public function removeColumn($columnName): void + public function removeColumn(string $columnName): void { unset($this[$columnName]); } @@ -360,7 +360,7 @@ public function preg_replace($pattern, $replacement): self * @param null|string $toDateFormat The date format of the output. * @throws Exception */ - public function convertTypes(array $typeMap, $fromDateFormat = null, $toDateFormat = null): void + public function convertTypes(array $typeMap, array|string|null $fromDateFormat = null, ?string $toDateFormat = null): void { foreach ($this as $i => $row) { foreach ($typeMap as $column => $type) { @@ -379,7 +379,7 @@ public function convertTypes(array $typeMap, $fromDateFormat = null, $toDateForm } } - private function convertNumeric($value): int|float + private function convertNumeric(mixed $value): int|float { if (is_numeric($value)) { return $value; @@ -396,7 +396,7 @@ private function convertNumeric($value): int|float return (\is_int($value / 1)) ? \intval($value) : $value; } - private function convertInt($value): int + private function convertInt(mixed $value): int { if (empty($value)) { return 0; @@ -413,7 +413,7 @@ private function convertInt($value): int return \intval(str_replace(',', '', $value)); } - private function convertDatetime($value, $fromFormat, $toFormat): string + private function convertDatetime(mixed $value, array|string|null $fromFormat, string $toFormat): string { if (empty($value)) { return DateTime::createFromFormat('Y-m-d', '0001-01-01')->format($toFormat); @@ -529,7 +529,7 @@ public function sortValues(array|string $by, bool $ascending = true): void $by = [$by]; } - usort($this->data, static function ($row_a, $row_b) use ($by, $ascending): int { + usort($this->data, static function (array $row_a, array $row_b) use ($by, $ascending): int { foreach ($by as $col) { if ($row_a[$col] > $row_b[$col]) { return $ascending ? 1 : -1; @@ -609,7 +609,7 @@ public function offsetGet(mixed $columnName): mixed * @throws DataFrameException * @since 0.1.0 */ - public function offsetSet($targetColumn, $rightHandSide): void + public function offsetSet(mixed $targetColumn, mixed $rightHandSide): void { if ($rightHandSide instanceof DataFrame) { $this->offsetSetDataFrame($targetColumn, $rightHandSide); @@ -631,7 +631,7 @@ public function offsetSet($targetColumn, $rightHandSide): void * @throws DataFrameException * @since 0.1.0 */ - private function offsetSetDataFrame($targetColumn, DataFrame $df): void + private function offsetSetDataFrame(string $targetColumn, DataFrame $df): void { if (\count($df->columns()) !== 1) { $msg = 'Can only set a new column from a DataFrame with a single '; @@ -664,7 +664,7 @@ private function offsetSetDataFrame($targetColumn, DataFrame $df): void * @param Closure $f * @since 0.1.0 */ - private function offsetSetClosure($targetColumn, Closure $f): void + private function offsetSetClosure(string $targetColumn, Closure $f): void { foreach ($this as $i => $row) { $this->data[$i][$targetColumn] = $f($row[$targetColumn]); @@ -691,7 +691,6 @@ private function offsetSetValue(?string $targetColumn, mixed $value): void $this->data[$i][$targetColumn] = $value; } } elseif (\is_array($value)) { - $this->addColumns(array_keys($value)); $this->data[] = $value; } } diff --git a/src/IO/XLSX.php b/src/IO/XLSX.php index 6a2a20c..154946a 100644 --- a/src/IO/XLSX.php +++ b/src/IO/XLSX.php @@ -96,7 +96,7 @@ public function loadFile(array $options): array * @throws \PhpOffice\PhpSpreadsheet\Exception * @since 0.3.0 */ - public static function saveToWorksheet(Spreadsheet &$excel, $worksheetTitle, array $data, array $columns): Worksheet + public static function saveToWorksheet(Spreadsheet &$excel, string $worksheetTitle, array $data, array $columns): Worksheet { // Check if this is a brand new spreadsheet if ($excel->getSheetCount() === 1) {