Skip to content

Commit

Permalink
Add some types
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Oct 6, 2023
1 parent 25732b0 commit e50d6da
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/DataFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
29 changes: 14 additions & 15 deletions src/DataFrameCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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]);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 ';
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/IO/XLSX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e50d6da

Please sign in to comment.