From 8f40a8f749c7bff76a032559dab10c08f3a1e090 Mon Sep 17 00:00:00 2001 From: marvin255 Date: Mon, 9 Sep 2024 18:39:48 +0200 Subject: [PATCH] Refactor EloquentVersionManager --- composer.json | 8 +-- generator/ModelGenerator.php | 6 +- phpunit.xml.dist | 12 ++-- src/Entity/AddrObj.php | 6 +- src/Entity/AddrObjDivision.php | 6 +- src/Entity/AddrObjTypes.php | 6 +- src/Entity/AdmHierarchy.php | 6 +- src/Entity/ApartmentTypes.php | 6 +- src/Entity/Apartments.php | 6 +- src/Entity/Carplaces.php | 6 +- src/Entity/ChangeHistory.php | 6 +- src/Entity/FiasVersion.php | 6 +- src/Entity/HouseTypes.php | 6 +- src/Entity/Houses.php | 6 +- src/Entity/MunHierarchy.php | 6 +- src/Entity/NormativeDocs.php | 6 +- src/Entity/NormativeDocsKinds.php | 6 +- src/Entity/NormativeDocsTypes.php | 6 +- src/Entity/ObjectLevels.php | 6 +- src/Entity/OperationTypes.php | 6 +- src/Entity/Param.php | 6 +- src/Entity/ParamTypes.php | 6 +- src/Entity/ReestrObjects.php | 6 +- src/Entity/RoomTypes.php | 6 +- src/Entity/Rooms.php | 6 +- src/Entity/Steads.php | 6 +- src/VersionManager/EloquentVersionManager.php | 42 ++++++------- tests/BaseCase.php | 17 ++++++ .../EloquentVersionManagerTest.php | 59 +++++++++---------- 29 files changed, 170 insertions(+), 112 deletions(-) diff --git a/composer.json b/composer.json index a36d413..353556d 100644 --- a/composer.json +++ b/composer.json @@ -39,12 +39,12 @@ "coverage": "vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-html=tests/coverage", "fixer": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vv --allow-risky=yes", "linter": [ - "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vv --dry-run --stop-on-violation --allow-risky=yes", - "vendor/bin/psalm --show-info=true --php-version=$(php -r \"echo phpversion();\")" + "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vv --dry-run --stop-on-violation --allow-risky=yes", + "vendor/bin/psalm --show-info=true --php-version=$(php -r \"echo phpversion();\")" ], "entities": [ - "php -f generator/generate_entities.php", - "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes -q" + "php -f generator/generate_entities.php", + "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes -q" ] }, "extra": { diff --git a/generator/ModelGenerator.php b/generator/ModelGenerator.php index c080e27..05a86b1 100644 --- a/generator/ModelGenerator.php +++ b/generator/ModelGenerator.php @@ -61,6 +61,8 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor) $class->addComment("{$description}.\n"); } + $class->addComment('@psalm-consistent-constructor'); + $isPrimaryIsUuid = false; $primaryName = null; $fill = []; @@ -106,7 +108,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor) $fillableValue = new Literal("[\n '" . implode("',\n '", $fill) . "',\n]"); $class->addProperty('fillable', $fillableValue) ->setVisibility('protected') - ->addComment('@var string[]') + ->addComment('@var array') ; $castValue = new Literal($this->createCastValue($descriptor)); @@ -116,7 +118,7 @@ protected function decorateClass(ClassType $class, EntityDescriptor $descriptor) ; $connectionMethod = "\$connection = \$this->connection;\n"; - $connectionMethod .= "if (\\function_exists('app') && app()->has('config')) {\n"; + $connectionMethod .= "if (\\function_exists('app') && app()->has('config') === true) {\n"; $connectionMethod .= " /** @var string|null */\n"; $connectionMethod .= " \$connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: \$this->connection;\n"; $connectionMethod .= "}\n\n"; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9ba9cf1..6b67abb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,10 +1,5 @@ - - - - ./src - - + ./tests @@ -17,4 +12,9 @@ + + + ./src + + diff --git a/src/Entity/AddrObj.php b/src/Entity/AddrObj.php index 47b525d..ed8f4d6 100644 --- a/src/Entity/AddrObj.php +++ b/src/Entity/AddrObj.php @@ -9,6 +9,8 @@ /** * Сведения классификатора адресообразующих элементов. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор адресного объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -39,7 +41,7 @@ class AddrObj extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -85,7 +87,7 @@ class AddrObj extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/AddrObjDivision.php b/src/Entity/AddrObjDivision.php index 71da6a0..f9c9471 100644 --- a/src/Entity/AddrObjDivision.php +++ b/src/Entity/AddrObjDivision.php @@ -9,6 +9,8 @@ /** * Сведения по операциям переподчинения. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $parentid Родительский ID * @property int $childid Дочерний ID @@ -28,7 +30,7 @@ class AddrObjDivision extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'parentid', @@ -52,7 +54,7 @@ class AddrObjDivision extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/AddrObjTypes.php b/src/Entity/AddrObjTypes.php index e3e9e52..50b853e 100644 --- a/src/Entity/AddrObjTypes.php +++ b/src/Entity/AddrObjTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типам адресных объектов. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор записи * @property int $level Уровень адресного объекта * @property string $shortname Краткое наименование типа объекта @@ -33,7 +35,7 @@ class AddrObjTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'level', @@ -67,7 +69,7 @@ class AddrObjTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/AdmHierarchy.php b/src/Entity/AdmHierarchy.php index dbab5f0..e21f9ba 100644 --- a/src/Entity/AdmHierarchy.php +++ b/src/Entity/AdmHierarchy.php @@ -9,6 +9,8 @@ /** * Сведения по иерархии в административном делении. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта * @property int|null $parentobjid Идентификатор родительского объекта @@ -41,7 +43,7 @@ class AdmHierarchy extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -91,7 +93,7 @@ class AdmHierarchy extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/ApartmentTypes.php b/src/Entity/ApartmentTypes.php index 0bd653c..3b6e274 100644 --- a/src/Entity/ApartmentTypes.php +++ b/src/Entity/ApartmentTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типам помещений. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор типа (ключ) * @property string $name Наименование * @property string|null $shortname Краткое наименование @@ -32,7 +34,7 @@ class ApartmentTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -64,7 +66,7 @@ class ApartmentTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Apartments.php b/src/Entity/Apartments.php index 746b820..1fe451b 100644 --- a/src/Entity/Apartments.php +++ b/src/Entity/Apartments.php @@ -9,6 +9,8 @@ /** * Сведения по помещениям. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -38,7 +40,7 @@ class Apartments extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -82,7 +84,7 @@ class Apartments extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Carplaces.php b/src/Entity/Carplaces.php index 635648c..2153137 100644 --- a/src/Entity/Carplaces.php +++ b/src/Entity/Carplaces.php @@ -9,6 +9,8 @@ /** * Сведения по машино-местам. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -37,7 +39,7 @@ class Carplaces extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -79,7 +81,7 @@ class Carplaces extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/ChangeHistory.php b/src/Entity/ChangeHistory.php index b767caf..23b3552 100644 --- a/src/Entity/ChangeHistory.php +++ b/src/Entity/ChangeHistory.php @@ -9,6 +9,8 @@ /** * Сведения по истории изменений. * + * @psalm-consistent-constructor + * * @property int $changeid ID изменившей транзакции * @property int $objectid Уникальный ID объекта * @property string $adrobjectid Уникальный ID изменившей транзакции (GUID) @@ -30,7 +32,7 @@ class ChangeHistory extends Model /** @var string */ protected $primaryKey = 'changeid'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'changeid', 'objectid', @@ -58,7 +60,7 @@ class ChangeHistory extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/FiasVersion.php b/src/Entity/FiasVersion.php index a82656b..aa63447 100644 --- a/src/Entity/FiasVersion.php +++ b/src/Entity/FiasVersion.php @@ -9,6 +9,8 @@ /** * Модель, которая хранит историю версий ФИАС. * + * @psalm-consistent-constructor + * * @property int $version Номер версии ФИАС * @property string $fullurl Ссылка для загрузки полной версии ФИАС * @property string $deltaurl Ссылка для загрузки обновления до версии ФИАС @@ -28,7 +30,7 @@ class FiasVersion extends Model /** @var string */ protected $primaryKey = 'version'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'version', 'fullurl', @@ -52,7 +54,7 @@ class FiasVersion extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/HouseTypes.php b/src/Entity/HouseTypes.php index 234ffa5..b528c32 100644 --- a/src/Entity/HouseTypes.php +++ b/src/Entity/HouseTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типам домов. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор * @property string $name Наименование * @property string|null $shortname Краткое наименование @@ -32,7 +34,7 @@ class HouseTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -64,7 +66,7 @@ class HouseTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Houses.php b/src/Entity/Houses.php index 66a9ce3..d129aba 100644 --- a/src/Entity/Houses.php +++ b/src/Entity/Houses.php @@ -9,6 +9,8 @@ /** * Сведения по номерам домов улиц городов и населенных пунктов. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -42,7 +44,7 @@ class Houses extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -94,7 +96,7 @@ class Houses extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/MunHierarchy.php b/src/Entity/MunHierarchy.php index e22da41..6b50992 100644 --- a/src/Entity/MunHierarchy.php +++ b/src/Entity/MunHierarchy.php @@ -9,6 +9,8 @@ /** * Сведения по иерархии в муниципальном делении. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор адресного объекта * @property int|null $parentobjid Идентификатор родительского объекта @@ -36,7 +38,7 @@ class MunHierarchy extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -76,7 +78,7 @@ class MunHierarchy extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/NormativeDocs.php b/src/Entity/NormativeDocs.php index 9373c95..bf4415e 100644 --- a/src/Entity/NormativeDocs.php +++ b/src/Entity/NormativeDocs.php @@ -9,6 +9,8 @@ /** * Сведения о нормативном документе, являющемся основанием присвоения адресному элементу наименования. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор документа * @property string $name Наименование документа * @property \DateTimeInterface $date Дата документа @@ -36,7 +38,7 @@ class NormativeDocs extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -76,7 +78,7 @@ class NormativeDocs extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/NormativeDocsKinds.php b/src/Entity/NormativeDocsKinds.php index dae6b37..9c5158a 100644 --- a/src/Entity/NormativeDocsKinds.php +++ b/src/Entity/NormativeDocsKinds.php @@ -9,6 +9,8 @@ /** * Сведения по видам нормативных документов. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор записи * @property string $name Наименование */ @@ -26,7 +28,7 @@ class NormativeDocsKinds extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -46,7 +48,7 @@ class NormativeDocsKinds extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/NormativeDocsTypes.php b/src/Entity/NormativeDocsTypes.php index fdf4bce..6be4556 100644 --- a/src/Entity/NormativeDocsTypes.php +++ b/src/Entity/NormativeDocsTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типам нормативных документов. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор записи * @property string $name Наименование * @property \DateTimeInterface $startdate Дата начала действия записи @@ -28,7 +30,7 @@ class NormativeDocsTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -52,7 +54,7 @@ class NormativeDocsTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/ObjectLevels.php b/src/Entity/ObjectLevels.php index c873d4f..0945958 100644 --- a/src/Entity/ObjectLevels.php +++ b/src/Entity/ObjectLevels.php @@ -9,6 +9,8 @@ /** * Сведения по уровням адресных объектов. * + * @psalm-consistent-constructor + * * @property int $level Уникальный идентификатор записи. Ключевое поле. Номер уровня объекта * @property string $name Наименование * @property string|null $shortname Краткое наименование @@ -31,7 +33,7 @@ class ObjectLevels extends Model /** @var string */ protected $primaryKey = 'level'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'level', 'name', @@ -61,7 +63,7 @@ class ObjectLevels extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/OperationTypes.php b/src/Entity/OperationTypes.php index bb3efca..bc94430 100644 --- a/src/Entity/OperationTypes.php +++ b/src/Entity/OperationTypes.php @@ -9,6 +9,8 @@ /** * Сведения по статусу действия. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор статуса (ключ) * @property string $name Наименование * @property string|null $shortname Краткое наименование @@ -32,7 +34,7 @@ class OperationTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -64,7 +66,7 @@ class OperationTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Param.php b/src/Entity/Param.php index bf6dc8e..262ad13 100644 --- a/src/Entity/Param.php +++ b/src/Entity/Param.php @@ -9,6 +9,8 @@ /** * Сведения о классификаторе параметров адресообразующих элементов и объектов недвижимости. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор записи * @property int $objectid Глобальный уникальный идентификатор адресного объекта * @property int|null $changeid ID изменившей транзакции @@ -33,7 +35,7 @@ class Param extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -67,7 +69,7 @@ class Param extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/ParamTypes.php b/src/Entity/ParamTypes.php index aa431c4..a26ef33 100644 --- a/src/Entity/ParamTypes.php +++ b/src/Entity/ParamTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типу параметра. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор типа параметра (ключ) * @property string $name Наименование * @property string $code Краткое наименование @@ -32,7 +34,7 @@ class ParamTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -64,7 +66,7 @@ class ParamTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/ReestrObjects.php b/src/Entity/ReestrObjects.php index e9768a3..107a1ac 100644 --- a/src/Entity/ReestrObjects.php +++ b/src/Entity/ReestrObjects.php @@ -9,6 +9,8 @@ /** * Сведения об адресном элементе в части его идентификаторов. * + * @psalm-consistent-constructor + * * @property int $objectid Уникальный идентификатор объекта * @property \DateTimeInterface $createdate Дата создания * @property int $changeid ID изменившей транзакции @@ -31,7 +33,7 @@ class ReestrObjects extends Model /** @var string */ protected $primaryKey = 'objectid'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'objectid', 'createdate', @@ -61,7 +63,7 @@ class ReestrObjects extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/RoomTypes.php b/src/Entity/RoomTypes.php index d37b85e..be656b3 100644 --- a/src/Entity/RoomTypes.php +++ b/src/Entity/RoomTypes.php @@ -9,6 +9,8 @@ /** * Сведения по типам комнат. * + * @psalm-consistent-constructor + * * @property int $id Идентификатор типа (ключ) * @property string $name Наименование * @property string|null $shortname Краткое наименование @@ -32,7 +34,7 @@ class RoomTypes extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'name', @@ -64,7 +66,7 @@ class RoomTypes extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Rooms.php b/src/Entity/Rooms.php index 10f2984..e54783a 100644 --- a/src/Entity/Rooms.php +++ b/src/Entity/Rooms.php @@ -9,6 +9,8 @@ /** * Сведения по комнатам. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -38,7 +40,7 @@ class Rooms extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -82,7 +84,7 @@ class Rooms extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/Entity/Steads.php b/src/Entity/Steads.php index 99c4b7d..a3bc4e1 100644 --- a/src/Entity/Steads.php +++ b/src/Entity/Steads.php @@ -9,6 +9,8 @@ /** * Сведения по земельным участкам. * + * @psalm-consistent-constructor + * * @property int $id Уникальный идентификатор записи. Ключевое поле * @property int $objectid Глобальный уникальный идентификатор объекта типа INTEGER * @property string $objectguid Глобальный уникальный идентификатор адресного объекта типа UUID @@ -37,7 +39,7 @@ class Steads extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array */ protected $fillable = [ 'id', 'objectid', @@ -79,7 +81,7 @@ class Steads extends Model public function getConnectionName() { $connection = $this->connection; - if (\function_exists('app') && app()->has('config')) { + if (\function_exists('app') && app()->has('config') === true) { /** @var string|null */ $connection = app('config')->get('liquetsoft_fias.eloquent_connection') ?: $this->connection; } diff --git a/src/VersionManager/EloquentVersionManager.php b/src/VersionManager/EloquentVersionManager.php index 3d3b18f..4e9025b 100644 --- a/src/VersionManager/EloquentVersionManager.php +++ b/src/VersionManager/EloquentVersionManager.php @@ -5,45 +5,37 @@ namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\VersionManager; use Carbon\Carbon; -use Liquetsoft\Fias\Component\FiasInformer\InformerResponse; -use Liquetsoft\Fias\Component\FiasInformer\InformerResponseBase; +use Liquetsoft\Fias\Component\FiasInformer\FiasInformerResponse; +use Liquetsoft\Fias\Component\FiasInformer\FiasInformerResponseFactory; use Liquetsoft\Fias\Component\VersionManager\VersionManager; use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\FiasVersion; /** * Объект, который сохраняет текущую версию ФИАС с помощью eloquent. */ -class EloquentVersionManager implements VersionManager +final class EloquentVersionManager implements VersionManager { - /** - * @var string - */ - protected $entityClassName; - - public function __construct(string $entityClassName) + public function __construct(private readonly string $entityClassName) { - $this->entityClassName = $entityClassName; } /** * {@inheritdoc} * * @throws \RuntimeException - * - * @psalm-suppress InvalidStringClass */ - public function setCurrentVersion(InformerResponse $info): VersionManager + public function setCurrentVersion(FiasInformerResponse $info): void { $entityClassName = $this->getEntityClassName(); + /** @var FiasVersion */ $entity = new $entityClassName(); $entity->version = $info->getVersion(); - $entity->url = $info->getUrl(); + $entity->fullurl = $info->getFullUrl(); + $entity->deltaurl = $info->getDeltaUrl(); $entity->created_at = Carbon::now(); $entity->save(); - - return $this; } /** @@ -51,27 +43,31 @@ public function setCurrentVersion(InformerResponse $info): VersionManager * * @throws \RuntimeException * - * @psalm-suppress InvalidStringClass * @psalm-suppress MixedMethodCall */ - public function getCurrentVersion(): InformerResponse + public function getCurrentVersion(): ?FiasInformerResponse { - $response = new InformerResponseBase(); - $entityClassName = $this->getEntityClassName(); + /** @var FiasVersion|null */ $entity = $entityClassName::query()->orderBy('created_at', 'desc')->first(); + if ($entity) { - $response->setVersion($entity->version); - $response->setUrl($entity->url); + return FiasInformerResponseFactory::create( + $entity->version, + $entity->fullurl, + $entity->deltaurl + ); } - return $response; + return null; } /** * Возвращает класс сущности для обращения к Eloquent. * + * @psalm-return class-string + * * @throws \RuntimeException */ protected function getEntityClassName(): string diff --git a/tests/BaseCase.php b/tests/BaseCase.php index 8c4afa7..033d793 100644 --- a/tests/BaseCase.php +++ b/tests/BaseCase.php @@ -9,6 +9,7 @@ use Marvin255\FileSystemHelper\FileSystemException; use Marvin255\FileSystemHelper\FileSystemFactory; use Marvin255\FileSystemHelper\FileSystemHelperInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -130,4 +131,20 @@ protected function tearDown(): void parent::tearDown(); } + + /** + * @template T + * + * @param class-string $className + * + * @return MockObject&T + */ + protected function mock(string $className): MockObject + { + $mock = $this->getMockBuilder($className) + ->disableOriginalConstructor() + ->getMock(); + + return $mock; + } } diff --git a/tests/VersionManager/EloquentVersionManagerTest.php b/tests/VersionManager/EloquentVersionManagerTest.php index ccd0c80..b8bfc28 100644 --- a/tests/VersionManager/EloquentVersionManagerTest.php +++ b/tests/VersionManager/EloquentVersionManagerTest.php @@ -4,7 +4,7 @@ namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\VersionManager; -use Liquetsoft\Fias\Component\FiasInformer\InformerResponse; +use Liquetsoft\Fias\Component\FiasInformer\FiasInformerResponse; use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\FiasVersion; use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\EloquentTestCase; use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\VersionManager\EloquentVersionManager; @@ -14,7 +14,7 @@ * * @internal */ -class EloquentVersionManagerTest extends EloquentTestCase +final class EloquentVersionManagerTest extends EloquentTestCase { /** * Создает таблицу в бд перед тестами. @@ -28,7 +28,10 @@ protected function setUp(): void 'type' => 'integer', 'primary' => true, ], - 'url' => [ + 'fullurl' => [ + 'type' => 'string', + ], + 'deltaurl' => [ 'type' => 'string', ], 'created_at' => [ @@ -43,12 +46,14 @@ protected function setUp(): void */ public function testSetCurrentVersion(): void { - $version = $this->createFakeData()->numberBetween(1, 1000); - $url = $this->createFakeData()->url(); + $version = 123; + $fullUrl = 'https://test.test/full'; + $deltaUrl = 'https://test.test/delta'; - $info = $this->getMockBuilder(InformerResponse::class)->getMock(); - $info->method('getVersion')->willReturn($version); - $info->method('getUrl')->willReturn($url); + $info = $this->mock(FiasInformerResponse::class); + $info->expects($this->any())->method('getVersion')->willReturn($version); + $info->expects($this->any())->method('getFullUrl')->willReturn($fullUrl); + $info->expects($this->any())->method('getDeltaUrl')->willReturn($deltaUrl); $versionManager = new EloquentVersionManager(FiasVersion::class); $versionManager->setCurrentVersion($info); @@ -57,53 +62,45 @@ public function testSetCurrentVersion(): void 'fias_laravel_fias_version', [ 'version' => $version, - 'url' => $url, + 'fullurl' => $fullUrl, + 'deltaurl' => $deltaUrl, ] ); } - /** - * Проверяет, что объект выбросит исключение, если задан неверный класс сущности. - */ - public function testSetCurrentVersionWrongEntityException(): void - { - $info = $this->getMockBuilder(InformerResponse::class)->getMock(); - - $versionManager = new EloquentVersionManager('test'); - - $this->expectException(\RuntimeException::class); - $versionManager->setCurrentVersion($info); - } - /** * Проверяет, что объект правильно получает текущую версию. */ public function testGetCurrentVersion(): void { - $version = $this->createFakeData()->numberBetween(1, 1000); - $url = $this->createFakeData()->url(); + $version = 123; + $fullUrl = 'https://test.test/full'; + $deltaUrl = 'https://test.test/delta'; - $info = $this->getMockBuilder(InformerResponse::class)->getMock(); - $info->method('getVersion')->willReturn($version); - $info->method('getUrl')->willReturn($url); + $info = $this->mock(FiasInformerResponse::class); + $info->expects($this->any())->method('getVersion')->willReturn($version); + $info->expects($this->any())->method('getFullUrl')->willReturn($fullUrl); + $info->expects($this->any())->method('getDeltaUrl')->willReturn($deltaUrl); $versionManager = new EloquentVersionManager(FiasVersion::class); $versionManager->setCurrentVersion($info); - $versionResponse = $versionManager->getCurrentVersion(); $this->assertSame($version, $versionResponse->getVersion()); - $this->assertSame($url, $versionResponse->getUrl()); + $this->assertSame($fullUrl, $versionResponse->getFullUrl()); + $this->assertSame($deltaUrl, $versionResponse->getDeltaUrl()); } /** * Проверяет, что объект выбросит исключение, если задан неверный класс сущности. */ - public function testGetCurrentVersionWrongEntityException(): void + public function testSetCurrentVersionWrongEntityException(): void { + $info = $this->mock(FiasInformerResponse::class); + $versionManager = new EloquentVersionManager('test'); $this->expectException(\RuntimeException::class); - $versionResponse = $versionManager->getCurrentVersion(); + $versionManager->setCurrentVersion($info); } }