-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass smarter PlaceholderTypeProvider from mysqli::execute_query
- Loading branch information
Showing
4 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/PHPStan/Helper/PHPStanTypeVarcharPlaceholderTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MariaStan\PHPStan\Helper; | ||
|
||
use MariaStan\Analyser\PlaceholderTypeProvider\PlaceholderTypeProvider; | ||
use MariaStan\Ast\Expr\Placeholder; | ||
use MariaStan\Schema\DbType\DbType; | ||
use MariaStan\Schema\DbType\EnumType; | ||
use MariaStan\Schema\DbType\NullType; | ||
use MariaStan\Schema\DbType\VarcharType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
use function array_map; | ||
use function array_unique; | ||
use function count; | ||
|
||
class PHPStanTypeVarcharPlaceholderTypeProvider implements PlaceholderTypeProvider | ||
{ | ||
/** @param array<int|string, \PHPStan\Type\Type> $placeholderPHPStanTypes */ | ||
public function __construct(private readonly array $placeholderPHPStanTypes) | ||
{ | ||
} | ||
|
||
public function getPlaceholderDbType(Placeholder $placeholder): DbType | ||
{ | ||
$phpstanType = $this->placeholderPHPStanTypes[$placeholder->name] ?? null; | ||
|
||
if ($phpstanType === null) { | ||
return new VarcharType(); | ||
} | ||
|
||
if ($phpstanType->isNull()->yes()) { | ||
return new NullType(); | ||
} | ||
|
||
$phpstanType = TypeCombinator::removeNull($phpstanType); | ||
$constantStrings = $phpstanType->toString()->getConstantStrings(); | ||
|
||
if (count($constantStrings) === 0) { | ||
return new VarcharType(); | ||
} | ||
|
||
$values = array_unique(array_map(static fn (ConstantStringType $t) => $t->getValue(), $constantStrings)); | ||
|
||
return new EnumType($values); | ||
} | ||
|
||
public function isPlaceholderNullable(Placeholder $placeholder): bool | ||
{ | ||
$phpstanType = $this->placeholderPHPStanTypes[$placeholder->name] ?? null; | ||
|
||
if ($phpstanType === null) { | ||
return true; | ||
} | ||
|
||
return ! $phpstanType->isNull()->no(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters