Skip to content

Commit

Permalink
feature #210 Strong typed function params and return types (michnovka)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x-dev branch.

Discussion
----------

Strong typed function params and return types

Ive reviewed bridges and added typed return types and params

Commits
-------

58e0449 Add return types and param types to bridge functions. Minor phpdoc fixes Do not use deprecated getVarcharTypeDeclarationSQL if newer getStringTypeDeclarationSQL is present
  • Loading branch information
ogizanagi committed Dec 15, 2022
2 parents dce599d + 58e0449 commit d6d138b
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include .make/help.mk
include .make/text.mk

PHP_CS_FIXER_VERSION=v3.9.4
PHP_CS_FIXER_VERSION=v3.13.0

###########
# Install #
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Common/AbstractTypesDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
abstract class AbstractTypesDumper
{
public function dumpToFile(string $file, array $types)
public function dumpToFile(string $file, array $types): void
{
file_put_contents($file, $this->dump($types));
}
Expand Down
22 changes: 10 additions & 12 deletions src/Bridge/Doctrine/DBAL/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ protected function onNullFromPhp(): int|string|null
* {@inheritdoc}
*
* @param \BackedEnum|null $value
*
* @return mixed
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform): string|int|null
{
if ($value !== null && !$value instanceof \BackedEnum) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -72,10 +70,8 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
* {@inheritdoc}
*
* @param int|string|null $value The value to convert.
*
* @return \BackedEnum|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform): ?\BackedEnum
{
if (null === $value) {
return $this->onNullFromDatabase();
Expand All @@ -87,11 +83,15 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $this->isIntBackedEnum()
? $platform->getIntegerTypeDeclarationSQL($fieldDeclaration)
: $platform->getVarcharTypeDeclarationSQL($fieldDeclaration)
? $platform->getIntegerTypeDeclarationSQL($column)
: (
method_exists($platform, 'getStringTypeDeclarationSQL') ?
$platform->getStringTypeDeclarationSQL($column) :
$platform->getVarcharTypeDeclarationSQL($column)
)
;
}

Expand All @@ -113,10 +113,8 @@ public function getBindingType(): int

/**
* Cast the value from database to proper enumeration internal type.
*
* @param int|string $value
*/
private function cast($value): int|string
private function cast(int|string $value): int|string
{
return $this->isIntBackedEnum() ? (int) $value : (string) $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

class DoctrineDBALTypesPass implements CompilerPassInterface
{
/** @var string */
private $typesFilePath;
private string $typesFilePath;

public function __construct(string $typesFilePath)
{
$this->typesFilePath = $typesFilePath;
}

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('.elao_enum.doctrine_types')) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

class DoctrineODMTypesPass implements CompilerPassInterface
{
/** @var string */
private $typesFilePath;
private string $typesFilePath;

public function __construct(string $typesFilePath)
{
$this->typesFilePath = $typesFilePath;
}

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('.elao_enum.doctrine_mongodb_types')) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getConfigTreeBuilder(): TreeBuilder
return $treeBuilder;
}

private function addDoctrineDbalSection(ArrayNodeDefinition $rootNode)
private function addDoctrineDbalSection(ArrayNodeDefinition $rootNode): void
{
$rootNode->children()
->arrayNode('doctrine')
Expand Down Expand Up @@ -97,7 +97,7 @@ private function addDoctrineDbalSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addDoctrineOdmSection(ArrayNodeDefinition $rootNode)
private function addDoctrineOdmSection(ArrayNodeDefinition $rootNode): void
{
$rootNode->children()
->arrayNode('doctrine_mongodb')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class ElaoEnumExtension extends Extension implements PrependExtensionInterface
{
public function prepend(ContainerBuilder $container)
public function prepend(ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.php');
Expand All @@ -42,7 +42,7 @@ public function prepend(ContainerBuilder $container)
}
}

public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);

Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Bundle/ElaoEnumBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class ElaoEnumBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractFlagBagTransformer implements DataTransformerInterface
/** @var class-string<\BackedEnum> */
protected string $enumType;

/** @var class-string<\BackedEnum> */
/** @param class-string<\BackedEnum> $enumType */
public function __construct(string $enumType)
{
if (!is_a($enumType, \BackedEnum::class, true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Form/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EnumType extends AbstractType
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
// Override the original type logic for label for readable enums.
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/Form/Type/FlagBagType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class FlagBagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (!$options['multiple']) {
throw new InvalidConfigurationException(sprintf(
Expand All @@ -38,7 +38,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('multiple', true)
Expand Down
1 change: 1 addition & 0 deletions src/FlagBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* A bag of {@see \BackedEnum} allowing to perform bit operations.
*
* @final
*
* @template T of \BackedEnum
*/
class FlagBag
Expand Down
1 change: 1 addition & 0 deletions src/ReadableEnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface ReadableEnumInterface extends \UnitEnum
* Gets human representations per enum cases.
*
* @return iterable labels indexed by enum cases
*
* @psalm-return iterable<ReadableEnumInterface, string>
*/
public static function readables(): iterable;
Expand Down

0 comments on commit d6d138b

Please sign in to comment.