Skip to content

Commit

Permalink
Add property typehints, use arrow functions, remove deprecated DBAL f…
Browse files Browse the repository at this point in the history
…eatures
  • Loading branch information
emodric committed Mar 4, 2022
1 parent 26e48fe commit 0e0af72
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 109 deletions.
22 changes: 6 additions & 16 deletions bundle/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Netgen\Bundle\EnhancedSelectionBundle\Command;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Driver\Result;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Type as EnhancedSelectionType;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -17,20 +16,11 @@

final class MigrateCommand extends Command
{
/**
* @var \Doctrine\DBAL\Connection
*/
private $db;
private Connection $db;

/**
* @var string
*/
private $typeIdentifier;
private string $typeIdentifier;

/**
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $io;
private SymfonyStyle $io;

public function __construct(Connection $db, EnhancedSelectionType $type)
{
Expand All @@ -56,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$statement = $this->getFields();
$this->io->progressStart($statement->rowCount());

while ($row = $statement->fetch(FetchMode::ASSOCIATIVE)) {
while ($row = $statement->fetchAssociative()) {
if ($row['data_text'] !== null) {
$fieldId = (int) $row['id'];
$version = (int) $row['version'];
Expand All @@ -79,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
return 0;
}

private function getFields(): Statement
private function getFields(): Result
{
$builder = $this->db->createQueryBuilder();
$builder->select('a.id', 'a.version', 'a.data_text')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,17 @@ abstract class Gateway extends StorageGateway
{
/**
* Stores the identifiers in the database based on the given field data.
*
* @param \Ibexa\Contracts\Core\Persistence\Content\VersionInfo $versionInfo
* @param \Ibexa\Contracts\Core\Persistence\Content\Field $field
*/
abstract public function storeFieldData(VersionInfo $versionInfo, Field $field): void;

/**
* Gets the identifiers stored in the field.
*
* @param \Ibexa\Contracts\Core\Persistence\Content\VersionInfo $versionInfo
* @param \Ibexa\Contracts\Core\Persistence\Content\Field $field
*/
abstract public function getFieldData(VersionInfo $versionInfo, Field $field): void;

/**
* Deletes field data for all $fieldIds in the version identified by
* $versionInfo.
*
* @param \Ibexa\Contracts\Core\Persistence\Content\VersionInfo $versionInfo
* @param array $fieldIds
*/
abstract public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage\Gateway;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Types\Types;
use Ibexa\Contracts\Core\Persistence\Content\Field;
use Ibexa\Contracts\Core\Persistence\Content\VersionInfo;
Expand All @@ -14,10 +13,7 @@

final class DoctrineStorage extends Gateway
{
/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;
private Connection $connection;

public function __construct(Connection $connection)
{
Expand Down Expand Up @@ -56,7 +52,7 @@ public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds): void
$query
->delete($this->connection->quoteIdentifier('sckenhancedselection'))
->where(
$query->expr()->andX(
$query->expr()->and(
$query->expr()->in('contentobject_attribute_id', [':contentobject_attribute_id']),
$query->expr()->eq('contentobject_attribute_version', ':contentobject_attribute_version')
)
Expand All @@ -70,19 +66,16 @@ public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds): void
/**
* Returns the data for the given $fieldId and $versionNo.
*
* @param mixed $fieldId
* @param mixed $versionNo
*
* @return array
* @return string[]
*/
private function loadFieldData($fieldId, $versionNo): array
private function loadFieldData(int $fieldId, int $versionNo): array
{
$query = $this->connection->createQueryBuilder();
$query
->select('DISTINCT identifier')
->from($this->connection->quoteIdentifier('sckenhancedselection'))
->where(
$query->expr()->andX(
$query->expr()->and(
$query->expr()->eq('contentobject_attribute_id', ':contentobject_attribute_id'),
$query->expr()->eq('contentobject_attribute_version', ':contentobject_attribute_version')
)
Expand All @@ -92,12 +85,10 @@ private function loadFieldData($fieldId, $versionNo): array

$statement = $query->execute();

$rows = $statement->fetchAll(FetchMode::ASSOCIATIVE);
$rows = $statement->fetchAllAssociative();

return array_map(
static function (array $row) {
return $row['identifier'];
},
static fn (array $row): string => $row['identifier'],
$rows
);
}
Expand Down
2 changes: 1 addition & 1 deletion bundle/Core/FieldType/EnhancedSelection/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class Type extends FieldType
{
/**
* @var array
* @var array<string, array<string, mixed>>
*/
protected $settingsSchema = [
'options' => [
Expand Down
2 changes: 1 addition & 1 deletion bundle/Core/FieldType/EnhancedSelection/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Value extends BaseValue
*
* @var string[]
*/
public $identifiers = [];
public array $identifiers = [];

/**
* @param string[] $identifiers
Expand Down
5 changes: 1 addition & 4 deletions bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

final class EnhancedSelectionFieldType extends AbstractType
{
/**
* @var \Ibexa\Contracts\Core\Repository\FieldTypeService
*/
private $fieldTypeService;
private FieldTypeService $fieldTypeService;

public function __construct(FieldTypeService $fieldTypeService)
{
Expand Down
12 changes: 3 additions & 9 deletions bundle/Form/Type/FieldType/FieldValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@

final class FieldValueTransformer implements DataTransformerInterface
{
/**
* @var \Ibexa\Contracts\Core\Repository\FieldType
*/
private $fieldType;

/**
* @var \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition
*/
private $fieldDefinition;
private FieldType $fieldType;

private FieldDefinition $fieldDefinition;

public function __construct(FieldType $fieldType, FieldDefinition $fieldDefinition)
{
Expand Down
5 changes: 1 addition & 4 deletions bundle/Installer/BuildSchemaListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

final class BuildSchemaListener implements EventSubscriberInterface
{
/**
* @var string
*/
private $schemaPath;
private string $schemaPath;

public function __construct(string $schemaPath)
{
Expand Down
5 changes: 1 addition & 4 deletions bundle/Templating/Twig/NetgenEnhancedSelectionRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

final class NetgenEnhancedSelectionRuntime
{
/**
* @var \Ibexa\Contracts\Core\Repository\ContentTypeService
*/
private $contentTypeService;
private ContentTypeService $contentTypeService;

public function __construct(ContentTypeService $contentTypeService)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
use Ibexa\Contracts\Core\Persistence\Content\VersionInfo;
use Ibexa\Core\FieldType\StorageGateway;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class EnhancedSelectionStorageTest extends TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $gateway;

/**
* @var EnhancedSelectionStorage
*/
private $storage;
private MockObject $gateway;

private EnhancedSelectionStorage $storage;

protected function setUp(): void
{
Expand Down
19 changes: 5 additions & 14 deletions tests/Core/FieldType/EnhancedSelection/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@

final class TypeTest extends TestCase
{
/**
* @var Type
*/
private $type;
private Type $type;

/**
* @var array
* @var string[]
*/
private $identifiers = ['identifier0', 'identifier1'];
private array $identifiers = ['identifier0', 'identifier1'];

/**
* @var Value
*/
private $value;
private Value $value;

/**
* @var Value
*/
private $emptyValue;
private Value $emptyValue;

protected function setUp(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/FieldType/EnhancedSelection/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class ValueTest extends TestCase
{
private $value;
private Value $value;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

final class EnhancedSelectionConverterTest extends TestCase
{
/**
* @var Converter
*/
private $converter;
private EnhancedSelectionConverter $converter;

protected function setUp(): void
{
Expand Down
5 changes: 1 addition & 4 deletions tests/Form/FieldTypeHandler/EnhancedSelectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

final class EnhancedSelectionTest extends TestCase
{
/**
* @var EnhancedSelection
*/
private $handler;
private EnhancedSelection $handler;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

final class NetgenEnhancedSelectionExtensionTest extends TestCase
{
/**
* @var NetgenEnhancedSelectionExtension
*/
private $extension;
private NetgenEnhancedSelectionExtension $extension;

protected function setUp(): void
{
Expand Down
13 changes: 4 additions & 9 deletions tests/Templating/Twig/NetgenEnhancedSelectionRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@
use Ibexa\Core\Repository\Values\ContentType\FieldDefinitionCollection;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Value;
use Netgen\Bundle\EnhancedSelectionBundle\Templating\Twig\NetgenEnhancedSelectionRuntime;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class NetgenEnhancedSelectionRuntimeTest extends TestCase
{
/**
* @var NetgenEnhancedSelectionRuntime
*/
private $runtime;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $contentTypeService;
private NetgenEnhancedSelectionRuntime $runtime;

private MockObject $contentTypeService;

protected function setUp(): void
{
Expand Down

0 comments on commit 0e0af72

Please sign in to comment.