Skip to content

Commit

Permalink
Typed conditional returns for non-empty-array typed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
devnix committed May 14, 2024
1 parent 7db3162 commit 880220a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Portability/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,27 @@ public function __construct(bool $convertEmptyStringToNull, bool $rightTrimStrin
}

/**
* @param non-empty-list<mixed>|false $row
* @param list<mixed>|false $row
*
* @return non-empty-list<mixed>|false
* @return list<mixed>|false
* @psalm-return (
* $row is non-empty-list<mixed> ? non-empty-list<mixed> :
* ($row is list<mixed> ? list<mixed> : false)
* )
*/
public function convertNumeric(array|false $row): array|false
{
return ($this->convertNumeric)($row);
}

/**
* @param non-empty-array<string,mixed>|false $row
* @param array<string, mixed>|false $row
*
* @return non-empty-array<string, mixed>|false
* @return array<string, mixed>|false
* @psalm-return (
* $row is non-empty-array<string, mixed> ? non-empty-array<string, mixed> :
* ($row is array<string, mixed> ? array<string, mixed> : false)
* )
*/
public function convertAssociative(array|false $row): array|false
{
Expand All @@ -76,19 +84,29 @@ public function convertOne(mixed $value): mixed
}

/**
* @param list<non-empty-list<mixed>> $data
* @param list<list<mixed>> $data
*
* @return list<non-empty-list<mixed>>
* @return list<list<mixed>>
* @psalm-return (
* $data is list<non-empty-list<mixed>>
* ? list<non-empty-list<mixed>>
* : list<list<mixed>>
* )
*/
public function convertAllNumeric(array $data): array
{
return ($this->convertAllNumeric)($data);
}

/**
* @param list<non-empty-array<string,mixed>> $data
* @param list<array<string,mixed>> $data
*
* @return list<non-empty-array<string,mixed>>
* @return list<array<string,mixed>>
* @psalm-return (
* $data is list<non-empty-array<string,mixed>>
* ? list<non-empty-array<string,mixed>>
* : list<array<string,mixed>>
* )
*/
public function convertAllAssociative(array $data): array
{
Expand Down
69 changes: 69 additions & 0 deletions static-analysis/result-converter-dependant-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Doctrine\StaticAnalysis\DBAL;

use Doctrine\DBAL\Portability\Converter;

/** @return false */
function convertNumericFalse(Converter $converter): bool
{
return $converter->convertNumeric(false);
}

/** @return list<mixed> */
function convertNumericEmptyArray(Converter $converter): array
{
return $converter->convertNumeric([]);
}

/** @return non-empty-list<mixed> */
function convertNumericNonEmptyArray(Converter $converter): array
{
return $converter->convertNumeric(['foo']);
}

/** @return false */
function convertAssociativeFalse(Converter $converter): bool
{
return $converter->convertAssociative(false);
}

/** @return array<string, mixed> */
function convertAssociativeEmptyArray(Converter $converter): array
{
return $converter->convertAssociative([]);
}

/** @return non-empty-array<string, mixed> */
function convertAssociativeNonEmptyArray(Converter $converter): array
{
return $converter->convertAssociative(['foo' => 'bar']);
}

/** @return list<list<mixed>> */
function convertAllNumericEmptyArray(Converter $converter): array
{
return $converter->convertAllNumeric([[]]);
}

/** @return list<non-empty-list<mixed>> */
function convertAllNumericNonEmptyArray(Converter $converter): array
{
return $converter->convertAllNumeric([['foo']]);
}


/** @return list<array<string, mixed>> */
function convertAllAssociativeEmptyArray(Converter $converter): array
{
return $converter->convertAllAssociative([[]]);
}

/** @return list<non-empty-array<string, mixed>> */
function convertAllAssociativeNonEmptyArray(Converter $converter): array
{
return $converter->convertAllAssociative([['foo' => 'bar']]);
}

0 comments on commit 880220a

Please sign in to comment.